-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm_test.go
More file actions
77 lines (58 loc) · 1.57 KB
/
Copy pathalgorithm_test.go
File metadata and controls
77 lines (58 loc) · 1.57 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package rtu_test
import (
"bytes"
"crypto/rand"
"crypto/sha256"
"testing"
rtu "github.com/MyNextID/ioss-rtu-go-sdk"
"github.com/MyNextID/ioss-rtu-go-sdk/internal/helpers"
)
// =====================================================
// AlgorithmEcdsaP256 test-suite
// =====================================================
func TestAlgorithmEcdsaP256_Digest(t *testing.T) {
t.Parallel()
payload := []byte{0}
hash := sha256.Sum256(payload)
expectedResult := hash[:]
result := rtu.AlgorithmEcdsaP256.Digest(payload)
if !bytes.Equal(result, expectedResult) {
t.Fatalf("Digest() result does not match expected result")
}
}
func TestAlgorithmEcdsaP256_VerifyValidSignature(t *testing.T) {
t.Parallel()
priv, err := rtu.NewECPrivateKey(helpers.GenerateKey(t))
if err != nil {
t.Fatal(err)
}
payload := []byte{0}
rtuType := helpers.GetASN1Type()
signature, err := priv.Sign(rtuType, rand.Reader, payload)
if err != nil {
t.Fatal(err)
}
err = priv.Public().Verify(rtuType, payload, signature)
if err != nil {
t.Errorf("Verify() unexepected error, got %v", err)
}
}
func TestAlgorithmEcdsaP256_VerifyInvalidSignature(t *testing.T) {
t.Parallel()
priv, err := rtu.NewECPrivateKey(helpers.GenerateKey(t))
if err != nil {
t.Fatal(err)
}
payload := []byte{0}
rtuType := helpers.GetASN1Type()
signature, err := priv.Sign(rtuType, rand.Reader, payload)
if err != nil {
t.Fatal(err)
}
// tamper with signature
signature[len(signature)/2] ^= 0xFF
err = priv.Public().Verify(rtuType, payload, signature)
if err == nil {
t.Errorf("Verify() expected error")
}
}