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

@@ -38,6 +38,8 @@ use cpu_time::ProcessTime;
use crate::crossterm::event::{read, Event, KeyCode, KeyEvent, KeyModifiers};
use crate::crossterm::terminal::{enable_raw_mode, disable_raw_mode};
use ring::rand::{SecureRandom, SystemRandom};
pub fn get_key() -> KeyEvent {
let key;
enable_raw_mode().expect("failed to enable raw mode");
@@ -5137,8 +5139,27 @@ impl MachineState {
let result = Addr::HeapCell(self.heap.to_list(chars));
self.unify(version, result);
}
&SystemClauseType::CryptoRandomByte => {
let arg = self[temp_v!(1)];
let mut bytes: Vec<u8> = vec![0];
rng().fill(&mut bytes);
let byte = self.heap.put_constant(Constant::Integer(Rc::new(Integer::from(bytes[0]))));
self.unify(arg, byte);
}
};
return_from_clause!(self.last_call, self)
}
}
fn rng() -> &'static SecureRandom {
use std::ops::Deref;
lazy_static! {
static ref RANDOM: SystemRandom = SystemRandom::new();
}
RANDOM.deref()
}