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/hex.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 lib/hex.go (limited to 'lib/hex.go') 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 +// 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 +} -- cgit v1.2.3