Change hashing algorithm from sha1 to meowhash

This commit is contained in:
Lachlan Sneff
2019-02-22 13:02:28 -08:00
parent cb84894d42
commit 4042cff914
5 changed files with 47 additions and 71 deletions

View File

@ -2,7 +2,8 @@ use crate::{
module::{Module, ModuleInfo},
sys::Memory,
};
use sha2::{Digest, Sha256};
use digest::Digest;
use meowhash::MeowHasher;
use std::{fmt, io, mem, slice};
#[derive(Debug)]
@ -33,7 +34,7 @@ impl From<io::Error> for Error {
///
/// [`Cache`]: trait.Cache.html
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct WasmHash([u8; 32]);
pub struct WasmHash([u8; 32], [u8; 32]);
impl WasmHash {
/// Hash a wasm module.
@ -42,19 +43,26 @@ impl WasmHash {
/// This does no verification that the supplied data
/// is, in fact, a wasm module.
pub fn generate(wasm: &[u8]) -> Self {
let mut array = [0u8; 32];
array.copy_from_slice(Sha256::digest(wasm).as_slice());
WasmHash(array)
let mut first_part = [0u8; 32];
let mut second_part = [0u8; 32];
let generic_array = MeowHasher::digest(wasm);
first_part.copy_from_slice(&generic_array[0..32]);
second_part.copy_from_slice(&generic_array[32..64]);
WasmHash(first_part, second_part)
}
/// Create the hexadecimal representation of the
/// stored hash.
pub fn encode(self) -> String {
hex::encode(self.0)
hex::encode(&self.into_array() as &[u8])
}
pub(crate) fn into_array(self) -> [u8; 32] {
self.0
pub(crate) fn into_array(self) -> [u8; 64] {
let mut total = [0u8; 64];
total[0..32].copy_from_slice(&self.0);
total[32..64].copy_from_slice(&self.1);
total
}
}
@ -67,7 +75,7 @@ struct ArtifactHeader {
magic: [u8; 8], // [W, A, S, M, E, R, \0, \0]
version: u64,
data_len: u64,
wasm_hash: [u8; 32], // Sha256 of the wasm in binary format.
wasm_hash: [u8; 64], // Sha256 of the wasm in binary format.
}
impl ArtifactHeader {