Cryptographically generate password characters

This commit is contained in:
Maximilian 2023-01-29 23:30:21 -06:00
parent 710f96e9e7
commit e96fded8ff

View File

@ -7,6 +7,7 @@ import (
cryptorand "crypto/rand" cryptorand "crypto/rand"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"math/big"
mathrand "math/rand" mathrand "math/rand"
"os" "os"
"strconv" "strconv"
@ -58,9 +59,15 @@ func main() {
mathrand.Seed(int64(binary.LittleEndian.Uint64(b[:]))) mathrand.Seed(int64(binary.LittleEndian.Uint64(b[:])))
// Assign every slot of pass to a random allowedCharacter // Assign every slot of pass to a random allowedCharacter
for i := range pass { for i := range pass {
// Generate a random int greater than 0 and not to exceed the length of allowedCharacters // Generate a random int greater than 0 and not to exceed the length of allowedCharacters
pass[i] = allowedCharacters[mathrand.Intn(len(allowedCharacters))] index, err := cryptorand.Int(cryptorand.Reader, big.NewInt(int64(len(allowedCharacters))))
if err != nil {
println("Error securely generating random character!")
return
}
pass[i] = allowedCharacters[index.Int64()]
} }
// Print the password // Print the password