-
-
Save rust-play/4fa62a7ab7b5ce30fd70ab64ac70ddd3 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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 blake2::{Blake2b, Digest}; | |
fn to_fixed_be_bytes(x: usize) -> [u8; 4] { | |
(x as u32).to_be_bytes() | |
} | |
fn find_hash(data: &[u8], label: &[u8]) -> Option<Vec<u8>> { | |
let len_data = to_fixed_be_bytes(data.len()); | |
let len_label = to_fixed_be_bytes(label.len()); | |
// label_data = len_label + label + len_data + data | |
let label_data: Vec<u8> = len_label.iter() | |
.chain(label.iter()) | |
.chain(len_data.iter()) | |
.chain(data.iter()).cloned().collect(); | |
let key_size_bytes = 32; | |
let mut to_hash = Vec::with_capacity(label_data.len() + 4); | |
to_hash.extend_from_slice(&label_data); | |
let mut i = 0u32; | |
while i < u32::MAX { | |
let ibytes = i.to_be_bytes(); | |
to_hash.truncate(label_data.len()); | |
to_hash.extend_from_slice(&ibytes); | |
let mut hash_function = Blake2b::new(); | |
hash_function.update(&to_hash); | |
let hash_digest_full = hash_function.finalize(); | |
let hash_digest = &hash_digest_full[0..1+key_size_bytes]; | |
let sign = if hash_digest[0] & 1 == 0 { b"\x02" } else { b"\x03" }; | |
// point = sign + hash_digest[1..end] | |
let point: Vec<u8> = sign.iter().chain(hash_digest[1..hash_digest.len()].iter()).cloned().collect(); | |
if point[1] == 0xff { | |
return Some(point); | |
} | |
i += 1 | |
} | |
None | |
} | |
fn main() { | |
let data = b"abcdefg"; | |
let label = b"sdasdasd"; | |
let p = find_hash(&data[..], &label[..]); | |
println!("{:?}", p); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment