From 9db1426878ff5b81f66d6d73bf69555eb8e5b5d2 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 29 Aug 2020 13:30:47 -0400 Subject: challenge: do challenge 2 * challenge/c02.go: Challenge 2 snafu. * cryptopals.go: Add challenge 2. * enc/hex.go (DecToHexChar): New function. * enc/xor.go (FixedXOR): New function. --- challenge/c02.go | 14 ++++++++++++++ cryptopals.go | 2 ++ enc/hex.go | 10 ++++++++++ enc/xor.go | 19 +++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 challenge/c02.go create mode 100644 enc/xor.go diff --git a/challenge/c02.go b/challenge/c02.go new file mode 100644 index 0000000..deab152 --- /dev/null +++ b/challenge/c02.go @@ -0,0 +1,14 @@ +package challenge + +import ( + "fmt" + "ricketyspace.net/cryptopals/enc" +) + +func C2() { + a := "1c0111001f010100061a024b53535009181c" + b := "686974207468652062756c6c277320657965" + c := enc.FixedXOR(a, b) + + fmt.Printf("XOR(%v ^ %v) = %v\n", a, b, c) +} diff --git a/cryptopals.go b/cryptopals.go index 9c1c803..37580be 100644 --- a/cryptopals.go +++ b/cryptopals.go @@ -20,5 +20,7 @@ func main() { flag.PrintDefaults() case 1: challenge.C1() + case 2: + challenge.C2() } } diff --git a/enc/hex.go b/enc/hex.go index 0d19538..a1b9b46 100644 --- a/enc/hex.go +++ b/enc/hex.go @@ -14,3 +14,13 @@ func HexCharToDec(c byte) uint16 { } 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 new file mode 100644 index 0000000..423c0a2 --- /dev/null +++ b/enc/xor.go @@ -0,0 +1,19 @@ +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 +} -- cgit v1.2.3