-
Notifications
You must be signed in to change notification settings - Fork 0
/
c2.go
31 lines (26 loc) · 919 Bytes
/
c2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main
import (
"encoding/hex"
"fmt"
)
// xorHexStrings performs a bitwise XOR operation between two input hexadecimal
// strings of equal length and returns the result as a hexadecimal string.
func xorHexStrings(s1, s2 string) (string, error) {
// We decode the hex strings to bytes before checking their length, because
// the byte length might be different from the hex string length due to how
// hexadecimal encoding works, and direct string length comparison might
// not always give you the correct assessment.
b1, err := hex.DecodeString(s1)
if err != nil {
return "", fmt.Errorf("malformed input hex string: %x", s1)
}
b2, err := hex.DecodeString(s2)
if err != nil {
return "", fmt.Errorf("malformed input hex string: %x", s2)
}
xored, err := xorBlocks(b1, b2)
if err != nil {
return "", fmt.Errorf("can't xor given strings: %s", err)
}
return hex.EncodeToString(xored), nil
}