1
- package main
1
+ package xor_test
2
2
3
3
import (
4
+ "TheAlgorithms/Go/ciphers/xor"
5
+ "fmt"
4
6
"reflect"
5
7
"testing"
6
8
)
7
9
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
+
8
28
var xorTestData = []struct {
9
29
description string
10
30
input string
@@ -61,16 +81,15 @@ var xorTestData = []struct {
61
81
},
62
82
}
63
83
84
+ var x * xor.Xor = xor .NewXor ()
85
+
64
86
func TestXorCipherEncrypt (t * testing.T ) {
65
87
for _ , test := range xorTestData {
66
88
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 ) {
72
91
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 ) )
74
93
}
75
94
})
76
95
}
@@ -79,13 +98,11 @@ func TestXorCipherEncrypt(t *testing.T) {
79
98
func TestXorCipherDecrypt (t * testing.T ) {
80
99
for _ , test := range xorTestData {
81
100
t .Run (test .description , func (t * testing.T ) {
101
+ decrypted := x .Decrypt (byte (test .key ), []byte (test .encrypted ))
82
102
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 ) {
87
104
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 ) )
89
106
}
90
107
})
91
108
}
0 commit comments