1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

crypt: implement crypto.rand.read/1 for OpenBSD and FreeBSD (#15437)

This commit is contained in:
John 2022-08-17 00:41:27 -04:00 committed by GitHub
parent 8736f26a1c
commit d41b2be3a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1,16 @@
// Copyright (c) 2022 John Lloyd. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module rand
#include <stdlib.h>
fn C.arc4random_buf(p &byte, n usize)
// read returns an array of `bytes_needed` random bytes read from the OS.
pub fn read(bytes_needed int) ?[]u8 {
mut buffer := unsafe { malloc_noscan(bytes_needed) }
C.arc4random_buf(buffer, bytes_needed)
return unsafe { buffer.vbytes(bytes_needed) }
}

View File

@ -0,0 +1,16 @@
// Copyright (c) 2022 John Lloyd. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module rand
#include <stdlib.h>
fn C.arc4random_buf(p &byte, n usize)
// read returns an array of `bytes_needed` random bytes read from the OS.
pub fn read(bytes_needed int) ?[]u8 {
mut buffer := unsafe { malloc_noscan(bytes_needed) }
C.arc4random_buf(buffer, bytes_needed)
return unsafe { buffer.vbytes(bytes_needed) }
}