简体   繁体   中英

What are the supported hashing algorithms in a Substrate runtime module?

What hashing algorithms do I have access to when building a Substrate runtime module?

Can I import other hashing algorithms to be used within a Substrate runtime module?

As of writing this post, Substrate provides a HashingApi trait in the core/sr-io crate which provides the following hash functions:

export_api! {
    pub(crate) trait HashingApi {
        /// Conduct a 256-bit Keccak hash.
        fn keccak_256(data: &[u8]) -> [u8; 32] ;

        /// Conduct a 128-bit Blake2 hash.
        fn blake2_128(data: &[u8]) -> [u8; 16];

        /// Conduct a 256-bit Blake2 hash.
        fn blake2_256(data: &[u8]) -> [u8; 32];

        /// Conduct four XX hashes to give a 256-bit result.
        fn twox_256(data: &[u8]) -> [u8; 32];

        /// Conduct two XX hashes to give a 128-bit result.
        fn twox_128(data: &[u8]) -> [u8; 16];

        /// Conduct two XX hashes to give a 64-bit result.
        fn twox_64(data: &[u8]) -> [u8; 8];
    }
}

Because these functions are written for the Runtime, which must build to Wasm, they must compile without the use of the standard Rust library ( std ).

If you want to introduce new hashing algorithms or any new library to your Substrate runtime, you must make sure that it too can be built without std , but other than that, I believe the sky is the limit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM