-
Notifications
You must be signed in to change notification settings - Fork 1
/
xor.go
39 lines (30 loc) · 984 Bytes
/
xor.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
32
33
34
35
36
37
38
39
package obfuscate
import (
"errors"
)
// xor abstracts XOR cryption. Accepts input and key values, Returns output and an error value.
func xor(input []byte, key []byte) (output []byte, err error) {
if len(key) == 0 {
return input, errors.New("Key length of zero means no encryption")
}
output = []byte("")
for i := 0; i < len(input); i++ {
output = append(output, input[i]^key[i%len(key)])
}
return output, err
}
// XOR structure maintains a key value and satisfies Obfuscator interface.
type XOR struct {
Key string `json:"key"`
}
func (x *XOR) ByteKey() []byte {
return []byte(x.Key)
}
// Obfuscate XOR encrypts input bytes. Returns encrypted output and an error value.
func (x *XOR) Obfuscate(input []byte) (output []byte, err error) {
return xor(input, x.ByteKey())
}
// Deobfuscate XOR decrypts input bytes. Returns decrypted output and an error value.
func (x *XOR) Deobfuscate(input []byte) (output []byte, err error) {
return xor(input, x.ByteKey())
}