author liangqin.fan gmail import { Codec , Hash } from "as-scale-codec" ; import { seal_hash_blake2_128 , seal_hash_blake2_256 , seal_hash_keccak_256 , seal_hash_sha2_256 , seal_random , } from "as-contract-runtime" ; import { toU8Array , typedToArray } from "../utils/ArrayUtils" ; import { ReadBuffer } from "./readbuffer" ; import { WriteBuffer } from "./writebuffer" ; function invoke_hash_algo ( message , outLen , fn ) { const wbuf = new WriteBuffer ( message ) ; const outbuf = new Uint8Array ( outLen ) ; fn ( wbuf . buffer , wbuf . size , outbuf . dataStart ) ; return Hash . bytesToHash ( typedToArray ( outbuf ) ) ; } export class Crypto { static sha256 < T extends Codec > ( obj ) { return invoke_hash_algo ( obj . toU8a ( ) , 32 , seal_hash_sha2_256 ) ; } static keccak256256 < T extends Codec > ( obj ) { return invoke_hash_algo ( obj . toU8a ( ) , 32 , seal_hash_keccak_256 ) ; } static blake256 < T extends Codec > ( obj ) { return invoke_hash_algo ( obj . toU8a ( ) , 32 , seal_hash_blake2_256 ) ; } static blake128 < T extends Codec > ( obj ) { return invoke_hash_algo ( obj . toU8a ( ) , 16 , seal_hash_blake2_128 ) ; } static random ( subject ) { const wbuf = new WriteBuffer ( subject ) ; const outbuf = new ReadBuffer ( 32 ) ; seal_random ( wbuf . buffer , wbuf . size , outbuf . valueBuffer , outbuf . sizeBuffer ) ; return Hash . bytesToHash ( outbuf . valueBytes ) ; } static sha256s ( message ) { return invoke_hash_algo ( toU8Array ( String . UTF8 . encode ( message ) ) , 32 , seal_hash_sha2_256 ) ; } static keccak256s ( message ) { return invoke_hash_algo ( toU8Array ( String . UTF8 . encode ( message ) ) , 32 , seal_hash_keccak_256 ) ; } static blake256s ( message ) { return invoke_hash_algo ( toU8Array ( String . UTF8 . encode ( message ) ) , 32 , seal_hash_blake2_256 ) ; } static blake128s ( message ) { return invoke_hash_algo ( toU8Array ( String . UTF8 . encode ( message ) ) , 16 , seal_hash_blake2_128 ) ; } }