diff options
author | rsiddharth <s@ricketyspace.net> | 2020-08-30 20:15:58 -0400 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2020-08-30 20:15:58 -0400 |
commit | c1240b472934f0b7649922722fe0d61c0c1c6cac (patch) | |
tree | 2e35f187c5caf7e81486465cfe1170ef1ce0b0e6 | |
parent | 60cd4c5cedf546c282b2bcb0f959596d4ed364a3 (diff) |
challenge: do challenge 5
* challenge/c05.go: Implement challenge 5.
* cryptopals.go (main): Add handling to run challenge 5.
* lib/hex.go (BytesToHexStr): New function.
* lib/xor.go (RepeatingXOR): New function.
-rw-r--r-- | challenge/c05.go | 21 | ||||
-rw-r--r-- | cryptopals.go | 2 | ||||
-rw-r--r-- | lib/hex.go | 12 | ||||
-rw-r--r-- | lib/xor.go | 30 |
4 files changed, 65 insertions, 0 deletions
diff --git a/challenge/c05.go b/challenge/c05.go new file mode 100644 index 0000000..7f22663 --- /dev/null +++ b/challenge/c05.go @@ -0,0 +1,21 @@ +// Copyright © 2020 rsiddharth <s@ricketyspace.net> +// SPDX-License-Identifier: ISC + +package challenge + +import ( + "fmt" + "ricketyspace.net/cryptopals/lib" +) + +var icebaby string = `Burning 'em, if you ain't quick and nimble +I go crazy when I hear a cymbal` + +var key string = "ICE" + +func C5() { + es := lib.RepeatingXOR(icebaby, key) + hs := lib.BytesToHexStr([]byte(es)) + + fmt.Printf("RepeatingXOR('%v', '%v') = %v\n", icebaby, key, hs) +} diff --git a/cryptopals.go b/cryptopals.go index 671564b..0d0a557 100644 --- a/cryptopals.go +++ b/cryptopals.go @@ -26,5 +26,7 @@ func main() { challenge.C3() case 4: challenge.C4() + case 5: + challenge.C5() } } @@ -53,3 +53,15 @@ func ByteToHexStr(b byte) string { return s } + +func BytesToHexStr(bs []byte) string { + hs := "" + if len(bs) < 1 { + return hs + } + + for i := 0; i < len(bs); i++ { + hs += ByteToHexStr(bs[i]) + } + return hs +} @@ -21,3 +21,33 @@ func FixedXOR(a, b string) string { } return cs } + +// Both 'data' and 'key' need to be plain ascii string. +func RepeatingXOR(data, key string) string { + xs := "" + if len(data) < 1 || len(key) < 1 { + return xs + } + + // data in bytes + db := []byte(data) + + // key in bytes + dk := []byte(key) + + lk := len(key) + for i, ki := 0, 0; i < len(db); i++ { + if ki == lk { + ki = 0 + } + + // xor a byte + eb := db[i] ^ dk[ki] + + // append to result + xs += string(eb) + + ki += 1 + } + return xs +} |