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:
parent
ee46f563d3
commit
ab8e0cc3f0
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)
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user