use matching to select the hashing algorithm

Suggested by @notoria in #533. Many thanks!
This commit is contained in:
Markus Triska
2020-05-19 16:36:06 +02:00
parent 9f5322d309
commit dd32e69061

View File

@@ -5221,45 +5221,39 @@ impl MachineState {
}; };
let ints_list = let ints_list =
if algorithm_str == "sha3_224" { match algorithm_str {
let mut context = Sha3_224::new(); "sha3_224" => { let mut context = Sha3_224::new();
context.input(&bytes); context.input(&bytes);
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b)))))) Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b)))))) }
} else if algorithm_str == "sha3_256" { "sha3_256" => { let mut context = Sha3_256::new();
let mut context = Sha3_256::new(); context.input(&bytes);
context.input(&bytes); Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b)))))) }
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b)))))) "sha3_384" => { let mut context = Sha3_384::new();
} else if algorithm_str == "sha3_384" { context.input(&bytes);
let mut context = Sha3_384::new(); Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b)))))) }
context.input(&bytes); "sha3_512" => { let mut context = Sha3_512::new();
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b)))))) context.input(&bytes);
} else if algorithm_str == "sha3_512" { Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b)))))) }
let mut context = Sha3_512::new(); "blake2s256" => { let mut context = Blake2s::new();
context.input(&bytes); context.input(&bytes);
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b)))))) Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b)))))) }
} else if algorithm_str == "blake2s256" { "blake2b512" => { let mut context = Blake2b::new();
let mut context = Blake2s::new(); context.input(&bytes);
context.input(&bytes); Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b)))))) }
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b)))))) "ripemd160" => { let mut context = Ripemd160::new();
} else if algorithm_str == "blake2b512" { context.input(&bytes);
let mut context = Blake2b::new(); Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b)))))) }
context.input(&bytes); _ => { let ints = digest::digest(
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b)))))) match algorithm_str {
} else if algorithm_str == "ripemd160" { "sha256" => { &digest::SHA256 }
let mut context = Ripemd160::new(); "sha384" => { &digest::SHA384 }
context.input(&bytes); "sha512" => { &digest::SHA512 }
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b)))))) "sha512_256" => { &digest::SHA512_256 }
} else { _ => { unreachable!() }
let ints = digest::digest( },
match algorithm_str { &bytes);
"sha256" => { &digest::SHA256 } Addr::HeapCell(self.heap.to_list(ints.as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b))))))
"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))))))
}; };
self.unify(self[temp_v!(2)], ints_list); self.unify(self[temp_v!(2)], ints_list);