diff --git a/vlib/crypto/README.md b/vlib/crypto/README.md index 09a30e586c..e5835019fe 100644 --- a/vlib/crypto/README.md +++ b/vlib/crypto/README.md @@ -21,7 +21,7 @@ import crypto.rand fn main() { // remember to save this key somewhere if you ever want to decrypt your data - key := rand.read(32) ? + key := rand.bytes(32) ? println('KEY: $key') // this data is one block (16 bytes) big diff --git a/vlib/crypto/bcrypt/bcrypt.v b/vlib/crypto/bcrypt/bcrypt.v index 04f047f550..4a8320d89f 100644 --- a/vlib/crypto/bcrypt/bcrypt.v +++ b/vlib/crypto/bcrypt/bcrypt.v @@ -59,7 +59,7 @@ pub fn compare_hash_and_password(password []byte, hashed_password []byte) ? { // generate_salt generate a string to be treated as a salt. pub fn generate_salt() string { - randbytes := rand.read(bcrypt.salt_length) or { panic(err) } + randbytes := rand.bytes(bcrypt.salt_length) or { panic(err) } return randbytes.bytestr() } diff --git a/vlib/crypto/rand/crypto_rand_read_test.v b/vlib/crypto/rand/crypto_rand_bytes_test.v similarity index 100% rename from vlib/crypto/rand/crypto_rand_read_test.v rename to vlib/crypto/rand/crypto_rand_bytes_test.v diff --git a/vlib/crypto/rand/rand.v b/vlib/crypto/rand/rand.v index b21e96923c..94d40f35b0 100644 --- a/vlib/crypto/rand/rand.v +++ b/vlib/crypto/rand/rand.v @@ -11,3 +11,13 @@ struct ReadError { pub fn (err ReadError) msg() string { return 'crypto.rand.read() error reading random bytes' } + +// bytes returns an array of `bytes_needed` random bytes. +// NB: this call can block your program for a long period of time, +// if your system does not have access to enough entropy. +// See also rand.bytes(), if you do not need really random bytes, +// but instead pseudo random ones, from a pseudo random generator +// that can be seeded, and that is usually faster. +pub fn bytes(bytes_needed int) ?[]byte { + return read(bytes_needed) +}