Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sha2 example is out of date #95

Closed
urbanslug opened this issue Dec 9, 2019 · 2 comments
Closed

Sha2 example is out of date #95

urbanslug opened this issue Dec 9, 2019 · 2 comments

Comments

@urbanslug
Copy link

Seems to me that the example on src/sha2/src/lib.rs#L20 cannot be ran, one would run into errors such as Sha256::new() not existing.

Something like this works for me though:

use sha2::{Sha256, Sha512, Digest};

let inp = "hello, world".as_bytes();

    
// SHA 256
// create a Sha512 object
let mut hasher: Sha256 = Digest::new();

// write input message
hasher.input(inp);
    
// read hash digest and consume hasher
let result = hasher.result();
    
let a_str = format!("{:x}", result);
assert_eq!(a_str, "\
09ca7e4eaa6e8ae9c7d261167129184883644d07dfba7cbfbc4c8a2e08360d5b\
");
    
    
// SHA 512
// create a Sha512 object
let mut hasher: Sha512 = Digest::new();

// write input message
hasher.input(inp);
    
// read hash digest and consume hasher
let result = hasher.result();
    
let a_str = format!("{:x}", result);
assert_eq!(a_str, "\
8710339dcb6814d0d9d2290ef422285c9322b7163951f9a0ca8f883d3305286f\
44139aa374848e4174f5aada663027e4548637b6d19894aec4fb6c46a139fbf9\
");

Am I missing something?


Slightly unrelated but still on documentation, could be related to #86.

Is there an ergonomic way to return a hash given some input?

I could try return a GenericArray but that seems to have 2 disadvantages:

The below doesn't compile but shows what I have in mind

extern crate sha2; // 0.7.1
use sha2::{Sha256, Sha512, Digest};

fn perform_hashing<'a> (str_to_hash: &str, hasher: &'a mut Sha256) -> &'a [u8] {
    hasher.input(str_to_hash.as_bytes());

    // we will be hashing a lot of strings
    // can be more efficient compared to hasher re-creation.
    let a = hasher.result_reset();
    &a[..]
}

fn main() {
    let my_strings = vec!["hello, world", "helios"];
    
    let mut hasher: Sha256 = Digest::new();
    
    for string in my_strings {
        perform_hashing(string, &mut hasher);
    }
}
@tarcieri
Copy link
Member

tarcieri commented Dec 9, 2019

Note that the doc tests for the linked example pass:

 cargo test --doc
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
   Doc-tests sha2

running 9 tests
[...]
test src/lib.rs -  (line 22) ... ok

test result: ok. 1 passed; 0 failed; 8 ignored; 0 measured; 0 filtered out

The new() method is part of the Digest trait, which you'll need in-scope:

https://docs.rs/digest/0.8.1/digest/trait.Digest.html


Is there an ergonomic way to return a hash given some input?

You probably want Sha256::digest():

https://docs.rs/digest/0.8.1/digest/trait.Digest.html#tymethod.digest

use sha2::{Sha256, Digest};

[...]

let digest = Sha256::digest("Hello, world!");

If you'd prefer to return a [u8; 32] rather than a GenericArray, you can use a try_into() conversion:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4cc5c3f1d7596df1656bacac701dbd9c

pub fn sha256(msg: &[u8]) -> [u8; 32] {
    Sha256::digest(msg).as_slice().try_into().unwrap()
}

@urbanslug
Copy link
Author

Thank you for this response 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants