Rust 将哈希作为Vec插入时匹配哈希<;u8>; 使用sha3:{Digest,Keccak256}; fn显示投票(mut投票人id:Vec,mut短语:Vec,投票提交:Vec){ 让mut hyphen_vec=“-”.as_bytes().to_vec(); 投票者id.append(&mut hyphen\u vec); 投票者id追加(&mut短语); let vote_bytes:&[u8]=&voter_id; 让mut hasher=Keccak256::new(); hasher.update(投票字节); 让result=hasher.finalize(); let vote_commit_字节:&[u8]=&vote_commit; if&result[…]==投票\u提交\u字节{ println!(“数据匹配”); }否则{ println!(“数据不匹配”) } } fn main(){ 让我们投票表决将字符串=“1-abcdef”。作为字节()到向量(); 让mut hasher=Keccak256::new(); 更新(投票\提交\字符串); 让result=hasher.finalize(); 让commit=format!(“{:x}”,result);/e2a18e9b74f228590ca8c563cecfc58c2845b2dde25b4bbdc663e99e791f47c 让commit_vec=commit.as_bytes().to_vec(); println!(“提交:{:?}”,提交); 显示投票(“1.as_bytes().to_vec(),“abcdef.as_bytes().to_vec(),commit_vec”); }

Rust 将哈希作为Vec插入时匹配哈希<;u8>; 使用sha3:{Digest,Keccak256}; fn显示投票(mut投票人id:Vec,mut短语:Vec,投票提交:Vec){ 让mut hyphen_vec=“-”.as_bytes().to_vec(); 投票者id.append(&mut hyphen\u vec); 投票者id追加(&mut短语); let vote_bytes:&[u8]=&voter_id; 让mut hasher=Keccak256::new(); hasher.update(投票字节); 让result=hasher.finalize(); let vote_commit_字节:&[u8]=&vote_commit; if&result[…]==投票\u提交\u字节{ println!(“数据匹配”); }否则{ println!(“数据不匹配”) } } fn main(){ 让我们投票表决将字符串=“1-abcdef”。作为字节()到向量(); 让mut hasher=Keccak256::new(); 更新(投票\提交\字符串); 让result=hasher.finalize(); 让commit=format!(“{:x}”,result);/e2a18e9b74f228590ca8c563cecfc58c2845b2dde25b4bbdc663e99e791f47c 让commit_vec=commit.as_bytes().to_vec(); println!(“提交:{:?}”,提交); 显示投票(“1.as_bytes().to_vec(),“abcdef.as_bytes().to_vec(),commit_vec”); },rust,Rust,要检查哈希匹配,但哈希和字符串都在Vec中, 但是&result[…]==投票\u提交\u字节不匹配。如何检查平等性? 对不起,老问题,用十六进制解决了 use sha3::{Digest, Keccak256}; fn reveal_vote(mut voter_id: Vec<u8>, mut phrase: Vec<u8>, vote_commit: Vec<u8>) { let mut hyphen_vec = "-"

要检查哈希匹配,但哈希和字符串都在Vec中,
但是
&result[…]==投票\u提交\u字节
不匹配。如何检查平等性?
对不起,老问题,用十六进制解决了

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);
}
使用sha3:{Digest,Keccak256};
使用十六进制;
fn显示投票(mut投票人id:Vec,mut短语:Vec,投票提交:Vec){
让mut hyphen_vec=“-”.as_bytes().to_vec();
投票者id.append(&mut hyphen\u vec);
投票者id追加(&mut短语);
let vote_bytes:&[u8]=&voter_id;
让mut hasher=Keccak256::new();
hasher.update(投票字节);
让result=hasher.finalize();
if hex::encode(result).as_bytes().to_vec()=投票表决{
println!(“数据匹配”);
}否则{
println!(“数据不匹配”)
}
}
fn main(){
让我们投票表决将字符串=“1-abcdef”。作为字节()到向量();
让mut hasher=Keccak256::new();
更新(投票\提交\字符串);
让result=hasher.finalize();
println!(“{:?}”,结果);
println!(“{:?},&结果[…]);
让commit=format!(“{:x}”,result);/e2a18e9b74f228590ca8c563cecfc58c2845b2dde25b4bbdc663e99e791f47c
让commit_vec=commit.as_bytes().to_vec();
//println!(“提交:{:?}”,提交);
println!(“提交向量:{:?}”,提交向量);
显示投票(“1.as_bytes().to_vec(),“abcdef.as_bytes().to_vec(),commit_vec”);
}

不存在相等,因为显然
格式!()
macro以某种方式更改数据(我认为这并非完全出乎意料)

“格式化”选项将数据格式化为:

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

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

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

使用sha3:{Digest,Keccak256};
fn显示投票(mut投票人id:Vec,mut短语:Vec,投票提交:Vec){
让mut hyphen_vec=“-”.as_bytes().to_vec();
投票者id.append(&mut hyphen\u vec);
投票者id追加(&mut短语);
let vote_bytes:&[u8]=&voter_id;
让mut hasher=Keccak256::new();
hasher.update(投票字节);
让result=hasher.finalize();
let vote_commit_字节:&[u8]=&vote_commit;
让commit=format!(“{:x}”,result);/e2a18e9b74f228590ca8c563cecfc58c2845b2dde25b4bbdc663e99e791f47c
如果commit.as_bytes().to_vec()=投票\u commit_bytes{
println!(“数据匹配”);
}否则{
println!(“数据不匹配”)
}
}
fn main(){
让我们投票表决将字符串=“1-abcdef”。作为字节()到向量();
让mut hasher=Keccak256::new();
更新(投票\提交\字符串);
让result=hasher.finalize();
让commit=format!(“{:x}”,result);/e2a18e9b74f228590ca8c563cecfc58c2845b2dde25b4bbdc663e99e791f47c
让commit_vec=commit.as_bytes().to_vec();
println!(“提交:{:?}”,提交);
显示投票(“1.as_bytes().to_vec(),“abcdef.as_bytes().to_vec(),commit_vec”);
}

“不工作”不是问题描述。当你尝试时会发生什么?为什么这是错误的?它不匹配,我使用了错误的等式。
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);
}