mirror of
https://github.com/KyleBanks/XOREncryption.git
synced 2023-08-10 21:13:15 +03:00
Added Go implementation
This commit is contained in:
11
Go/xor.go
Normal file
11
Go/xor.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package xor
|
||||
|
||||
// EncryptDecrypt runs a XOR encryption on the input string, encrypting it if it hasn't already been,
|
||||
// and decrypting it if it has, using the key provided.
|
||||
func EncryptDecrypt(input, key string) (output string) {
|
||||
for i := range input {
|
||||
output += string(input[i] ^ key[i%len(key)])
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
22
Go/xor_test.go
Normal file
22
Go/xor_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package xor
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestEncryptDecrypt(t *testing.T) {
|
||||
sample := "kylewbanks.com"
|
||||
key := "KCQ"
|
||||
|
||||
// Test encrypting
|
||||
encrypted := EncryptDecrypt(sample, key)
|
||||
if encrypted == sample {
|
||||
t.Fatalf("Expected encrypted string not to match the input: %v", encrypted)
|
||||
} else if len(encrypted) != len(sample) {
|
||||
t.Fatalf("Expected encrypted string to have the same length as the input: %v", len(encrypted))
|
||||
}
|
||||
|
||||
// Test decrypting
|
||||
decrypted := EncryptDecrypt(encrypted, key)
|
||||
if decrypted != sample {
|
||||
t.Fatalf("Expected decrypted string to match the original input: %v", decrypted)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user