TBD54566975 / web5-go

Apache License 2.0
10 stars 6 forks source link

Add nonce generation to crypto package #51

Closed KendallWeihe closed 6 months ago

KendallWeihe commented 6 months ago

Add the following functionality to the crypto package

// GenerateEntropy creates a string of random characters the specified length in bytes.
func GenerateEntropy(n int) (string, error) {
    // Allocate a slice for the random bytes.
    b := make([]byte, n)
    // Read random bytes into the slice.
    _, err := rand.Read(b)
    // Check for errors from the random number generator.
    if err != nil {
        return "", err
    }
    // Return the hexadecimal encoding of the random bytes.
    return hex.EncodeToString(b), nil
}
KendallWeihe commented 6 months ago

Actually, probably we should not prescribe the hexadecimal encoding in the return type, and instead just return the byte array. Then, we could add a second function GenerateNonce() which calls GenerateEntropy and does prescribe a hex encoding.