ADDED: crypto_n_random_bytes/2, creating cryptographically secure random bytes

The ring crate is used since it will be needed also for future
predicates in library(crypto).
This commit is contained in:
Markus Triska
2020-05-13 00:22:17 +02:00
parent c70b873397
commit 716fde5784
4 changed files with 34 additions and 1 deletions

View File

@@ -12,7 +12,8 @@
using strings leaves little trace of what was processed in the system,
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
:- module(crypto, [hex_bytes/2]).
:- module(crypto, [hex_bytes/2,
crypto_n_random_bytes/2]).
:- use_module(library(error)).
:- use_module(library(lists)).
@@ -70,3 +71,10 @@ bytes_hex([B|Bs]) --> [C0,C1],
char_hexval(C, H) :- nth0(H, "0123456789abcdef", C), !.
char_hexval(C, H) :- nth0(H, "0123456789ABCDEF", C), !.
crypto_n_random_bytes(N, Bs) :-
must_be(integer, N),
length(Bs, N),
maplist(crypto_random_byte, Bs).
crypto_random_byte(B) :- '$crypto_random_byte'(B).