use ffi;
use libc::c_void;
use std::mem;
#[inline]
pub fn sha1(data: &[u8]) -> [u8; 20] {
unsafe {
let mut hash: [u8; 20] = mem::uninitialized();
ffi::SHA1(data.as_ptr(), data.len(), hash.as_mut_ptr());
hash
}
}
#[inline]
pub fn sha224(data: &[u8]) -> [u8; 28] {
unsafe {
let mut hash: [u8; 28] = mem::uninitialized();
ffi::SHA224(data.as_ptr(), data.len(), hash.as_mut_ptr());
hash
}
}
#[inline]
pub fn sha256(data: &[u8]) -> [u8; 32] {
unsafe {
let mut hash: [u8; 32] = mem::uninitialized();
ffi::SHA256(data.as_ptr(), data.len(), hash.as_mut_ptr());
hash
}
}
#[inline]
pub fn sha384(data: &[u8]) -> [u8; 48] {
unsafe {
let mut hash: [u8; 48] = mem::uninitialized();
ffi::SHA384(data.as_ptr(), data.len(), hash.as_mut_ptr());
hash
}
}
#[inline]
pub fn sha512(data: &[u8]) -> [u8; 64] {
unsafe {
let mut hash: [u8; 64] = mem::uninitialized();
ffi::SHA512(data.as_ptr(), data.len(), hash.as_mut_ptr());
hash
}
}
pub struct Sha1(ffi::SHA_CTX);
impl Sha1 {
#[inline]
pub fn new() -> Sha1 {
unsafe {
let mut ctx = mem::uninitialized();
ffi::SHA1_Init(&mut ctx);
Sha1(ctx)
}
}
#[inline]
pub fn update(&mut self, buf: &[u8]) {
unsafe {
ffi::SHA1_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len());
}
}
#[inline]
pub fn finish(mut self) -> [u8; 20] {
unsafe {
let mut hash: [u8; 20] = mem::uninitialized();
ffi::SHA1_Final(hash.as_mut_ptr(), &mut self.0);
hash
}
}
}
pub struct Sha224(ffi::SHA256_CTX);
impl Sha224 {
#[inline]
pub fn new() -> Sha224 {
unsafe {
let mut ctx = mem::uninitialized();
ffi::SHA224_Init(&mut ctx);
Sha224(ctx)
}
}
#[inline]
pub fn update(&mut self, buf: &[u8]) {
unsafe {
ffi::SHA224_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len());
}
}
#[inline]
pub fn finish(mut self) -> [u8; 28] {
unsafe {
let mut hash: [u8; 28] = mem::uninitialized();
ffi::SHA224_Final(hash.as_mut_ptr(), &mut self.0);
hash
}
}
}
pub struct Sha256(ffi::SHA256_CTX);
impl Sha256 {
#[inline]
pub fn new() -> Sha256 {
unsafe {
let mut ctx = mem::uninitialized();
ffi::SHA256_Init(&mut ctx);
Sha256(ctx)
}
}
#[inline]
pub fn update(&mut self, buf: &[u8]) {
unsafe {
ffi::SHA256_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len());
}
}
#[inline]
pub fn finish(mut self) -> [u8; 32] {
unsafe {
let mut hash: [u8; 32] = mem::uninitialized();
ffi::SHA256_Final(hash.as_mut_ptr(), &mut self.0);
hash
}
}
}
pub struct Sha384(ffi::SHA512_CTX);
impl Sha384 {
#[inline]
pub fn new() -> Sha384 {
unsafe {
let mut ctx = mem::uninitialized();
ffi::SHA384_Init(&mut ctx);
Sha384(ctx)
}
}
#[inline]
pub fn update(&mut self, buf: &[u8]) {
unsafe {
ffi::SHA384_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len());
}
}
#[inline]
pub fn finish(mut self) -> [u8; 48] {
unsafe {
let mut hash: [u8; 48] = mem::uninitialized();
ffi::SHA384_Final(hash.as_mut_ptr(), &mut self.0);
hash
}
}
}
pub struct Sha512(ffi::SHA512_CTX);
impl Sha512 {
#[inline]
pub fn new() -> Sha512 {
unsafe {
let mut ctx = mem::uninitialized();
ffi::SHA512_Init(&mut ctx);
Sha512(ctx)
}
}
#[inline]
pub fn update(&mut self, buf: &[u8]) {
unsafe {
ffi::SHA512_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len());
}
}
#[inline]
pub fn finish(mut self) -> [u8; 64] {
unsafe {
let mut hash: [u8; 64] = mem::uninitialized();
ffi::SHA512_Final(hash.as_mut_ptr(), &mut self.0);
hash
}
}
}
#[cfg(test)]
mod test {
use hex;
use super::*;
#[test]
fn standalone_1() {
let data = b"abc";
let expected = "a9993e364706816aba3e25717850c26c9cd0d89d";
assert_eq!(hex::encode(sha1(data)), expected);
}
#[test]
fn struct_1() {
let expected = "a9993e364706816aba3e25717850c26c9cd0d89d";
let mut hasher = Sha1::new();
hasher.update(b"a");
hasher.update(b"bc");
assert_eq!(hex::encode(hasher.finish()), expected);
}
#[test]
fn standalone_224() {
let data = b"abc";
let expected = "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7";
assert_eq!(hex::encode(sha224(data)), expected);
}
#[test]
fn struct_224() {
let expected = "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7";
let mut hasher = Sha224::new();
hasher.update(b"a");
hasher.update(b"bc");
assert_eq!(hex::encode(hasher.finish()), expected);
}
#[test]
fn standalone_256() {
let data = b"abc";
let expected = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
assert_eq!(hex::encode(sha256(data)), expected);
}
#[test]
fn struct_256() {
let expected = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
let mut hasher = Sha256::new();
hasher.update(b"a");
hasher.update(b"bc");
assert_eq!(hex::encode(hasher.finish()), expected);
}
#[test]
fn standalone_384() {
let data = b"abc";
let expected =
"cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e\
7cc2358baeca134c825a7";
assert_eq!(hex::encode(&sha384(data)[..]), expected);
}
#[test]
fn struct_384() {
let expected =
"cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e\
7cc2358baeca134c825a7";
let mut hasher = Sha384::new();
hasher.update(b"a");
hasher.update(b"bc");
assert_eq!(hex::encode(&hasher.finish()[..]), expected);
}
#[test]
fn standalone_512() {
let data = b"abc";
let expected =
"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274\
fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f";
assert_eq!(hex::encode(&sha512(data)[..]), expected);
}
#[test]
fn struct_512() {
let expected =
"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274\
fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f";
let mut hasher = Sha512::new();
hasher.update(b"a");
hasher.update(b"bc");
assert_eq!(hex::encode(&hasher.finish()[..]), expected);
}
}