2

Starting with all the printable characters, I want to get the sha256 characters one by one and compare them to the sha256 taken from the input.

My code is :

use hex_literal::hex;
use sha2::{Digest, Sha256};
use std::io;


fn main() {
    let printable = vec![b" ", b"!", b"#"];
    enter code here
    let mut hash_target = String::new();

    io::stdin()
        .read_line(&mut hash_target)
        .expect("Something Wrong!").to_owned();
    
    let hash_target = &hash_target[..];
    
    for i in 0..printable.len() {
        let mut sha256 = Sha256::new();

        sha256.update(printable[i]);

        let result = sha256.finalize();
        if result[..]
            == hex!(hash_target)[..]
        {
            println!("True");
        }
    }
}

I do not know how to give the value of the sha read from the input to the hex function.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Emad Deve
  • 33
  • 10
  • Does this answer your question? [How can I convert a hex string to a u8 slice?](https://stackoverflow.com/questions/52987181/how-can-i-convert-a-hex-string-to-a-u8-slice) – cdhowie May 27 '22 at 05:44

1 Answers1

0

As stated in the documentation of the hex_literal crate, the hex_literal::hex! macro is intended for hex literals and thus does not work with runtime-generated strings as the input.

To convert a hex string into a u8 slice, you may want to use the hex crate like this.

let target = hex::decode(hash_target).expect("Failed to decode hex string");

A full example adapted from the original code with some minor fixes:

use sha2::{Digest, Sha256};
use std::io;

fn main() {
    let printables = vec![b" ", b"!", b"#"];
    let mut input = String::new();

    io::stdin()
        .read_line(&mut input)
        .expect("Failed to read input");
    let input_hash = hex::decode(input.trim()).expect("Failed to decode");

    for printable in printables.iter() {
        let mut sha256 = Sha256::new();
        sha256.update(printable);
        let expected_hash = sha256.finalize();

        if &expected_hash[..] == &input_hash[..] {
            println!("True");
        }
    }
}
kotatsuyaki
  • 1,441
  • 3
  • 10
  • 17
  • hi, @kotatsuyaki thank you for your answer. I tested your code but not work! do you know what style is better for checking for two sha256 in rust? – Emad Deve May 27 '22 at 13:54
  • i got the "thread 'main' panicked at 'Failed to decode hex string: OddLength" error with your answer(@kotatsuyaki). – Emad Deve May 27 '22 at 13:57
  • As suggested by [the documentation of `hex::FromHexError`](https://docs.rs/hex/latest/hex/enum.FromHexError.html), the error you got meant that the string you passed to `hex::decode` had an odd length, while it should be even. What did you pass to `hex::decode`? Can you print it for debugging? – kotatsuyaki May 27 '22 at 14:01
  • printed targte var? it is not posiable! Because panic occurs exactly in the value line of this variable. – Emad Deve May 27 '22 at 14:11
  • I can set the string slice in the "hex::decode" function and it is works. but i can not set the input string! – Emad Deve May 27 '22 at 14:15
  • I asked you to try to print **the input** to `hex::decode`, not the output. The `OddLength` error occurs only if the input (in the case it's `hash_target`) has an odd length. – kotatsuyaki May 27 '22 at 14:22
  • I understand what you mean, but it does not work. I do not know the reason, have you tried this code yourself? Panic happens in exactly the same line. I can not print it – Emad Deve May 27 '22 at 14:26
  • I updated my answer with a working piece of code adapted from yours. – kotatsuyaki May 27 '22 at 14:35
  • Thank you for your patience and support. Your code worked – Emad Deve May 27 '22 at 14:45
  • i must use the "trim" function. your right – Emad Deve May 27 '22 at 14:46