Source file src/vendor/golang.org/x/crypto/sha3/hashes.go

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

View as plain text