-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature_rsa.go
56 lines (43 loc) · 1.08 KB
/
signature_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
package jwt
import (
"crypto"
"crypto/rand"
"crypto/rsa"
)
type signatureRSA struct {
Name string
Hash crypto.Hash
}
func (sg *signatureRSA) Algorithm() string {
return sg.Name
}
func (sg *signatureRSA) Verify(headerAndClaims []byte, signature []byte, key interface{}) error {
var rsaKey *rsa.PublicKey
var ok bool
if rsaKey, ok = key.(*rsa.PublicKey); !ok {
return ErrorInvalidAuthorization
}
if !sg.Hash.Available() {
return ErrorInvalidAuthorization
}
hasher := sg.Hash.New()
hasher.Write(headerAndClaims)
return rsa.VerifyPKCS1v15(rsaKey, sg.Hash, hasher.Sum(nil), signature)
}
func (sg *signatureRSA) Signature(headerAndClaims []byte, key interface{}) ([]byte, error) {
var rsaKey *rsa.PrivateKey
var ok bool
if rsaKey, ok = key.(*rsa.PrivateKey); !ok {
return nil, ErrorInvalidAuthorization
}
if !sg.Hash.Available() {
return nil, ErrorInvalidAuthorization
}
hasher := sg.Hash.New()
hasher.Write(headerAndClaims)
sigBytes, err := rsa.SignPKCS1v15(rand.Reader, rsaKey, sg.Hash, hasher.Sum(nil))
if err != nil {
return nil, err
}
return sigBytes, nil
}