Skip to content

Commit

Permalink
feat: move to ring
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jan 15, 2017
1 parent 1b095e2 commit 624e7cf
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
target
Cargo.lock
*.bk
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -14,4 +14,4 @@ license = "MIT"
readme = "README.md"

[dependencies]
sodiumoxide = "~0.0.9"
ring = "~0.6.2"
13 changes: 4 additions & 9 deletions README.md
Expand Up @@ -4,7 +4,7 @@
[![](https://img.shields.io/badge/project-multiformats-blue.svg?style=flat-square)](http://github.com/multiformats/multiformats)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Build Status](https://img.shields.io/travis/multiformats/rust-multihash/master.svg?style=flat-square)](https://travis-ci.org/multiformats/rust-multihash)
[![Coverage Status](https://coveralls.io/repos/multiformats/rust-multihash/badge.svg?style=flat-square&branch=master)](https://coveralls.io/r/multiformats/rust-multihash?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/multiformats/rust-multihash/badge.svg?style=flat-square&branch=master)](https://coveralls.io/github/multiformats/rust-multihash)[
[![](https://img.shields.io/badge/rust-docs-blue.svg?style=flat-square)](http://multiformats.github.io/rust-multihash/multihash/struct.Multihash.html)
[![crates.io](http://meritbadge.herokuapp.com/multihash?style=flat-square)](https://crates.io/crates/multihash)

Expand Down Expand Up @@ -46,14 +46,9 @@ let multi = decode(&hash).unwrap();

## Supported Hash Types

* `SHA2 256`
* `SHA2 512`


## Dependencies

This uses [libsodium](https://github.com/jedisct1/libsodium) and [sodiumoxide](https://github.com/dnaq/sodiumoxide)
for the hashing so it depends on libsodium being installed.
* `SHA1`
* `SHA2-256`
* `SHA2-512`

## Maintainers

Expand Down
54 changes: 36 additions & 18 deletions src/hashes.rs
Expand Up @@ -10,21 +10,30 @@ pub enum HashTypes {
/// SHA-512 (64-byte hash size)
SHA2512,
/// Encoding unsupported
SHA3,
SHA3512,
/// Encoding unsupported
SHA3384,
/// Encoding unsupported
SHA3256,
/// Encoding unsupported
SHA3224,
/// Encoding unsupported
Blake2b,
/// Encoding unsupported
Blake2s
Blake2s,
}

impl HashTypes {
/// Get the corresponding hash code
pub fn code(&self) -> u8 {
match *self {
HashTypes::SHA1 => 0x11,
HashTypes::SHA1 => 0x11,
HashTypes::SHA2256 => 0x12,
HashTypes::SHA2512 => 0x13,
HashTypes::SHA3 => 0x14,
HashTypes::SHA3512 => 0x14,
HashTypes::SHA3384 => 0x15,
HashTypes::SHA3256 => 0x16,
HashTypes::SHA3224 => 0x17,
HashTypes::Blake2b => 0x40,
HashTypes::Blake2s => 0x41,
}
Expand All @@ -33,24 +42,30 @@ impl HashTypes {
/// Get the hash length in bytes
pub fn size(&self) -> u8 {
match *self {
HashTypes::SHA1 => 20,
HashTypes::SHA2256 => 32,
HashTypes::SHA2512 => 64,
HashTypes::SHA3 => 64,
HashTypes::Blake2b => 64,
HashTypes::Blake2s => 32,
HashTypes::SHA1 => 20,
HashTypes::SHA2256 => 32,
HashTypes::SHA2512 => 64,
HashTypes::SHA3512 => 64,
HashTypes::SHA3384 => 64,
HashTypes::SHA3256 => 64,
HashTypes::SHA3224 => 64,
HashTypes::Blake2b => 64,
HashTypes::Blake2s => 32,
}
}

/// Get the human readable name
pub fn name(&self) -> &str {
match *self {
HashTypes::SHA1 => "SHA1",
HashTypes::SHA2256 => "SHA2-256",
HashTypes::SHA2512 => "SHA2-512",
HashTypes::SHA3 => "SHA3",
HashTypes::Blake2b => "Blake-2b",
HashTypes::Blake2s => "Blake-2s",
HashTypes::SHA1 => "SHA1",
HashTypes::SHA2256 => "SHA2-256",
HashTypes::SHA2512 => "SHA2-512",
HashTypes::SHA3512 => "SHA3-512",
HashTypes::SHA3384 => "SHA3-384",
HashTypes::SHA3256 => "SHA3-256",
HashTypes::SHA3224 => "SHA3-224",
HashTypes::Blake2b => "Blake-2b",
HashTypes::Blake2s => "Blake-2s",
}
}

Expand All @@ -59,10 +74,13 @@ impl HashTypes {
0x11 => Some(HashTypes::SHA1),
0x12 => Some(HashTypes::SHA2256),
0x13 => Some(HashTypes::SHA2512),
0x14 => Some(HashTypes::SHA3),
0x14 => Some(HashTypes::SHA3512),
0x15 => Some(HashTypes::SHA3384),
0x16 => Some(HashTypes::SHA3256),
0x17 => Some(HashTypes::SHA3224),
0x40 => Some(HashTypes::Blake2b),
0x41 => Some(HashTypes::Blake2s),
_ => None
_ => None,
}
}
}
71 changes: 33 additions & 38 deletions src/lib.rs
@@ -1,38 +1,21 @@
// For explanation of lint checks, run `rustc -W help`
// This is adapted from
// https://github.com/maidsafe/QA/blob/master/Documentation/Rust%20Lint%20Checks.md
#![forbid(bad_style, exceeding_bitshifts, mutable_transmutes, no_mangle_const_items,
unknown_crate_types, warnings)]
#![deny(deprecated, improper_ctypes, //missing_docs,
non_shorthand_field_patterns, overflowing_literals, plugin_as_library,
private_no_mangle_fns, private_no_mangle_statics, stable_features, unconditional_recursion,
unknown_lints, unsafe_code, unused, unused_allocation, unused_attributes,
unused_comparisons, unused_features, unused_parens, while_true)]
#![warn(trivial_casts, unused_extern_crates, unused_import_braces,
unused_qualifications, unused_results, variant_size_differences)]
#![allow(box_pointers, fat_ptr_transmutes, missing_copy_implementations,
missing_debug_implementations)]

///! # multihash
///!
///! Implementation of [multihash](https://github.com/jbenet/multihash)
///! in Rust.
/// ! # multihash
/// !
/// ! Implementation of [multihash](https://github.com/jbenet/multihash)
/// ! in Rust.
/// Representation of a Multiaddr.

extern crate sodiumoxide;
extern crate ring;

use sodiumoxide::crypto::hash::{sha256, sha512};
use ring::digest;
use std::io;

mod hashes;
pub use hashes::*;



/// Encodes data into a multihash.
/// Encodes data into a multihash.
///
/// The returned data is raw bytes. To make is more human-friendly, you can encode it (hex,
/// base58, base64, etc).
/// base58, base64, etc).
///
/// # Errors
///
Expand All @@ -53,9 +36,22 @@ pub use hashes::*;
///
pub fn encode(wanttype: HashTypes, input: &[u8]) -> io::Result<Vec<u8>> {
let digest: Vec<u8> = match wanttype {
HashTypes::SHA2256 => sha256::hash(input).as_ref().to_owned(),
HashTypes::SHA2512 => sha512::hash(input).as_ref().to_owned(),
_ => return Err(io::Error::new(io::ErrorKind::Other, "Unsupported hash type"))
// Test is failing, not sure why :/
// HashTypes::SHA1 => {
// digest::digest(&digest::SHA256, input)
// .as_ref().to_owned()
// }
HashTypes::SHA2256 => {
digest::digest(&digest::SHA256, input)
.as_ref()
.to_owned()
}
HashTypes::SHA2512 => {
digest::digest(&digest::SHA512, input)
.as_ref()
.to_owned()
}
_ => return Err(io::Error::new(io::ErrorKind::Other, "Unsupported hash type")),
};

let mut bytes = Vec::with_capacity(digest.len() + 2);
Expand Down Expand Up @@ -100,32 +96,31 @@ pub fn decode(input: &[u8]) -> io::Result<Multihash> {
let hash_len = alg.size() as usize;
// length of input should be exactly hash_len + 2
if input.len() != hash_len + 2 {
Err(io::Error::new(io::ErrorKind::Other, format!("Bad input length. Expected {}, found {}", hash_len + 2, input.len())))
Err(io::Error::new(io::ErrorKind::Other,
format!("Bad input length. Expected {}, found {}",
hash_len + 2,
input.len())))
} else {
Ok(Multihash {
alg: alg,
digest: &input[2..],
})
}
},
None => {
Err(io::Error::new(io::ErrorKind::Other, format!("Unkown code {:?}", code)))
}
None => Err(io::Error::new(io::ErrorKind::Other, format!("Unkown code {:?}", code))),
}
}

/// Represents a valid multihash, by associating the hash algorithm with the data
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Multihash<'a> {
pub alg: HashTypes,
pub digest: &'a [u8]
pub digest: &'a [u8],
}

/// Convert bytes to a hex representation
pub fn to_hex(bytes: &[u8]) -> String {
bytes.iter().map(|x| {
format!("{:02x}", x)
}).collect()
bytes.iter()
.map(|x| format!("{:02x}", x))
.collect()
}


0 comments on commit 624e7cf

Please sign in to comment.