-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature.go
45 lines (37 loc) · 936 Bytes
/
signature.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
package do
import (
"crypto"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
)
type Signer struct {
secret string // base64 encoded
}
// NewSigner which secret is base64 encoded
//
// secret can use `ssh-keygen -t rsa -b 2048 -m PKCS8 -f jwtRS256.key` to generate, it will generate `jwtRS256.key` and `jwtRS256.key.pub`, and the first file is what we want
func NewSigner(secret string) *Signer {
return &Signer{
secret: secret,
}
}
// Sign return raw's signature which is base64 encoded
func (s *Signer) Sign(raw []byte) (r string, err error) {
secret, err := base64.StdEncoding.DecodeString(s.secret)
if err != nil {
return
}
key, err := x509.ParsePKCS8PrivateKey(secret)
if err != nil {
return
}
hashed := sha256.Sum256(raw)
enc, err := rsa.SignPKCS1v15(nil, key.(*rsa.PrivateKey), crypto.SHA256, hashed[:])
if err != nil {
return
}
r = base64.StdEncoding.EncodeToString(enc)
return
}