-
Notifications
You must be signed in to change notification settings - Fork 17
/
utils.go
31 lines (28 loc) · 840 Bytes
/
utils.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
package utils
import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
)
// GenerateCodeVerifier
//
// @Description: 生成校验code
// @return string
func GenerateCodeVerifier() string {
// 随机生成一个长度为 32 的 code_verifier
token := make([]byte, 32)
rand.Read(token)
codeVerifier := base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(token)
return codeVerifier
}
// GenerateCodeChallenge
//
// @Description: 校验code challenge
// @param codeVerifier
// @return string
func GenerateCodeChallenge(codeVerifier string) string {
// 对 code_verifier 进行哈希处理,然后再进行 base64url 编码,生成 code_challenge
sha256Hash := sha256.Sum256([]byte(codeVerifier))
codeChallenge := base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(sha256Hash[:])
return codeChallenge
}