-
-
Save nodegin/9ee3b717be72b54c6ef5 to your computer and use it in GitHub Desktop.
use braille patterns for base64, instead of alphanumerics. joke.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function _b64enc($in, $padding = false) | |
| { | |
| $inLen = count($in); | |
| $out = []; | |
| for ($i = 0; $i < $inLen; $i += 3) | |
| { | |
| $out[] = $in[$i] >> 2; | |
| if ($i + 1 < $inLen) | |
| { | |
| $out[] = (($in[$i] << 4) | ($in[$i + 1] >> 4)) & 63; | |
| if ($i + 2 < $inLen) | |
| { | |
| $out[] = (($in[$i + 1] << 2) | ($in[$i + 2] >> 6)) & 63; | |
| $out[] = $in[$i + 2] & 63; | |
| } | |
| else | |
| { | |
| $out[] = ($in[$i + 1] << 2) & 63; | |
| if ($padding) | |
| $out[] = -1; | |
| } | |
| } | |
| else | |
| { | |
| $out[] = ($in[$i] << 4) & 63; | |
| if ($padding) | |
| { | |
| $out[] = -1; | |
| $out[] = -1; | |
| } | |
| } | |
| } | |
| return $out; | |
| } | |
| function _b64dec($in, $padding = false) | |
| { | |
| $inLen = count($in); | |
| if (!$padding) | |
| { | |
| $in[] = -1; | |
| $in[] = -1; | |
| } | |
| $out = []; | |
| for ($i = 0; $i < $inLen; $i += 4) | |
| { | |
| $o = ($in[$i] << 18) | ($in[$i + 1] << 12); | |
| $r = 1; | |
| if ($in[$i + 2] >= 0) | |
| { | |
| $o |= $in[$i + 2] << 6; | |
| $r = 2; | |
| if ($in[$i + 3] >= 0) | |
| { | |
| $o |= $in[$i + 3]; | |
| $r = 3; | |
| } | |
| } | |
| $out[] = $o >> 16; | |
| if ($r >= 2) | |
| { | |
| $out[] = ($o >> 8) & 255; | |
| if ($r >= 3) | |
| $out[] = $o & 255; | |
| } | |
| } | |
| return $out; | |
| } | |
| function unichr($i) { | |
| return iconv('UCS-4LE', 'UTF-8', pack('V', $i)); | |
| } | |
| function uniord($u) { | |
| $k = mb_convert_encoding($u, 'UCS-2LE', 'UTF-8'); | |
| $k1 = ord(substr($k, 0, 1)); | |
| $k2 = ord(substr($k, 1, 1)); | |
| return $k2 * 256 + $k1; | |
| } | |
| function braille_encode($text) | |
| { | |
| preg_match_all('/./', $text, $out); | |
| $b64 = _b64enc(array_map('ord', $out[0]), false); | |
| return join('', array_map(function($x) { | |
| return unichr($x + 0x2800); | |
| }, $b64)); | |
| } | |
| function braille_decode($text) | |
| { | |
| $chars = preg_split('/(?!^)(?=.)/u', $text); | |
| $data = _b64dec(array_map(function($c) { | |
| return uniord($c) - 0x2800; | |
| }, $chars), false); | |
| return join('', array_map('chr', $data)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| #coding:utf-8 | |
| import sys | |
| def base64_encode_(in_data, padding=False): | |
| in_len = len(in_data) | |
| out_data = [] | |
| for i in range(0, in_len, 3): | |
| out_data.append(in_data[i] >> 2) | |
| if i+1 < in_len: | |
| out_data.append(((in_data[i] << 4) | (in_data[i+1] >> 4)) & 63) | |
| if i+2 < in_len: | |
| out_data.append(((in_data[i+1] << 2) | (in_data[i+2] >> 6)) & 63) | |
| out_data.append(in_data[i+2] & 63) | |
| else: | |
| out_data.append((in_data[i+1] << 2) & 63) | |
| if padding: | |
| out_data.append(-1) | |
| else: | |
| out_data.append((in_data[i] << 4) & 63) | |
| if padding: | |
| out_data.append(-1) | |
| out_data.append(-1) | |
| return out_data | |
| def base64_decode_(in_data, padding=False): | |
| in_len = len(in_data) | |
| if not padding: | |
| in_data.append(-1) | |
| in_data.append(-1) | |
| out_data = [] | |
| for i in range(0, in_len, 4): | |
| o = (in_data[i] << 18) | (in_data[i+1] << 12) | |
| r = 1 | |
| if in_data[i+2] >= 0: | |
| o |= (in_data[i+2] << 6) | |
| r = 2 | |
| if in_data[i+3] >= 0: | |
| o |= in_data[i+3] | |
| r = 3 | |
| out_data.append(o >> 16) | |
| if r >= 2: | |
| out_data.append((o >> 8) & 255) | |
| if r >= 3: | |
| out_data.append(o & 255) | |
| return out_data | |
| def ord64(s): | |
| if s == '=': return -1 | |
| elif s == '+': return 62 | |
| elif s == '/': return 63 | |
| c = ord(s) | |
| if c < 0x30: return -1 | |
| elif c <= 0x39: return c - 0x30 + 52 | |
| elif c < 0x41: return -1 | |
| elif c <= 0x5A: return c - 0x41 | |
| elif c < 0x61: return -1 | |
| elif c <= 0x7A: return c - 0x61 + 26 | |
| return -1 | |
| BASE64_TABLE = "=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" | |
| def base64_encode(text): | |
| b64 = base64_encode_(map(ord, text), padding=True) | |
| return ''.join(map(lambda x:BASE64_TABLE[1+x], b64)) | |
| def base64_decode(text): | |
| data = base64_decode_(map(ord64, text), padding=True) | |
| return ''.join(map(chr, data)) | |
| def base64t_encode(text): | |
| b64 = base64_encode_(map(ord, text), padding=False) | |
| return ''.join(map(lambda x:unichr(0x2800+x), b64)).encode('utf-8') | |
| def base64t_decode(text): | |
| data = base64_decode_(map(lambda c:ord(c)-0x2800, text.decode('utf-8')), padding=False) | |
| return ''.join(map(chr, data)) | |
| assert("QUJDREVGRw==" == base64_encode("ABCDEFG")) | |
| assert("ABCDEFG" == base64_decode("QUJDREVGRw==")) | |
| assert("⠐⠔⠉⠃⠑⠄⠕⠆⠑⠰" == base64t_encode("ABCDEFG")) | |
| assert("ABCDEFG" == base64t_decode("⠐⠔⠉⠃⠑⠄⠕⠆⠑⠰")) | |
| if __name__ == '__main__': | |
| for line in sys.stdin: | |
| enc = base64t_encode(line.rstrip()) | |
| dec = base64t_decode(enc) | |
| print enc, dec | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment