Cleanup and docs.

This commit is contained in:
Gav Wood 2016-01-16 15:29:36 +01:00
parent a79644d982
commit 8e9ea7969a

View File

@ -1,7 +1,7 @@
//! Wrapper around tiny-keccak crate.
use std::mem::uninitialized;
use bytes::BytesConvertable;
use bytes::{BytesConvertable, Populatable};
use hash::{H256, FixedHash};
pub const SHA3_EMPTY: H256 = H256( [0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0, 0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70] );
@ -23,7 +23,10 @@ extern {
/// }
/// ```
pub trait Hashable {
/// Calculate SHA3 of this object.
fn sha3(&self) -> H256;
/// Calculate SHA3 of this object and place result into dest.
fn sha3_into(&self, dest: &mut [u8]) {
self.sha3().copy_to(dest);
}
@ -32,9 +35,8 @@ pub trait Hashable {
impl<T> Hashable for T where T: BytesConvertable {
fn sha3(&self) -> H256 {
unsafe {
let input: &[u8] = self.bytes();
let mut ret: H256 = uninitialized();
sha3_256(ret.as_mut_ptr(), ret.len(), input.as_ptr(), input.len());
self.sha3_into(ret.as_slice_mut());
ret
}
}