你如何在奇偶校验衬底的“哈希”中修改字节?

时间:2019-01-14 09:19:11

标签: blockchain parity-io substrate

鉴于在底物运行时内生成的某个Hash值,我如何修改或访问该哈希的各个字节?

1 个答案:

答案 0 :(得分:3)

Hash特征Output具有AsRefAsMut特征,使您可以作为字节片([u8])与哈希进行交互:

pub trait Hash: 'static + MaybeSerializeDebug + Clone + Eq + PartialEq {
    type Output: Member + MaybeSerializeDebug + AsRef<[u8]> + AsMut<[u8]>;

    // ... removed for brevity
}

在哈希上使用as_ref()as_mut()将返回一片字节,您可以照常使用它们:

例如:

// Iterate over a hash
let hash1 = <T as system::Trait>::Hashing::hash(1);
for hash_byte in hash1.as_ref().iter() {
    // ... do something
}

// Add one to the first byte of a hash
let mut hash2 = <T as system::Trait>::Hashing::hash(2);
hash2.as_mut()[0] += 1;