github.com/turingchain2020/turingchain@v1.1.21/common/crypto/sha3/hashes.go (about)

     1  // Copyright Turing Corp. 2018 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  // Copyright 2014 The Go Authors. All rights reserved.
     6  // Use of this source code is governed by a BSD-style
     7  // license that can be found in the LICENSE file.
     8  
     9  package sha3
    10  
    11  // This file provides functions for creating instances of the SHA-3
    12  // and SHAKE hash functions, as well as utility functions for hashing
    13  // bytes.
    14  
    15  import (
    16  	"hash"
    17  )
    18  
    19  // NewKeccak256 creates a new Keccak-256 hash.
    20  func NewKeccak256() hash.Hash { return &state{rate: 136, outputLen: 32, dsbyte: 0x01} }
    21  
    22  // NewKeccak512 creates a new Keccak-512 hash.
    23  func NewKeccak512() hash.Hash { return &state{rate: 72, outputLen: 64, dsbyte: 0x01} }
    24  
    25  // New224 creates a new SHA3-224 hash.
    26  // Its generic security strength is 224 bits against preimage attacks,
    27  // and 112 bits against collision attacks.
    28  func New224() hash.Hash { return &state{rate: 144, outputLen: 28, dsbyte: 0x06} }
    29  
    30  // New256 creates a new SHA3-256 hash.
    31  // Its generic security strength is 256 bits against preimage attacks,
    32  // and 128 bits against collision attacks.
    33  func New256() hash.Hash { return &state{rate: 136, outputLen: 32, dsbyte: 0x06} }
    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 { return &state{rate: 104, outputLen: 48, dsbyte: 0x06} }
    39  
    40  // New512 creates a new SHA3-512 hash.
    41  // Its generic security strength is 512 bits against preimage attacks,
    42  // and 256 bits against collision attacks.
    43  func New512() hash.Hash { return &state{rate: 72, outputLen: 64, dsbyte: 0x06} }
    44  
    45  // NewLegacyKeccak256 creates a new Keccak-256 hash.
    46  //
    47  // Only use this function if you require compatibility with an existing cryptosystem
    48  // that uses non-standard padding. All other users should use New256 instead.
    49  func NewLegacyKeccak256() hash.Hash { return &state{rate: 136, outputLen: 32, dsbyte: 0x01} }
    50  
    51  // Sum224 returns the SHA3-224 digest of the data.
    52  func Sum224(data []byte) (digest [28]byte) {
    53  	h := New224()
    54  	h.Write(data)
    55  	h.Sum(digest[:0])
    56  	return
    57  }
    58  
    59  // Sum256 returns the SHA3-256 digest of the data.
    60  func Sum256(data []byte) (digest [32]byte) {
    61  	h := New256()
    62  	h.Write(data)
    63  	h.Sum(digest[:0])
    64  	return
    65  }
    66  
    67  // Sum384 returns the SHA3-384 digest of the data.
    68  func Sum384(data []byte) (digest [48]byte) {
    69  	h := New384()
    70  	h.Write(data)
    71  	h.Sum(digest[:0])
    72  	return
    73  }
    74  
    75  // Sum512 returns the SHA3-512 digest of the data.
    76  func Sum512(data []byte) (digest [64]byte) {
    77  	h := New512()
    78  	h.Write(data)
    79  	h.Sum(digest[:0])
    80  	return
    81  }
    82  
    83  // KeccakSum256 returns the Keccak-256 digest of the data.
    84  func KeccakSum256(data []byte) (digest [32]byte) {
    85  	h := NewKeccak256()
    86  	h.Write(data)
    87  	h.Sum(digest[:0])
    88  	return
    89  }
    90  
    91  // KeccakSum512 returns the Keccak-512 digest of the data.
    92  func KeccakSum512(data []byte) (digest [32]byte) {
    93  	h := NewKeccak512()
    94  	h.Write(data)
    95  	h.Sum(digest[:0])
    96  	return
    97  }