From e57a76620a5de1f627a50ab63635f6f1cae39c8c Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 5 Sep 2020 16:40:10 -0400 Subject: lib: add Base64ToBytes * lib/b64.go (Base64ToBytes): New function. lib/b64.go --- lib/b64.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'lib') diff --git a/lib/b64.go b/lib/b64.go index 1fb45dd..f566b14 100644 --- a/lib/b64.go +++ b/lib/b64.go @@ -19,6 +19,38 @@ func HexToBase64(hex string) string { return b64 } +func Base64ToBytes(enc string) []byte { + enc = stripSpaceChars(enc) + + l := len(enc) + bs := make([]byte, 3*(l/4)) + + // Base64 decode. + for i, j := 0, 0; i <= l-4; i, j = i+4, j+3 { + // Jam 24 bits together. + a := index(enc[i])<<18 | + index(enc[i+1])<<12 | + index(enc[i+2])<<6 | + index(enc[i+3]) + + // Get first byte. + bs[j] = byte(a >> 16) + + if enc[i+2] == '=' { + return bs[0 : j+1] + } + // Get second byte. + bs[j+1] = byte((a & 0xff00) >> 8) + + if enc[i+3] == '=' { + return bs[0 : j+2] + } + // Get third byte. + bs[j+2] = byte(a & 0xff) + } + return bs +} + func encode(b uint16) string { return string(b64_table[b]) } -- cgit v1.2.3