diff options
Diffstat (limited to 'enc')
-rw-r--r-- | enc/b64.go | 18 | ||||
-rw-r--r-- | enc/hex.go | 16 |
2 files changed, 19 insertions, 15 deletions
@@ -10,9 +10,9 @@ func HexToBase64(hex string) string { 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])) + a := (HexCharToDec(hb[i])<<8 | + HexCharToDec(hb[i+1])<<4 | + HexCharToDec(hb[i+2])) b64 += encode(a >> 6) b64 += encode(a & 0b111111) } @@ -22,15 +22,3 @@ func HexToBase64(hex string) string { 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 -} diff --git a/enc/hex.go b/enc/hex.go new file mode 100644 index 0000000..0d19538 --- /dev/null +++ b/enc/hex.go @@ -0,0 +1,16 @@ +// Copyright © 2020 rsiddharth <s@ricketyspace.net> +// SPDX-License-Identifier: ISC + +package enc + +// Adapted from +// https://go.googlesource.com/go/+/refs/tags/go1.15/src/encoding/hex/hex.go#83 +func HexCharToDec(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 +} |