rug = { version = "1.4.0", optional = true }
rustyline = "6.0.0"
unicode_reader = "1.0.0"
+ring = "0.16.13"
WriteTerm,
WriteTermToChars,
ScryerPrologVersion,
+ CryptoRandomByte
}
impl SystemClauseType {
&SystemClauseType::WriteTerm => clause_name!("$write_term"),
&SystemClauseType::WriteTermToChars => clause_name!("$write_term_to_chars"),
&SystemClauseType::ScryerPrologVersion => clause_name!("$scryer_prolog_version"),
+ &SystemClauseType::CryptoRandomByte => clause_name!("$crypto_random_byte"),
}
}
("$write_term", 7) => Some(SystemClauseType::WriteTerm),
("$write_term_to_chars", 7) => Some(SystemClauseType::WriteTermToChars),
("$scryer_prolog_version", 1) => Some(SystemClauseType::ScryerPrologVersion),
+ ("$crypto_random_byte", 1) => Some(SystemClauseType::CryptoRandomByte),
_ => None,
}
}
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)).
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).
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");
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()
+}