Refactored a lot of cryptography. Moved from "rust-crypto" to https://github.com/RustCrypto
This commit is contained in:
@@ -1,18 +1,9 @@
|
||||
use blakeout::Blakeout;
|
||||
use crypto::digest::Digest;
|
||||
use crypto::sha2::Sha256;
|
||||
use num_bigint::BigUint;
|
||||
use num_traits::One;
|
||||
|
||||
use crate::{Block, Bytes, Keystore};
|
||||
|
||||
/// Creates needed hasher by current blockchain version
|
||||
pub(crate) fn get_hasher_for_version(version: u32) -> Box<dyn Digest> {
|
||||
match version {
|
||||
2 => Box::new(Blakeout::default()),
|
||||
_ => Box::new(Sha256::new())
|
||||
}
|
||||
}
|
||||
use sha2::{Sha256, Digest};
|
||||
|
||||
/// Checks block's hash and returns true on valid hash or false otherwise
|
||||
pub fn check_block_hash(block: &Block) -> bool {
|
||||
@@ -20,21 +11,14 @@ pub fn check_block_hash(block: &Block) -> bool {
|
||||
copy.hash = Bytes::default();
|
||||
copy.signature = Bytes::default();
|
||||
let data = serde_json::to_string(©).unwrap();
|
||||
let mut hasher = get_hasher_for_version(block.version);
|
||||
hash_data(&mut *hasher, data.as_bytes()) == block.hash
|
||||
blakeout_data(data.as_bytes()) == block.hash
|
||||
}
|
||||
|
||||
/// Hashes data by given hasher
|
||||
pub fn hash_data(digest: &mut dyn Digest, data: &[u8]) -> Bytes {
|
||||
let mut buf = match digest.output_bytes() {
|
||||
32 => Bytes::zero32(),
|
||||
64 => Bytes::zero64(),
|
||||
_ => panic!("Supplied wrong digest!")
|
||||
};
|
||||
|
||||
digest.input(data);
|
||||
digest.result(buf.as_mut_slice());
|
||||
buf
|
||||
pub fn blakeout_data(data: &[u8]) -> Bytes {
|
||||
let mut digest = Blakeout::default();
|
||||
digest.update(data);
|
||||
Bytes::from_bytes(digest.result())
|
||||
}
|
||||
|
||||
/// Checks block's signature, returns true if the signature is valid, false otherwise
|
||||
@@ -47,14 +31,12 @@ pub fn check_block_signature(block: &Block) -> bool {
|
||||
/// Hashes some identity (domain in case of DNS). If you give it a public key, it will hash with it as well.
|
||||
/// Giving public key is needed to create a confirmation field in [Transaction]
|
||||
pub fn hash_identity(identity: &str, key: Option<&Bytes>) -> Bytes {
|
||||
let mut buf: [u8; 32] = [0; 32];
|
||||
let mut digest = Sha256::new();
|
||||
digest.input_str(identity);
|
||||
let mut digest = Sha256::default();
|
||||
digest.update(identity.as_bytes());
|
||||
if let Some(key) = key {
|
||||
digest.input(key.as_slice());
|
||||
digest.update(key.as_slice());
|
||||
}
|
||||
digest.result(&mut buf);
|
||||
Bytes::from_bytes(&buf)
|
||||
Bytes::from_bytes(&digest.finalize()[..])
|
||||
}
|
||||
|
||||
/// There is no default PartialEq implementation for arrays > 32 in size
|
||||
|
||||
Reference in New Issue
Block a user