diff options
author | rsiddharth <s@ricketyspace.net> | 2020-08-29 13:37:41 -0400 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2020-08-29 13:37:41 -0400 |
commit | 209a14eff23fd2f75b1e82288505b04a0596a110 (patch) | |
tree | 6ffc3fd8d77e0c45bb71d7b76b24090f45709adf /lib | |
parent | 3cc030abc080a79101998afa5df99e7ef5d4a2fd (diff) |
rename package enc -> hex
* Makefile (fmt): fmt lib instead of enc
* challenge/c01.go: Use lib instead of enc
* challenge/c02.go: Use lib instead of enc
* enc/b64.go -> lib/b64.go
* enc/hex.go -> lib/hex.go
* enc/xor.go -> lib/xor.go
Diffstat (limited to 'lib')
-rw-r--r-- | lib/b64.go | 24 | ||||
-rw-r--r-- | lib/hex.go | 26 | ||||
-rw-r--r-- | lib/xor.go | 22 |
3 files changed, 72 insertions, 0 deletions
diff --git a/lib/b64.go b/lib/b64.go new file mode 100644 index 0000000..d37f710 --- /dev/null +++ b/lib/b64.go @@ -0,0 +1,24 @@ +// Copyright © 2020 rsiddharth <s@ricketyspace.net> +// SPDX-License-Identifier: ISC + +package lib + +const b64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" + +func HexToBase64(hex string) string { + hb := []byte(hex) + + b64 := "" + for i := 0; i <= len(hb)-3; i = i + 3 { + a := (HexCharToDec(hb[i])<<8 | + HexCharToDec(hb[i+1])<<4 | + HexCharToDec(hb[i+2])) + b64 += encode(a >> 6) + b64 += encode(a & 0b111111) + } + return b64 +} + +func encode(b uint16) string { + return string(b64_table[b]) +} diff --git a/lib/hex.go b/lib/hex.go new file mode 100644 index 0000000..bf539bc --- /dev/null +++ b/lib/hex.go @@ -0,0 +1,26 @@ +// Copyright © 2020 rsiddharth <s@ricketyspace.net> +// SPDX-License-Identifier: ISC + +package lib + +// 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 +} + +func DecToHexChar(i uint16) byte { + switch { + case 0 <= i && i <= 9: + return byte(48 + i) + case 10 <= i && i <= 15: + return byte(97 + (i - 10)) + } + return 0 +} diff --git a/lib/xor.go b/lib/xor.go new file mode 100644 index 0000000..270999c --- /dev/null +++ b/lib/xor.go @@ -0,0 +1,22 @@ +// Copyright © 2020 rsiddharth <s@ricketyspace.net> +// SPDX-License-Identifier: ISC + +package lib + +func FixedXOR(a, b string) string { + cs := "" + if len(a) != len(b) { + return cs + } + + ab := []byte(a) + bb := []byte(b) + for i := 0; i < len(ab); i++ { + p := HexCharToDec(ab[i]) + q := HexCharToDec(bb[i]) + r := DecToHexChar(p ^ q) + + cs += string(r) + } + return cs +} |