Added Go implementation

This commit is contained in:
KyleBanks 2016-07-09 06:34:05 -04:00
parent ee46f563d3
commit ab8e0cc3f0
3 changed files with 34 additions and 0 deletions

11
Go/xor.go Normal file
View 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
View 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)
}
}

View File

@ -8,6 +8,7 @@ Simple implementation of XOR Encryption/Decrypting in various languages, includi
- [C++](C++/main.cpp)
- [Dart](Dart/xorencryption.dart)
- [F#](F%23/Program.fs) by [pawelizycki](https://github.com/pawelizycki)
- [Go](Go/xo.go)
- [Groovy](Groovy/XOREncryption.groovy)
- [Java \(Android Compatible\)](Java \(Android compatible\)/XOREncryption.java)
- [JavaScript \(Node.js Compatible\)](JavaScript/XOREncryption.js)