-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigner_rsa.go
39 lines (30 loc) · 979 Bytes
/
signer_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
package jwt
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"fmt"
)
func RS256Signer(kid string, privateKey *rsa.PrivateKey) Signer {
return rsaSigner(kid, crypto.SHA256, privateKey)
}
func RS384Signer(kid string, privateKey *rsa.PrivateKey) Signer {
return rsaSigner(kid, crypto.SHA384, privateKey)
}
func RS512Signer(kid string, privateKey *rsa.PrivateKey) Signer {
return rsaSigner(kid, crypto.SHA512, privateKey)
}
func rsaSigner(kid string, hash crypto.Hash, privateKey *rsa.PrivateKey) Signer {
return signerFunc(fmt.Sprintf("RS%d", hash.Size()<<3), kid, func(data []byte) ([]byte, error) {
return signRSA(data, privateKey, hash)
})
}
func signRSA(data []byte, privateKey *rsa.PrivateKey, hash crypto.Hash) ([]byte, error) {
// Compute the hash of the input data
hasher := hash.New()
hasher.Reset()
hasher.Write(data)
digest := hasher.Sum(nil)
// Sign the digest with the private key
return rsa.SignPKCS1v15(rand.Reader, privateKey, hash, digest)
}