/
sign.go
53 lines (48 loc) · 1 KB
/
sign.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
package opensdk
import (
"crypto"
"crypto/hmac"
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
)
func Sha256Hmac(s string, k []byte) (string, error) {
h := hmac.New(sha256.New, k)
_, err := io.WriteString(h, s)
if err != nil {
return "", err
}
return fmt.Sprintf("%X", h.Sum(nil)), nil
}
func Sha256RSA(origData string, key *rsa.PrivateKey) (string, error) {
hash := sha256.New()
hash.Write([]byte(origData))
digest := hash.Sum(nil)
s, err := rsa.SignPKCS1v15(rand.Reader, key, crypto.SHA256, digest)
if err != nil {
return "", err
}
data := base64.StdEncoding.EncodeToString(s)
return string(data), nil
}
func Sha1(s string) (string, error) {
h := sha1.New()
_, err := io.WriteString(h, s)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
func MD5(s string) (string, error) {
h := md5.New()
_, err := io.WriteString(h, s)
if err != nil {
return "", err
}
return fmt.Sprintf("%X", h.Sum(nil)), nil
}