Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package crypto
import (
"crypto/rand"
"testing"
"github.com/stretchr/testify/assert"
)
// TestCrypto_OTP tests if a byte array is XOR'ed the proper way.
func TestCrypto_OTP_XOR(t *testing.T) {
testByteArray := []byte{0xF}
testByteArray2 := []byte{0xF0}
resultByteArray := make([]byte, len(testByteArray))
for i := range testByteArray {
resultByteArray[i] = testByteArray[i] ^ testByteArray2[i]
}
assert.Equal(t, resultByteArray, []byte{0xFF})
}
func TestCrypto_OTP_EncryptAndDecryptPlaintext(t *testing.T) {
secret := []byte("this is a secret")
key := make([]byte, len(secret))
_, err := rand.Read(key)
assert.NoError(t, err)
otp := NewOTP("OTP")
// encrypt the secret with encrypt method
_, encryptedSecret, err := otp.Encrypt(secret, key)
assert.NoError(t, err)
// decrypt the encryptedSecret with decrypt method
decryptedSecret, err := otp.Decrypt(nil, encryptedSecret, key)
assert.NoError(t, err)
assert.Equal(t, secret, decryptedSecret)
}