Created
February 18, 2019 17:16
-
-
Save encody/ddfae92ecfec16a30052d381a1337c43 to your computer and use it in GitHub Desktop.
Hash Comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use crypto_hash::*; | |
// Converts a 32-bit unsigned integer into its respective byte array | |
fn u32_bytes (x: u32) -> [u8; 4] { | |
[ | |
(x >> 0x0 * 8) as u8, | |
(x >> 0x1 * 8) as u8, | |
(x >> 0x2 * 8) as u8, | |
(x >> 0x3 * 8) as u8, | |
] | |
} | |
fn main () { | |
let matching = 3; // How many bytes from the beginning of the hash must match? | |
let s = b"GeekLaunch"; // String to match hashes against | |
println!("{:?}", s); | |
let h = digest(Algorithm::SHA256, s); | |
println!("{}", &hex::encode(&h)); // a17d5669f2148e2982baab7c0b4c7d81100c7cf52c45a8d7deb429aeba156ea6 | |
for x in 0..u32::max_value() { | |
if x % 50000 == 0 { | |
println!("{}", &x); | |
} | |
let b = u32_bytes(x); | |
let h2 = digest(Algorithm::SHA256, &b); | |
// Compare slices | |
if h[0..matching] == h2[0..matching] { | |
println!("{}: {:?} -> {:?}", &x, &b, &hex::encode(&h2)); // 1709934: [110, 23, 26, 0] -> "a17d560dfc6c675b65b33643fe8156f5ccdf97d8120e70c3c2e27cdc55142c50" | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment