Skip to content

Commit

Permalink
gen secure random num & hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopertz committed Sep 1, 2024
1 parent 5104b0b commit fa6c62a
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions internal/passcode/passcode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package passcode

import (
"crypto/rand"
"crypto/sha256"
"math/big"
mrand "math/rand"
"strconv"
)

func GenSecureRandomNumber() int {

max := new(big.Int).SetInt64(900000)
offset := new(big.Int).SetInt64(100000)

num, err := rand.Int(rand.Reader, max)
if err == nil {
passcode := num.Add(num, offset).Int64()
return int(passcode)
} else {
fallbackPasscode := mrand.Intn(900000) + 100000
return fallbackPasscode
}

}

func HashPasscode() (int, [32]byte) {

passcode := GenSecureRandomNumber()
numStr := strconv.Itoa(passcode)
data := []byte(numStr)

return passcode, sha256.Sum256(data)

}

0 comments on commit fa6c62a

Please sign in to comment.