v / vlib / crypto / rand / rand.v
24 lines · 20 sloc · 780 bytes · 3aed78e76ab5a0764bfb4bff28b8ab6eb540c236
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4
5module rand
6
7struct ReadError {
8 Error
9}
10
11// msg returns the error message.
12pub fn (err ReadError) msg() string {
13 return 'crypto.rand.read() error reading random bytes'
14}
15
16// bytes returns an array of `bytes_needed` random bytes.
17// Note: this call can block your program for a long period of time,
18// if your system does not have access to enough entropy.
19// See also rand.bytes(), if you do not need really random bytes,
20// but instead pseudo random ones, from a pseudo random generator
21// that can be seeded, and that is usually faster.
22pub fn bytes(bytes_needed int) ![]u8 {
23 return read(bytes_needed)
24}
25