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 --- Makefile | 2 +- challenge/c01.go | 4 ++-- challenge/c02.go | 4 ++-- enc/b64.go | 24 ------------------------ enc/hex.go | 26 -------------------------- enc/xor.go | 22 ---------------------- lib/b64.go | 24 ++++++++++++++++++++++++ lib/hex.go | 26 ++++++++++++++++++++++++++ lib/xor.go | 22 ++++++++++++++++++++++ 9 files changed, 77 insertions(+), 77 deletions(-) delete mode 100644 enc/b64.go delete mode 100644 enc/hex.go delete mode 100644 enc/xor.go create mode 100644 lib/b64.go create mode 100644 lib/hex.go create mode 100644 lib/xor.go diff --git a/Makefile b/Makefile index fd91059..2abddd3 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ build: fmt go build fmt: - go fmt -x ${MOD} ${MOD}/challenge ${MOD}/enc + go fmt -x ${MOD} ${MOD}/challenge ${MOD}/lib clean: go clean diff --git a/challenge/c01.go b/challenge/c01.go index b7f5a4d..656a859 100644 --- a/challenge/c01.go +++ b/challenge/c01.go @@ -5,12 +5,12 @@ package challenge import ( "fmt" - "ricketyspace.net/cryptopals/enc" + "ricketyspace.net/cryptopals/lib" ) func C1() { hex := "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d" - b64 := enc.HexToBase64(hex) + b64 := lib.HexToBase64(hex) fmt.Printf("b64(0x%v) = %v\n", hex, b64) } diff --git a/challenge/c02.go b/challenge/c02.go index d8f03c9..eb45a4f 100644 --- a/challenge/c02.go +++ b/challenge/c02.go @@ -5,13 +5,13 @@ package challenge import ( "fmt" - "ricketyspace.net/cryptopals/enc" + "ricketyspace.net/cryptopals/lib" ) func C2() { a := "1c0111001f010100061a024b53535009181c" b := "686974207468652062756c6c277320657965" - c := enc.FixedXOR(a, b) + c := lib.FixedXOR(a, b) fmt.Printf("XOR(%v ^ %v) = %v\n", a, b, c) } diff --git a/enc/b64.go b/enc/b64.go deleted file mode 100644 index 4c251a2..0000000 --- a/enc/b64.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright © 2020 rsiddharth -// SPDX-License-Identifier: ISC - -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 := (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/enc/hex.go b/enc/hex.go deleted file mode 100644 index a1b9b46..0000000 --- a/enc/hex.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright © 2020 rsiddharth -// 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 -} - -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/enc/xor.go b/enc/xor.go deleted file mode 100644 index fe412b2..0000000 --- a/enc/xor.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright © 2020 rsiddharth -// SPDX-License-Identifier: ISC - -package enc - -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 -} 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]) +} 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 +} 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 +// 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 +} -- cgit v1.2.3