summaryrefslogtreecommitdiffstats
path: root/enc/b64.go
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2020-08-26 23:23:53 -0400
committerrsiddharth <s@ricketyspace.net>2020-08-26 23:23:53 -0400
commita5592f87b39756a2e2df57dd066e3525f3ea24e1 (patch)
treee82d8e750febfd62653d9d1a9813ede21ce38bf3 /enc/b64.go
parentdb2d9fefcbb46818e832d923c9b1bed1752e7727 (diff)
enc/b64.go: flesh it out
Diffstat (limited to 'enc/b64.go')
-rw-r--r--enc/b64.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/enc/b64.go b/enc/b64.go
index e69de29..0ea0a4c 100644
--- a/enc/b64.go
+++ b/enc/b64.go
@@ -0,0 +1,31 @@
+package enc
+
+const b64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
+
+func HexToBase64(hex string) string {
+ hb := []byte(hex)
+
+ b64 := ""
+ for i := 0; i <= len(hb)-3; i = i + 3 {
+ a := fromHexChar(hb[i])<<8 | fromHexChar(hb[i+1])<<4 | fromHexChar(hb[i+2])
+ b64 += encode(a >> 6)
+ b64 += encode(a & 0b111111)
+ }
+ return b64
+}
+
+func encode(b uint16) string {
+ return string(b64_table[b])
+}
+
+// adapted from
+// https://go.googlesource.com/go/+/refs/tags/go1.15/src/encoding/hex/hex.go#83
+func fromHexChar(c byte) uint16 {
+ switch {
+ case '0' <= c && c <= '9':
+ return uint16(c - '0')
+ case 'a' <= c && c <= 'f':
+ return uint16(c - 'a' + 10)
+ }
+ return 0
+}