-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigner.go
80 lines (74 loc) · 1.96 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package oauth
import (
"bytes"
"crypto/cipher"
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"encoding/hex"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"strings"
)
func padding(src []byte,blocksize int) []byte {
padnum:=blocksize-len(src)%blocksize
pad:=bytes.Repeat([]byte{byte(padnum)},padnum)
return append(src,pad...)
}
func unpadding(src []byte) []byte {
n:=len(src)
unpadnum:=int(src[n-1])
return src[:n-unpadnum]
}
func (s *UserPassOAuthServer) AESEncrypt(src string) string {
code := []byte(src)
code = padding(code, s.aesCipher.BlockSize())
aesEncrypt := cipher.NewCBCEncrypter(s.aesCipher, s.aesKey)
aesEncrypt.CryptBlocks(code, code)
return hex.EncodeToString(code)
}
func (s *UserPassOAuthServer) AESDecrypt(src string) string {
code, err := hex.DecodeString(src)
if err != nil {
return ""
}
aesDecrypt := cipher.NewCBCDecrypter(s.aesCipher, s.aesKey)
aesDecrypt.CryptBlocks(code, code)
res := unpadding(code)
return string(res)
}
func (s *UserPassOAuthServer) RS256Sign(content string) (string, error) {
if !s.Hash.Available() {
return "", status.Error(codes.Internal, "hash unavailable")
}
hasher := s.Hash.New()
hasher.Write([]byte(content))
if sigBytes, err := rsa.SignPSS(rand.Reader, s.privKey, s.Hash, hasher.Sum(nil), nil); err == nil {
return EncodeSegment(sigBytes), nil
} else {
return "", err
}
}
func (s *UserPassOAuthServer) RS256Verify(content, sig string) bool {
hasher := s.Hash.New()
s.Hash.New()
hasher.Write([]byte(content))
rawSig, err := DecodeSegment(sig)
if err != nil {
return false
}
if err := rsa.VerifyPSS(s.pubKey, s.Hash, hasher.Sum(nil), rawSig, nil); err == nil {
return true
} else {
return false
}
}
func EncodeSegment(seg []byte) string {
return strings.TrimRight(base64.URLEncoding.EncodeToString(seg), "=")
}
func DecodeSegment(seg string) ([]byte, error) {
if l := len(seg) % 4; l > 0 {
seg += strings.Repeat("=", 4-l)
}
return base64.URLEncoding.DecodeString(seg)
}