当哈希作为 Vec<u8> 插入时匹配哈希

时间:2021-04-14 11:05:39

标签: rust

use sha3::{Digest, Keccak256};


fn reveal_vote(mut voter_id: Vec<u8>, mut phrase: Vec<u8>, vote_commit: Vec<u8>) {

    let mut hyphen_vec = "-".as_bytes().to_vec();
    voter_id.append(&mut hyphen_vec);
    voter_id.append(&mut phrase);
    let vote_bytes: &[u8] = &voter_id;
    let mut hasher = Keccak256::new();
    hasher.update(vote_bytes);
    let result = hasher.finalize();
    let vote_commit_bytes: &[u8] = &vote_commit;

    if &result[..] == vote_commit_bytes {
        println!("Data matched");
    } else {
        println!("data mismatched")
    }
}

fn main() {
    let vote_commit_string = "1-abcdef".as_bytes().to_vec();
    let mut hasher = Keccak256::new();
    hasher.update(vote_commit_string);
    let result = hasher.finalize();
    let commit = format!("{:x}", result); // e2a18e9b74f228590ca8c563cecfc58c28455b2dde25b4bbdc663e99e791f47c
    let commit_vec = commit.as_bytes().to_vec();
    println!("commit: {:?}", commit);
    reveal_vote("1".as_bytes().to_vec(), "abcdef".as_bytes().to_vec(), commit_vec);
}

想检查hash匹配,但是hash和string都在Vec,
&result[..] == vote_commit_bytes 不匹配。如何检查相等性? https://docs.rs/sha3/0.9.1/sha3/

2 个答案:

答案 0 :(得分:2)

不存在相等性,因为显然 format!() 宏以某种方式改变了数据(我认为这并不完全出乎意料)。

{:x} 格式化选项,将数据格式化为:

<块引用>

使用小写十六进制整数调试

这就是为什么这些表示不相等的原因。

如果您在代码中格式化两个哈希结果,它会按预期工作:

use sha3::{Digest, Keccak256};


fn reveal_vote(mut voter_id: Vec<u8>, mut phrase: Vec<u8>, vote_commit: Vec<u8>) {

    let mut hyphen_vec = "-".as_bytes().to_vec();
    voter_id.append(&mut hyphen_vec);
    voter_id.append(&mut phrase);
    let vote_bytes: &[u8] = &voter_id;
    let mut hasher = Keccak256::new();
    hasher.update(vote_bytes);
    let result = hasher.finalize();
    let vote_commit_bytes: &[u8] = &vote_commit;

    let commit = format!("{:x}", result); // e2a18e9b74f228590ca8c563cecfc58c28455b2dde25b4bbdc663e99e791f47c

    if commit.as_bytes().to_vec() == vote_commit_bytes {
        println!("Data matched");
    } else {
        println!("data mismatched")
    }
}

fn main() {
    let vote_commit_string = "1-abcdef".as_bytes().to_vec();
    let mut hasher = Keccak256::new();
    hasher.update(vote_commit_string);
    let result = hasher.finalize();
    let commit = format!("{:x}", result); // e2a18e9b74f228590ca8c563cecfc58c28455b2dde25b4bbdc663e99e791f47c
    let commit_vec = commit.as_bytes().to_vec();
    println!("commit: {:?}", commit);
    reveal_vote("1".as_bytes().to_vec(), "abcdef".as_bytes().to_vec(), commit_vec);
}

答案 1 :(得分:0)

抱歉,老问题,用十六进制解决了

use sha3::{Digest, Keccak256};
use hex;


fn reveal_vote(mut voter_id: Vec<u8>, mut phrase: Vec<u8>, vote_commit: Vec<u8>) {

    let mut hyphen_vec = "-".as_bytes().to_vec();
    voter_id.append(&mut hyphen_vec);
    voter_id.append(&mut phrase);
    let vote_bytes: &[u8] = &voter_id;
    let mut hasher = Keccak256::new();
    hasher.update(vote_bytes);
    let result = hasher.finalize();    
    if  hex::encode(result).as_bytes().to_vec() == vote_commit {
        println!("Data matched");
    } else {
        println!("data mismatched")
    }
}

fn main() {
    let vote_commit_string = "1-abcdef".as_bytes().to_vec();
    let mut hasher = Keccak256::new();
    hasher.update(vote_commit_string);
    let result = hasher.finalize();
    println!("{:?}", result);
    println!("{:?}", &result[..]);
    let commit = format!("{:x}", result); // e2a18e9b74f228590ca8c563cecfc58c28455b2dde25b4bbdc663e99e791f47c
    let commit_vec = commit.as_bytes().to_vec();
    // println!("commit: {:?}", commit);
    println!("commit_vec: {:?}", commit_vec);
    reveal_vote("1".as_bytes().to_vec(), "abcdef".as_bytes().to_vec(), commit_vec);
}