From ab8e0cc3f02aa5fb3eadfee445b72aac3f4bea3b Mon Sep 17 00:00:00 2001 From: KyleBanks Date: Sat, 9 Jul 2016 06:34:05 -0400 Subject: [PATCH] Added Go implementation --- Go/xor.go | 11 +++++++++++ Go/xor_test.go | 22 ++++++++++++++++++++++ README.md | 1 + 3 files changed, 34 insertions(+) create mode 100644 Go/xor.go create mode 100644 Go/xor_test.go diff --git a/Go/xor.go b/Go/xor.go new file mode 100644 index 0000000..bf029c1 --- /dev/null +++ b/Go/xor.go @@ -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 +} diff --git a/Go/xor_test.go b/Go/xor_test.go new file mode 100644 index 0000000..aedfb79 --- /dev/null +++ b/Go/xor_test.go @@ -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) + } +} diff --git a/README.md b/README.md index 6b27ec8..9cd6043 100644 --- a/README.md +++ b/README.md @@ -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)