Merge pull request #533 from triska/master

ADDED: Support for SHA-3 algorithms in crypto_data_hash/3
This commit is contained in:
Mark Thom
2020-05-19 14:23:12 -03:00
committed by GitHub
3 changed files with 70 additions and 33 deletions

View File

@@ -42,6 +42,8 @@ use crate::crossterm::terminal::{enable_raw_mode, disable_raw_mode};
use ring::rand::{SecureRandom, SystemRandom};
use ring::{digest,hkdf,pbkdf2,aead,error};
use ripemd160::{Ripemd160, Digest};
use sha3::{Sha3_224, Sha3_256, Sha3_384, Sha3_512};
use blake2::{Blake2s, Blake2b};
pub fn get_key() -> KeyEvent {
let key;
@@ -5241,21 +5243,39 @@ impl MachineState {
};
let ints_list =
if algorithm_str == "ripemd160" {
let mut context = Ripemd160::new();
context.input(&bytes);
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b))))))
} else {
let ints = digest::digest(
match algorithm_str {
"sha256" => { &digest::SHA256 }
"sha384" => { &digest::SHA384 }
"sha512" => { &digest::SHA512 }
"sha512_256" => { &digest::SHA512_256 }
_ => { unreachable!() }
},
&bytes);
Addr::HeapCell(self.heap.to_list(ints.as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b))))))
match algorithm_str {
"sha3_224" => { let mut context = Sha3_224::new();
context.input(&bytes);
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::from(Addr::Fixnum(*b as isize))))) }
"sha3_256" => { let mut context = Sha3_256::new();
context.input(&bytes);
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::from(Addr::Fixnum(*b as isize))))) }
"sha3_384" => { let mut context = Sha3_384::new();
context.input(&bytes);
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::from(Addr::Fixnum(*b as isize))))) }
"sha3_512" => { let mut context = Sha3_512::new();
context.input(&bytes);
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::from(Addr::Fixnum(*b as isize))))) }
"blake2s256" => { let mut context = Blake2s::new();
context.input(&bytes);
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::from(Addr::Fixnum(*b as isize))))) }
"blake2b512" => { let mut context = Blake2b::new();
context.input(&bytes);
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::from(Addr::Fixnum(*b as isize))))) }
"ripemd160" => { let mut context = Ripemd160::new();
context.input(&bytes);
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::from(Addr::Fixnum(*b as isize))))) }
_ => { let ints = digest::digest(
match algorithm_str {
"sha256" => { &digest::SHA256 }
"sha384" => { &digest::SHA384 }
"sha512" => { &digest::SHA512 }
"sha512_256" => { &digest::SHA512_256 }
_ => { unreachable!() }
},
&bytes);
Addr::HeapCell(self.heap.to_list(ints.as_ref().iter().map(|b| HeapCellValue::from(Addr::Fixnum(*b as isize)))))
}
};
self.unify(self[temp_v!(2)], ints_list);
@@ -5287,11 +5307,12 @@ impl MachineState {
usize::try_from(n).unwrap()
}
Ok(Number::Integer(n)) => {
n.to_usize().unwrap()
}
_ => {
unreachable!()
match n.to_usize() {
Some(u) => { u }
_ => { self.fail = true; return Ok(()); }
}
}
_ => { unreachable!() }
};
let ints_list =
@@ -5300,7 +5321,7 @@ impl MachineState {
"sha256" => { hkdf::HKDF_SHA256 }
"sha384" => { hkdf::HKDF_SHA384 }
"sha512" => { hkdf::HKDF_SHA512 }
_ => { unreachable!() }
_ => { self.fail = true; return Ok(()); }
};
let salt = hkdf::Salt::new(digest_alg, &salt);
let mut bytes : Vec<u8> = Vec::new();
@@ -5310,7 +5331,7 @@ impl MachineState {
_ => { self.fail = true; return Ok(()); }
}
Addr::HeapCell(self.heap.to_list(bytes.iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b))))))
Addr::HeapCell(self.heap.to_list(bytes.iter().map(|b| HeapCellValue::from(Addr::Fixnum(*b as isize)))))
};
self.unify(self[temp_v!(6)], ints_list);
@@ -5343,7 +5364,7 @@ impl MachineState {
NonZeroU32::new(iterations as u32).unwrap(), &salt,
&data, &mut bytes);
Addr::HeapCell(self.heap.to_list(bytes.iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b))))))
Addr::HeapCell(self.heap.to_list(bytes.iter().map(|b| HeapCellValue::from(Addr::Fixnum(*b as isize)))))
};
self.unify(self[temp_v!(4)], ints_list);
@@ -5368,7 +5389,7 @@ impl MachineState {
};
let tag_list =
Addr::HeapCell(self.heap.to_list(tag.as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b))))));
Addr::HeapCell(self.heap.to_list(tag.as_ref().iter().map(|b| HeapCellValue::from(Addr::Fixnum(*b as isize)))));
let complete_string = {
let buffer = String::from_iter(in_out.iter().map(|b| *b as char));