@@ -2,10 +2,12 @@ package encryption
4 "crypto/hmac"
4 "crypto/hmac"
5 "crypto/sha256"
6 "crypto/sha256"
6 "encoding/base64"
7 "encoding/base64"
9 "net/http"
11 "net/http"
@@ -15,6 +17,7 @@ import (
16 CodeChallengeMethodPlain = "plain"
18 CodeChallengeMethodPlain = "plain"
17 CodeChallengeMethodS256 = "S256"
19 CodeChallengeMethodS256 = "S256"
20+ asciiCharset = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
20// SecretBytes attempts to base64 decode the secret, if that fails it treats the secret as binary
23// SecretBytes attempts to base64 decode the secret, if that fails it treats the secret as binary
@@ -80,6 +83,19 @@ func SignedValue(seed string, key string, value []byte, now time.Time) (string,
80 return cookieVal, nil
83 return cookieVal, nil
86+func GenerateRandomASCIIString(length int) (string, error) {
87+ b := make([]byte, length)
88+ charsetLen := new(big.Int).SetInt64(int64(len(asciiCharset)))
90+ character, err := rand.Int(rand.Reader, charsetLen)
94+ b[i] = asciiCharset[character.Int64()]
96+ return string(b), nil
83func GenerateCodeChallenge(method, codeVerifier string) (string, error) {
99func GenerateCodeChallenge(method, codeVerifier string) (string, error) {
84 switch method {
100 switch method {
85 case CodeChallengeMethodPlain:
101 case CodeChallengeMethodPlain: