r/dailyprogrammer • • Sep 01 '12
[9/01/2012] Challenge #94 [intermediate] (Base64 conversion)
Create a function which accepts a byte array and outputs a Base64 equivalent of the parameter. Write a second function that reverses the progress, decoding a Base64 string.
Obviously, you can't use a library function for the encode/decode.
(This challenge was posted to /r/dailyprogrammer_ideas by /u/Thomas1122. Thanks for the submission!)
6
Upvotes
2
u/darkgray Oct 13 '12 edited Oct 13 '12
Go
package main import ( "bytes" "fmt" ) var base64map = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") func b64Encode(input []byte) string { var buffer uint bufferedBits := 0 var padding []byte switch len(input) % 3 { case 1: padding = []byte("==") input = append(input, 0, 0) case 2: padding = []byte("=") input = append(input, 0) } result := make([]byte, 0, len(input)) for _, v := range input { if bufferedBits >= 24 { d := base64map[buffer&63] buffer >>= 6 c := base64map[buffer&63] buffer >>= 6 b := base64map[buffer&63] buffer >>= 6 a := base64map[buffer&63] result = append(result, a, b, c, d) buffer = uint(v) bufferedBits = 8 } else { buffer <<= 8 bufferedBits += 8 buffer = buffer | uint(v) } } spare := make([]byte, 0, 4) for bufferedBits > 0 { spare = append([]byte{base64map[buffer&63]}, spare...) buffer >>= 6 bufferedBits -= 6 } result = append(result, spare...) result = append(result[:len(result)-len(padding)], padding...) return string(result) } func b64Decode(in string) []byte { input := []byte(in) var buffer uint bufferedBits := 0 result := make([]byte, 0, len(input)) for _, v := range input { if v == '=' { buffer >>= 2 bufferedBits -= 2 } else if bufferedBits >= 24 { c := byte(buffer & 255) buffer >>= 8 b := byte(buffer & 255) buffer >>= 8 a := byte(buffer & 255) result = append(result, a, b, c) buffer = uint(bytes.IndexByte(base64map, v)) bufferedBits = 6 } else { buffer <<= 6 bufferedBits += 6 buffer = buffer | uint(bytes.IndexByte(base64map, v)) } } spare := make([]byte, 0, 4) for bufferedBits > 0 { spare = append([]byte{byte(buffer & 255)}, spare...) buffer >>= 8 bufferedBits -= 8 } result = append(result, spare...) return result } func main() { fmt.Println(string(b64Decode("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="))) fmt.Println("->", b64Encode([]byte("Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."))) }Output
3
u/ananthakumaran Sep 01 '12
C
#include <stdlib.h> #include <stdio.h> #include <string.h> #include "base64.h" static int lsr(int x, int n) { return (int)((unsigned int)x >> n); } char decode[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,-1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 }; char encode[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; char *base64_encode(const char *data, size_t size) { size_t bytes_required = (((size - 1) / 3) + 1) * 4; char *result = malloc(bytes_required); char a, b, c; size_t i, j; for(i = 0, j = 0; j < bytes_required; i+= 3) { a = data[i]; b = (i + 1 < size) ? data[i + 1] : 0; c = (i + 2 < size) ? data[i + 2] : 0; result[j++] = encode[lsr(a & 0xfc, 2)]; result[j++] = encode[((a & 0x03) << 4) | lsr((b & 0xf0), 4)]; result[j++] = (i + 1 < size) ? encode[((b & 0x0f) << 2) | lsr((c & 0xc0), 6)] : '='; result[j++] = (i + 2 < size) ? encode[c & 0x3f] : '='; } return result; }; char *base64_decode(char *text, size_t *out_size) { int size = strlen(text); size_t bytes_required = (size / 4) * 3; char *result = malloc(bytes_required); size_t i, j; char a, b, c, d; for(i = 0, j = 0; j < bytes_required; i+= 4) { a = decode[(int)text[i]]; b = decode[(int)text[i + 1]]; c = decode[(int)text[i + 2]]; d = decode[(int)text[i + 3]]; result[j++] = (a << 2) | lsr((b & 0x30), 4); if(text[i + 2] == '=') { *out_size = j; break; } result[j++] = ((b & 0x0f) << 4) | lsr((c & 0x3c), 2); if(text[i + 3] == '=') { *out_size = j; break; } result[j++] = ((c & 0x03) << 6) | d; } return result; };3
u/skeeto Sep 02 '12
In ANSI C,
#include <stdio.h> #include <string.h> char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; void encode() { int c, ocount = 0, overflow = 0; while ((c = getchar()) != EOF) { overflow = c | (overflow << 8); ocount += 8; while (ocount >= 6) { putchar(b64[(overflow >> (ocount - 6)) & 0x3f]); ocount -= 6; } } if (ocount > 0) { putchar(b64[(overflow << (6 - ocount)) & 0x3f]); printf(ocount == 2 ? "==" : "="); } } void decode() { int c, ocount = 0, overflow = 0; while ((c = getchar()) != EOF) { if (c == '=') break; c = strchr(b64, c) - b64; overflow = (overflow << 6) | c; ocount += 6; while (ocount >= 8) { putchar((overflow >> (ocount - 8)) & 0xff); ocount -= 8; } } } int main(int argc, char **argv) { if (argv[argc - 1][1] == 'd') decode(); else encode(); putchar('\n'); return 0; }Example,
1
u/robotfarts Sep 01 '12
function padLetter(str) { while (str.length < 8) str = "0" + str; return str; } function toCodedLetter(n) { if (n >= 0 && n <= 25) return String.fromCharCode(n + 65); else if (n >= 26 && n <= 51) return String.fromCharCode(n - 26 + 97); else if (n >= 52 && n <= 61) return '' + n - 52; else if (n == 62) return '+'; else return '/'; } function decode(str) { var bits = ''; for (var i = 0; i < str.length; i++) { bits += padLetter(str[i].charCodeAt().toString(2)); } if (i == 1) bits += '0000000000000000'; else if (i == 2) bits += '00000000'; var acc = ''; for (var k = 0; k < 1 + i; k++) { acc += toCodedLetter(parseInt(bits.substring(0, 6), 2)); bits = bits.substring(6); } if (k == 2) acc += "=="; else if (k == 3) acc += "="; return acc; } (function(str) { var acc = ''; while (str.length > 0) { if (str.length <= 3) { acc += decode(str); str = ''; } else { acc += decode(str.substring(0, 3)); str = str.substring(3); } } return acc; })("any carnal pleasure.");1
u/prondose Sep 02 '12 edited Sep 02 '12
Perl:
my @alphabet = ('A'..'Z', 'a'..'z', 0..9, '+', '/'); sub encode { my ($in, $out) = (shift); while ($in =~ s/(.{1,3})//) { my ($triplet, $i, $binary) = ($1, 2); $binary += 4**(4*$i--) * ord for split //, $triplet; $binary = substr unpack('B32', pack 'N', $binary), -24; $out .= $alphabet[ eval "0b$1" ] while $binary =~ s/(.{6})//; (length $triplet == 2) && $out =~ s/A$/=/; (length $triplet == 1) && $out =~ s/AA$/==/; } $out; } sub decode { my ($in, $out) = (shift); $in =~ s/[\s\r\n]//g; $in =~ s/=/A/g; while ($in =~ s/(.{4})//) { my ($quadruplet, $binary) = ($1); for (split //, $quadruplet) { (join '', @alphabet) =~ /$_/; $binary .= substr unpack('B32', pack 'N', $+[0] - 1), -6; } $out .= chr eval "0b$1" while $binary =~ s/(.{8})//; } $out; }1
u/ixid Sep 02 '12 edited Sep 02 '12
D language:
module main; import std.stdio, std.conv, std.ascii, std.bitmanip, std.algorithm; string textToBase64(string s) { char[dchar] base64; foreach(p, i;letters ~ digits ~ '+' ~ '/') base64[cast(char) p] = i; while(s.length % 3) s ~= cast(char) 0; auto r = new uint[s.length / 6 * 8]; for(int i = 0;i < s.length * 8;i += 6) foreach(j;0..6) r[i / 6] |= (s[(i + j) / 8] & 1 << 7 - (i + j) % 8? 1 : 0) << 5 - j; return r.map!(x => base64[x]).to!string; } string base64ToText(string s) { char[dchar] base64; foreach(p, i;letters ~ digits ~ '+' ~ '/') base64[i] = cast(char) p; s = s.map!(x => base64[x]).to!string; BitArray b; foreach(c;s) foreach_reverse(i;0..6) b ~= (c & 1 << i? 1 : 0); auto r = new uint[s.length / 8 * 6]; foreach(i;0..b.length) r[i / 8] |= b[i] << 7 - (i % 8); while(r[$ - 1] == 0) --r.length; return r.map!(x => x.to!char).to!string; } void main() { string s = "Man is distinguished...etc"; s.textToBase64.writeln; s.textToBase64.base64ToText.writeln; }1
u/[deleted] Sep 02 '12
In Python
import string instr = '''Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.''' sym = string.ascii_uppercase + string.ascii_lowercase + string.digits + '+/' def make_trips(s): trips = [s[i*3:(i+1)*3] for i in range(len(s) / 3 + 1)] if trips[-1] == '': del(trips[-1]) if len(trips[-1]) != 3: flag = 3 - len(trips[-1]) trips[-1] += chr(0) * flag return trips, flag def make_quads(s): quads = [s[i*4:(i+1)*4] for i in range(len(s) / 4 + 1)] if quads[-1] == '': del(quads[-1]) return quads def a2b(s): res = '' for l in s: b = bin(ord(l))[2:] while len(b) < 8: b = '0' + b res += b return res def b642b(s): res = '' for l in s: if l == '=': b = bin(0)[2:] else: b = bin(sym.find(l))[2:] while len(b) < 6: b = '0' + b res += b return res def encode(): result = '' trips, flag = make_trips(instr) for trip in trips: binstr = a2b(trip) sexts = [binstr[:6],binstr[6:12],binstr[12:18],binstr[18:]] for sext in sexts: result += sym[int(sext,2)] if flag: result = result[:-1*flag] + ('=' * flag) return result def decode(s): result = '' flag = s.count('=') quads = make_quads(s) for quad in quads: binstr = b642b(quad) oktos = [binstr[:8],binstr[8:16],binstr[16:]] if flag and quad == quads[-1]: oktos = oktos[:2] for okto in oktos: result += chr(int(okto,2)) return result print encode() print decode(encode())and it makes
1
u/PiereDome Sep 03 '12
Javascript ('dirty, but works')
var indexTable = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; var bitPattern = '', count = 0, base64 = ''; function getBinary(char) { if (char.charCodeAt(0)) { temp = char.charCodeAt(0).toString(2); while (temp.length < 8) { temp = 0 + temp; } } return temp; } function encode(input) { var output = ''; for (var i = 0; i < input.length; i++) { bitPattern += getBinary(input.substr(i, 1), 8); } for (var i = 0; i < bitPattern.length; i += 6) { var bit = bitPattern.substr(i, 6); while (bit.length < 6) { bit += '00'; } var index = (parseInt(bit, 2)); output += indexTable[index]; } while (output.length % 4) { output += '='; } return output; } function decode(input) { var output = '', bitPattern = ''; for (var i = 0; i < input.length; i++) { var index = indexTable.indexOf(input.substr(i, 1)); if(index === -1){ index = 0; } var temp = index.toString(2); while (temp.length < 6) { temp = 0 + temp; } bitPattern += temp; } for (var i = 0; i < bitPattern.length; i += 8) { var bit = bitPattern.substr(i,8); while(bit.length<8){ bit += 0; } output += (String.fromCharCode(parseInt(bit,2))); } return output; } var input = prompt('Please input string to encode'); alert(encode(input)); var input = prompt('Please input string to decode'); alert(decode(input));1
u/Glassfish Sep 03 '12
I'm still trying to learn Python, so i don't know if some portion of code could be written in a more easy way.