hashes.go (2728B)
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 // New224 creates a new SHA3-224 hash. 16 // Its generic security strength is 224 bits against preimage attacks, 17 // and 112 bits against collision attacks. 18 func New224() hash.Hash { 19 if h := new224Asm(); h != nil { 20 return h 21 } 22 return &state{rate: 144, outputLen: 28, dsbyte: 0x06} 23 } 24 25 // New256 creates a new SHA3-256 hash. 26 // Its generic security strength is 256 bits against preimage attacks, 27 // and 128 bits against collision attacks. 28 func New256() hash.Hash { 29 if h := new256Asm(); h != nil { 30 return h 31 } 32 return &state{rate: 136, outputLen: 32, dsbyte: 0x06} 33 } 34 35 // New384 creates a new SHA3-384 hash. 36 // Its generic security strength is 384 bits against preimage attacks, 37 // and 192 bits against collision attacks. 38 func New384() hash.Hash { 39 if h := new384Asm(); h != nil { 40 return h 41 } 42 return &state{rate: 104, outputLen: 48, dsbyte: 0x06} 43 } 44 45 // New512 creates a new SHA3-512 hash. 46 // Its generic security strength is 512 bits against preimage attacks, 47 // and 256 bits against collision attacks. 48 func New512() hash.Hash { 49 if h := new512Asm(); h != nil { 50 return h 51 } 52 return &state{rate: 72, outputLen: 64, dsbyte: 0x06} 53 } 54 55 // NewLegacyKeccak256 creates a new Keccak-256 hash. 56 // 57 // Only use this function if you require compatibility with an existing cryptosystem 58 // that uses non-standard padding. All other users should use New256 instead. 59 func NewLegacyKeccak256() hash.Hash { return &state{rate: 136, outputLen: 32, dsbyte: 0x01} } 60 61 // NewLegacyKeccak512 creates a new Keccak-512 hash. 62 // 63 // Only use this function if you require compatibility with an existing cryptosystem 64 // that uses non-standard padding. All other users should use New512 instead. 65 func NewLegacyKeccak512() hash.Hash { return &state{rate: 72, outputLen: 64, dsbyte: 0x01} } 66 67 // Sum224 returns the SHA3-224 digest of the data. 68 func Sum224(data []byte) (digest [28]byte) { 69 h := New224() 70 h.Write(data) 71 h.Sum(digest[:0]) 72 return 73 } 74 75 // Sum256 returns the SHA3-256 digest of the data. 76 func Sum256(data []byte) (digest [32]byte) { 77 h := New256() 78 h.Write(data) 79 h.Sum(digest[:0]) 80 return 81 } 82 83 // Sum384 returns the SHA3-384 digest of the data. 84 func Sum384(data []byte) (digest [48]byte) { 85 h := New384() 86 h.Write(data) 87 h.Sum(digest[:0]) 88 return 89 } 90 91 // Sum512 returns the SHA3-512 digest of the data. 92 func Sum512(data []byte) (digest [64]byte) { 93 h := New512() 94 h.Write(data) 95 h.Sum(digest[:0]) 96 return 97 }