github.com/gochain-io/gochain@v2.2.26+incompatible/crypto/sha3/hashes.go (about) 1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package sha3 6 7 // This file provides functions for creating instances of the SHA-3 8 // and SHAKE hash functions, as well as utility functions for hashing 9 // bytes. 10 11 import ( 12 "hash" 13 ) 14 15 // NewKeccak256 creates a new Keccak-256 hash. 16 func NewKeccak256() hash.Hash { return &state{rate: 136, outputLen: 32, dsbyte: 0x01} } 17 18 // NewKeccak256SingleSum is like NewKeccak256, but the returned hash must be 19 // Reset() after calling Sum(). This allows skipping an internal state copy. 20 func NewKeccak256SingleSum() hash.Hash { 21 return &singleSumState{state{rate: 136, outputLen: 32, dsbyte: 0x01}} 22 } 23 24 // Keccak256 is optimized for cases that call Sum() just once. When h is 32 25 // bytes it is equivalent to calling NewKeccak256() and using the Write() and 26 // Sum() hash.Hash interface methods, but avoids the extra allocations in 27 // (*state).Sum(), which copies the internal state for continued use. This copy 28 // is unnecessary when the hash.Hash is discarded after calling Sum(). 29 func Keccak256(h []byte, data ...[]byte) { 30 s := &state{rate: 136, outputLen: 32, dsbyte: 0x01} 31 for _, b := range data { 32 s.Write(b) 33 } 34 s.Read(h) 35 } 36 37 // NewKeccak512 creates a new Keccak-512 hash. 38 func NewKeccak512() hash.Hash { return &state{rate: 72, outputLen: 64, dsbyte: 0x01} } 39 40 // NewKeccak512SingleSum returns an optimized instance, like NewKeccak256SingleSum(). 41 func NewKeccak512SingleSum() hash.Hash { 42 return &singleSumState{state{rate: 72, outputLen: 64, dsbyte: 0x01}} 43 } 44 45 // Keccack512 is an optimized alternative to NewKeccak512/Write/Sum, like Keccack256. 46 func Keccak512(data ...[]byte) []byte { 47 var h [64]byte 48 s := &state{rate: 72, outputLen: 64, dsbyte: 0x01} 49 for _, b := range data { 50 s.Write(b) 51 } 52 s.Read(h[:]) 53 return h[:] 54 } 55 56 // New224 creates a new SHA3-224 hash. 57 // Its generic security strength is 224 bits against preimage attacks, 58 // and 112 bits against collision attacks. 59 func New224() hash.Hash { return &state{rate: 144, outputLen: 28, dsbyte: 0x06} } 60 61 // New256 creates a new SHA3-256 hash. 62 // Its generic security strength is 256 bits against preimage attacks, 63 // and 128 bits against collision attacks. 64 func New256() hash.Hash { return &state{rate: 136, outputLen: 32, dsbyte: 0x06} } 65 66 // New384 creates a new SHA3-384 hash. 67 // Its generic security strength is 384 bits against preimage attacks, 68 // and 192 bits against collision attacks. 69 func New384() hash.Hash { return &state{rate: 104, outputLen: 48, dsbyte: 0x06} } 70 71 // New512 creates a new SHA3-512 hash. 72 // Its generic security strength is 512 bits against preimage attacks, 73 // and 256 bits against collision attacks. 74 func New512() hash.Hash { return &state{rate: 72, outputLen: 64, dsbyte: 0x06} } 75 76 // Sum224 returns the SHA3-224 digest of the data. 77 func Sum224(data []byte) (digest [28]byte) { 78 h := New224() 79 h.Write(data) 80 h.Sum(digest[:0]) 81 return 82 } 83 84 // Sum256 returns the SHA3-256 digest of the data. 85 func Sum256(data []byte) (digest [32]byte) { 86 h := New256() 87 h.Write(data) 88 h.Sum(digest[:0]) 89 return 90 } 91 92 // Sum384 returns the SHA3-384 digest of the data. 93 func Sum384(data []byte) (digest [48]byte) { 94 h := New384() 95 h.Write(data) 96 h.Sum(digest[:0]) 97 return 98 } 99 100 // Sum512 returns the SHA3-512 digest of the data. 101 func Sum512(data []byte) (digest [64]byte) { 102 h := New512() 103 h.Write(data) 104 h.Sum(digest[:0]) 105 return 106 }