package qrc import ( "crypto/keccak256" "crypto/sha256" ) // HashSize is the output size, in bytes, of both hash functions. const HashSize = 32 // Hash names the hash function a commit is built on. A commit uses its // function for everything: public keys, message digests and tree nodes. type Hash uint8 const ( // SHA256 is SHA-256 of FIPS 180-4, the SHA-2 family. SHA256 Hash = 1 // Keccak256 is Keccak-256 with the original padding, as used by the // EVM. It is the same sponge and permutation as SHA3-256 of FIPS 202 // and differs from it only in the padding byte. Keccak256 Hash = 2 ) // Valid reports whether h names a supported hash function. func (h Hash) Valid() bool { return h == SHA256 || h == Keccak256 } // String returns the name of the hash function. func (h Hash) String() string { switch h { case SHA256: return "sha256" case Keccak256: return "keccak256" } return "unknown" } // Sum returns the digest of data. It panics if h is not [Valid]. func (h Hash) Sum(data []byte) [HashSize]byte { return h.function()(data) } // function returns the function h names. Code that hashes in a loop fetches // it once instead of calling Sum, which looks it up every time. func (h Hash) function() func([]byte) [HashSize]byte { switch h { case SHA256: return sha256.Sum256 case Keccak256: return keccak256.Sum256 } panic("qrc: unknown hash function") }