-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathsign_test.go
108 lines (102 loc) · 3.73 KB
/
sign_test.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2021 Tencent Inc. All rights reserved.
package utils
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"testing"
)
const (
testAlgorithmPrivateKeyStr = `-----BEGIN TESTING KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDZUJN33V+dSfvd
fL0Mu+39XrZNXFFMQSy1V15FpncHeV47SmV0TzTqZc7hHB0ddqAdDi8Z5k3TKqb7
6sOwYr5TcAfuR6PIPaleyE0/0KrljBum2Isa2Nyq7Dgc3ElBQ6YN4l/a+DpvKaz1
FSKmKrhLNskqokWVSlu4g8OlKlbPXQ9ibII14MZRQrrkTmHYHzfi7GXXM0thAKuR
0HNvyhTHBh4/lrYM3GaMvmWwkwvsMavnOex6+eioZHBOb1/EIZ/LzC6zuHArPpyW
3daGaZ1rtQB1vVzTyERAVVFsXXgBHvfFud3w3ShsJYk8JvMwK2RpJ5/gV0QSARcm
LDRUAlPzAgMBAAECggEBAMc7rDeUaXiWv6bMGbZ3BTXpg1FhdddnWUnYE8HfX/km
OFI7XtBHXcgYFpcjYz4D5787pcsk7ezPidAj58zqenuclmjKnUmT3pfbI5eCA2v4
C9HnbYDrmUPK1ZcADtka4D6ScDccpNYNa1g2TFHzkIrEa6H+q7S3O2fqxY/DRVtN
0JIXalBb8daaqL5QVzSmM2BMVnHy+YITJWIkP2a3pKs9C0W65JGDsnG0wVrHinHF
+cnhFZIbaPE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OO6Fsuml2JkFANJU9K6q9bLlDhPXuoV" +
"z+pp4hAm6pHU4ld815U4jsKu1RkyaII+1CYBUYC8TK0XtJ8FwUXXz8vZHh58rrAVN1XwNyvD1vfpxrMT4SL536GLwvpUHlCqIMzoZUguLl" +
"i/K8V29QiOhuH6IEqLNJn8e9b3nwNcQ7be3CzYGpDAKBfDGPCqCv8Rw5zndhlffk2FEA70G4hvMwe51qMN/RAJbknXG23bSlObuTCN7Ndj" +
"1aJGH6/L+hdwfLpUtJm4QYVazzW7DFD27EpSQEqA8bX9+8m1rLg=="
)
var testAlgorithmPrivateKey *rsa.PrivateKey
var ok bool
func init() {
block, _ := pem.Decode([]byte(testingKey(testAlgorithmPrivateKeyStr)))
if block == nil {
panic("decode private key err")
}
key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
panic(fmt.Errorf("parse private key err:%s", err.Error()))
}
testAlgorithmPrivateKey, ok = key.(*rsa.PrivateKey)
if !ok {
panic(fmt.Errorf("%s is not rsa private key", testingKey(testAlgorithmPrivateKeyStr)))
}
}
func TestSha256WithRsa(t *testing.T) {
type args struct {
source string
privateKey *rsa.PrivateKey
}
tests := []struct {
name string
args args
wantSignature string
wantErr bool
}{
{
name: "sign message in sha256withrsa success",
args: args{
source: "source",
privateKey: testAlgorithmPrivateKey,
},
wantSignature: testAlgorithmExpectSignature,
wantErr: false,
},
{
name: "sign message in sha256withrsa err",
args: args{
source: "source",
privateKey: nil,
},
wantErr: true,
wantSignature: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotSignature, err := SignSHA256WithRSA(tt.args.source, tt.args.privateKey)
if (err != nil) != tt.wantErr {
t.Errorf("SignSHA256WithRSA() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotSignature != tt.wantSignature {
t.Errorf("SignSHA256WithRSA() gotSignature = %v, want %v", gotSignature, tt.wantSignature)
}
})
}
}