Set 1, Challenge 2

This commit is contained in:
Miroslav Vasilev 2024-10-28 17:54:51 +02:00
parent 635e727741
commit 4b789cf646
2 changed files with 51 additions and 0 deletions

38
set1/challenge_2.go Normal file
View file

@ -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)
}

View file

@ -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"))
}
}