-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa.go
73 lines (61 loc) · 1.64 KB
/
rsa.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
package jwt
import (
"crypto"
"crypto/rand"
"crypto/rsa"
_ "crypto/sha512"
"fmt"
)
type SigningMethodRSA struct {
Name string
Hash crypto.Hash // 假設你用crypto.SHA512,那麼import必須要包含"crypto/sha512",否則會報錯
}
var (
SigningMethodRSA256 *SigningMethodRSA
SigningMethodRSA384 *SigningMethodRSA
SigningMethodRSA512 *SigningMethodRSA
)
func init() {
SigningMethodRSA256 = &SigningMethodRSA{"RS256", crypto.SHA256}
SigningMethodRSA384 = &SigningMethodRSA{"RS384", crypto.SHA384}
SigningMethodRSA512 = &SigningMethodRSA{"RS512", crypto.SHA512}
}
func (m *SigningMethodRSA) AlgName() string {
return m.Name
}
func (m *SigningMethodRSA) Sign(signingBytes []byte, key any) ([]byte, error) {
privateKey, ok := key.(*rsa.PrivateKey)
if !ok {
return nil, fmt.Errorf("RSA sign expects *rsa.PrivateKey. %w", ErrInvalidKeyType)
}
if !m.Hash.Available() {
return nil, ErrHashUnavailable
}
hasher := m.Hash.New()
hasher.Write(signingBytes)
return rsa.SignPKCS1v15(rand.Reader, privateKey, m.Hash, hasher.Sum(nil))
}
func (m *SigningMethodRSA) Verify(
signingBytes []byte,
signature []byte,
key any,
) (err error) {
publicKey, ok := key.(*rsa.PublicKey)
if !ok {
return fmt.Errorf("RSA verify expects *rsa.PublicKey. %w", ErrInvalidKeyType)
}
if !m.Hash.Available() {
return ErrHashUnavailable
}
// 加簽本次的內容
hasher := m.Hash.New()
hasher.Write(signingBytes)
if err = rsa.VerifyPKCS1v15(
publicKey,
m.Hash, hasher.Sum(nil),
signature, // 計算出來的雜湊值+公鑰+之前的簽名,可以知道是否同源
); err != nil {
return fmt.Errorf("%w %w", err, ErrSignatureInvalid)
}
return nil
}