Skip to content

Commit

Permalink
Use crypto/rand for pkce and state generation
Browse files Browse the repository at this point in the history
math/rand is only a pseudo-random number generator, use crypto/rand
instead for the secret generator
  • Loading branch information
thschmitt committed Jan 12, 2023
1 parent bdc5e19 commit d6d1529
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions auth/secret_generator.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package auth

import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"fmt"
"math/rand"
"strings"
"time"
)

type SecretGenerator struct{}
Expand All @@ -33,8 +32,10 @@ func (g SecretGenerator) base64Encode(value []byte) string {
}

func (g SecretGenerator) randomString(length int) string {
rand.Seed(time.Now().UnixNano())
b := make([]byte, length)
rand.Read(b)
_, err := rand.Read(b)
if err != nil {
panic(fmt.Errorf("Could not get cryptographically secure random numbers: %v", err))
}
return fmt.Sprintf("%x", b)[:length]
}

0 comments on commit d6d1529

Please sign in to comment.