-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsa_signer.go
50 lines (43 loc) · 964 Bytes
/
rsa_signer.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
package pasargad
/**
* Thanks to Github User @Aaron0 for go-rsa-sign repository that inspired us:
* @URL: https://github.com/AaronO/go-rsa-sign
*/
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"encoding/hex"
)
type Signer struct {
Key *rsa.PrivateKey
}
func NewSigner(pemKey []byte) (*Signer, error) {
key, err := parsePrivateKey(pemKey)
if err != nil {
return nil, err
}
return &Signer{key}, nil
}
func (s *Signer) Sign(data []byte) ([]byte, error) {
hash := crypto.SHA1
h := hash.New()
h.Write(data)
hashed := h.Sum(nil)
return rsa.SignPKCS1v15(rand.Reader, s.Key, hash, hashed)
}
func (s *Signer) SignHex(data []byte) (string, error) {
sig, err := s.Sign(data)
if err != nil {
return "", err
}
return hex.EncodeToString(sig), nil
}
func (s *Signer) SignBase64(data []byte) (string, error) {
sig, err := s.Sign(data)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(sig), nil
}