Skip to content

Commit a8f855d

Browse files
authored
Refactor xor package (#264)
* refactor the code * update xor_cipher_test
1 parent 219d8a1 commit a8f855d

File tree

2 files changed

+54
-47
lines changed

2 files changed

+54
-47
lines changed

ciphers/xor/xorCipher.go

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,34 @@
1-
package main
1+
// Package xor is an encryption algorithm that operates the exclusive disjunction(XOR)
2+
// ref: https://en.wikipedia.org/wiki/XOR_cipher
3+
package xor
24

3-
import "fmt"
4-
5-
func encrypt(key int, plaintext []int) (ciphertext []int) {
6-
for _, char := range plaintext {
7-
ciphertext = append(ciphertext, xor(char, key))
8-
}
9-
return
10-
}
11-
func xor(char int, key int) int {
12-
return (char ^ key)
5+
// Xor is struct for xor package
6+
type Xor struct {
137
}
148

15-
func decrypt(key int, ciphertext []int) (plaintext []int) {
16-
for _, char := range ciphertext {
17-
plaintext = append(plaintext, xor(char, key))
18-
}
19-
return
9+
// NewXor returns a pointer to object of Xor.
10+
func NewXor() *Xor {
11+
return &Xor{}
2012
}
2113

22-
func decodeToString(slice []int) (str string) {
23-
for _, v := range slice {
24-
str += string(v)
14+
// Encrypt encrypts with Xor encryption after converting each character to byte
15+
// The returned value might not be readable because there is no guarantee
16+
// which is within the ASCII range
17+
// If using other type such as string, []int, or some other types,
18+
// add the statements for converting the type to []byte.
19+
func (x Xor) Encrypt(key byte, plaintext []byte) []byte {
20+
cipherText := []byte{}
21+
for _, ch := range plaintext {
22+
cipherText = append(cipherText, key^ch)
2523
}
26-
return
24+
return cipherText
2725
}
28-
func toASCII(slice []rune) []int {
29-
var converted []int
30-
for _, v := range slice {
31-
converted = append(converted, int(v))
26+
27+
// Decrypt decrypts with Xor encryption
28+
func (x Xor) Decrypt(key byte, cipherText []byte) []byte {
29+
plainText := []byte{}
30+
for _, ch := range cipherText {
31+
plainText = append(plainText, key^ch)
3232
}
33-
return converted
34-
}
35-
func main() {
36-
str := "hello world"
37-
key := 97
38-
temp := []rune(str)
39-
message := toASCII(temp)
40-
encrypted := encrypt(key, message)
41-
decrypted := decrypt(key, encrypted)
42-
plaintext := decodeToString(decrypted)
43-
fmt.Println(plaintext)
33+
return plainText
4434
}

ciphers/xor/xor_cipher_test.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1-
package main
1+
package xor_test
22

33
import (
4+
"TheAlgorithms/Go/ciphers/xor"
5+
"fmt"
46
"reflect"
57
"testing"
68
)
79

10+
func ExampleNewXor() {
11+
const (
12+
seed = "Hello World"
13+
key = 97
14+
)
15+
16+
x := xor.NewXor()
17+
encrypted := x.Encrypt(byte(key), []byte(seed))
18+
fmt.Printf("Encrypt=> key: %d, seed: %s, encryptedText: %v\n", key, seed, encrypted)
19+
20+
decrypted := x.Decrypt(byte(key), encrypted)
21+
fmt.Printf("Decrypt=> key: %d, encryptedText: %v, DecryptedText: %s\n", key, encrypted, string(decrypted))
22+
23+
// Output:
24+
// Encrypt=> key: 97, seed: Hello World, encryptedText: [41 4 13 13 14 65 54 14 19 13 5]
25+
// Decrypt=> key: 97, encryptedText: [41 4 13 13 14 65 54 14 19 13 5], DecryptedText: Hello World
26+
}
27+
828
var xorTestData = []struct {
929
description string
1030
input string
@@ -61,16 +81,15 @@ var xorTestData = []struct {
6181
},
6282
}
6383

84+
var x *xor.Xor = xor.NewXor()
85+
6486
func TestXorCipherEncrypt(t *testing.T) {
6587
for _, test := range xorTestData {
6688
t.Run(test.description, func(t *testing.T) {
67-
68-
message := toASCII([]rune(test.input))
69-
encrypted := encrypt(test.key, message)
70-
71-
if actual := decodeToString(encrypted); !reflect.DeepEqual(actual, test.encrypted) {
89+
encrypted := x.Encrypt(byte(test.key), []byte(test.input))
90+
if !reflect.DeepEqual(string(encrypted), test.encrypted) {
7291
t.Logf("FAIL: %s", test.description)
73-
t.Fatalf("Expecting %s, actual %s", test.encrypted, actual)
92+
t.Fatalf("Expecting %s, actual %s", test.encrypted, string(encrypted))
7493
}
7594
})
7695
}
@@ -79,13 +98,11 @@ func TestXorCipherEncrypt(t *testing.T) {
7998
func TestXorCipherDecrypt(t *testing.T) {
8099
for _, test := range xorTestData {
81100
t.Run(test.description, func(t *testing.T) {
101+
decrypted := x.Decrypt(byte(test.key), []byte(test.encrypted))
82102

83-
message := toASCII([]rune(test.encrypted))
84-
decrypted := decrypt(test.key, message)
85-
86-
if actual := decodeToString(decrypted); !reflect.DeepEqual(actual, test.input) {
103+
if !reflect.DeepEqual(string(decrypted), test.input) {
87104
t.Logf("FAIL: %s", test.description)
88-
t.Fatalf("Expecting %s, actual %s", test.input, actual)
105+
t.Fatalf("Expecting %s, actual %s", test.input, string(decrypted))
89106
}
90107
})
91108
}

0 commit comments

Comments
 (0)