diff --git a/set1/challenge_2.go b/set1/challenge_2.go new file mode 100644 index 0000000..3f7bb77 --- /dev/null +++ b/set1/challenge_2.go @@ -0,0 +1,38 @@ +package set1 + +func XORHexString(content, password string) string { + var hexValues = map[byte]byte{ + '0': 0, + '1': 1, + '2': 2, + '3': 3, + '4': 4, + '5': 5, + '6': 6, + '7': 7, + '8': 8, + '9': 9, + 'a': 10, + 'b': 11, + 'c': 12, + 'd': 13, + 'e': 14, + 'f': 15, + } + + var hexRepresentations = []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'} + + contentBytes := []byte(content) + passwordBytes := []byte(password) + + buffer := make([]byte, len(contentBytes), len(contentBytes)) + + for i := 0; i < len(contentBytes); i++ { + contentByte := hexValues[contentBytes[i]] + passwordByte := hexValues[passwordBytes[i]] + + buffer[i] = hexRepresentations[contentByte^passwordByte] + } + + return string(buffer) +} diff --git a/test/set_1_test.go b/test/set_1_test.go index 7b5d312..010aed7 100644 --- a/test/set_1_test.go +++ b/test/set_1_test.go @@ -17,3 +17,16 @@ func Test_Challenge1(t *testing.T) { t.Fatal("Set 1, Challenge 1 has failed") } } + +func Test_Challenge2(t *testing.T) { + const expected = "746865206b696420646f6e277420706c6179" + + const content = "1c0111001f010100061a024b53535009181c" + const password = "686974207468652062756c6c277320657965" + + value := set1.XORHexString(content, password) + + if value != expected { + t.Fatal(("Set 1, Challenge 2 has failed")) + } +}