summaryrefslogtreecommitdiffstats
path: root/lib/xor.go
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2020-08-30 20:15:58 -0400
committerrsiddharth <s@ricketyspace.net>2020-08-30 20:15:58 -0400
commitc1240b472934f0b7649922722fe0d61c0c1c6cac (patch)
tree2e35f187c5caf7e81486465cfe1170ef1ce0b0e6 /lib/xor.go
parent60cd4c5cedf546c282b2bcb0f959596d4ed364a3 (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.
Diffstat (limited to 'lib/xor.go')
-rw-r--r--lib/xor.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/xor.go b/lib/xor.go
index ae2bd12..0d1f3d7 100644
--- a/lib/xor.go
+++ b/lib/xor.go
@@ -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
+}