cryptopals/set1/challenge_2.go
2024-10-29 20:36:04 +02:00

38 lines
669 B
Go

package set1
const HexRepresentations = "0123456789abcdef"
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,
}
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)
}