mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
crypto: implement rand.bytes(needed_bytes int) ?[]byte
, use it consistently instead of the old rand.read(), which will change to be compatible with io and the pseudo random rand
module
This commit is contained in:
parent
80444c8ec4
commit
ff34b79d39
@ -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
|
||||
|
@ -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()
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user