1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-03 10:17:03 +00:00

implement the Iris Crypto Library for Request Authentication and Verification. With Examples and Tests.

Relative to this one as well: https://github.com/kataras/iris/issues/1200


Former-commit-id: 3a29e7398b7fdeb9b48a118b742d419d5681d56b
This commit is contained in:
Gerasimos (Makis) Maropoulos
2019-07-02 19:19:43 +03:00
parent 35389c6ef8
commit 9dbb300d9b
12 changed files with 992 additions and 2 deletions

46
crypto/gcm/gcm_test.go Normal file
View File

@@ -0,0 +1,46 @@
package gcm
import (
"bytes"
"testing"
)
var testKey = MustGenerateKey()
func TestEncryptDecrypt(t *testing.T) {
if len(testKey) == 0 {
t.Fatalf("testKey is empty??")
}
tests := []struct {
payload []byte
aData []byte // IV of a random aes-256-cbc, 32 size.
}{
{[]byte("test my content 1"), []byte("FFA0A43EA6B8C829AD403817B2F5B7A2")},
{[]byte("test my content 2"), []byte("364787B9AF1AEE4BE26690EA8CBF4AB7")},
}
for i, tt := range tests {
ciphertext, err := Encrypt(testKey, tt.payload, tt.aData)
if err != nil {
t.Fatalf("[%d] encrypt error: %v", i, err)
}
payload, err := Decrypt(testKey, ciphertext, tt.aData)
if err != nil {
t.Fatalf("[%d] decrypt error: %v", i, err)
}
if !bytes.Equal(payload, tt.payload) {
t.Fatalf("[%d] expected data to be decrypted to: '%s' but got: '%s'", i, tt.payload, payload)
}
// test with other, invalid key, should fail to decrypt.
tempKey := MustGenerateKey()
payload, err = Decrypt(tempKey, ciphertext, tt.aData)
if err == nil || len(payload) > 0 {
t.Fatalf("[%d] verification should fail but passed for '%s'", i, tt.payload)
}
}
}