From 209a14eff23fd2f75b1e82288505b04a0596a110 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 29 Aug 2020 13:37:41 -0400 Subject: 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 --- lib/b64.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 lib/b64.go (limited to 'lib/b64.go') 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 +// 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]) +} -- cgit v1.2.3