【发布时间】:2020-10-26 20:45:08
【问题描述】:
我刚开始生锈,我已经读完紫色螃蟹书的第 1 章,但我被卡住了。
我将这个箱子用于 sha1 https://docs.rs/sha-1/0.9.1/sha1/
我想返回 m.finalize(); 的输出 我如何在 rust 中实现这一点?
在 python 中我会做这样的事情
def gen_chain():
chain_redux = "some_Str"
hash = None
hasher = Sha1()
for i in range(0,iterations):
hasher.update(chain_redux)
hash = hasher.finalize()
hasher.reset()
chain_redux = redux(hash)
print(chain_redux)
return hash
这就是我所拥有的 rust
fn calc_chain(name_table:&Vec<String> ,iterations:i32) -> GenericArray<u8, <sha1::Sha1 as Digest>::OutputSize>{
let mut m = sha1::Sha1::new();
let mut chain_redux = &name_table[0];
let mut hash;
// The above doesnt fly in rust. and i can't figure out how to make the arr! macro return a
// GenericArray<u8, <sha1::Sha1 as Digest>::OutputSize> I also don't know if I should.
for i in 0..iterations {
println!("Curr string: {}",chain_redux);
m.update(chain_redux);
hash = m.finalize(); // let hash = m.finalize() here keeps me from returning it because rust
// deletes it after the for loop scope
m.reset();
chain_redux = redux(&hash, name_table);
println!("Redux Output: {}",chain_redux);
};
hash
}
任何防锈提示/指针也将在这里受到赞赏。
【问题讨论】:
标签: rust