Search Apps Documentation Source Content File Folder Download Copy Actions Download

hashes.gno

1.07 Kb ยท 41 lines
 1package keccak256
 2
 3// This file provides functions for creating instances of the SHA-3
 4// and SHAKE hash functions, as well as utility functions for hashing
 5// bytes.
 6
 7import (
 8	"hash"
 9)
10
11const (
12	dsbyteSHA3   = 0b00000110
13	dsbyteKeccak = 0b00000001
14	dsbyteShake  = 0b00011111
15	dsbyteCShake = 0b00000100
16
17	// rateK[c] is the rate in bytes for Keccak[c] where c is the capacity in
18	// bits. Given the sponge size is 1600 bits, the rate is 1600 - c bits.
19	rateK256  = (1600 - 256) / 8
20	rateK448  = (1600 - 448) / 8
21	rateK512  = (1600 - 512) / 8
22	rateK768  = (1600 - 768) / 8
23	rateK1024 = (1600 - 1024) / 8
24)
25
26// NewLegacyKeccak256 creates a new Keccak-256 hash.
27//
28// Only use this function if you require compatibility with an existing cryptosystem
29// that uses non-standard padding. All other users should use New256 instead.
30func NewLegacyKeccak256() hash.Hash {
31	return &state{rate: rateK512, outputLen: 32, dsbyte: dsbyteKeccak}
32}
33
34func Hash(data []byte) [32]byte {
35	var res [32]byte
36	keccak256 := NewLegacyKeccak256()
37	keccak256.Write(data)
38	copy(res[:], keccak256.Sum(nil))
39
40	return res
41}