| 1 |
// Copyright 2009 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 sha512 implements the SHA-384, SHA-512, SHA-512/224, and SHA-512/256 |
| 6 |
// hash algorithms as defined in FIPS 180-4. |
| 7 |
// |
| 8 |
// All the hash.Hash implementations returned by this package also |
| 9 |
// implement encoding.BinaryMarshaler and encoding.BinaryUnmarshaler to |
| 10 |
// marshal and unmarshal the internal state of the hash. |
| 11 |
package sha512 |
| 12 |
|
| 13 |
import ( |
| 14 |
"crypto" |
| 15 |
"crypto/internal/boring" |
| 16 |
"crypto/internal/fips140/sha512" |
| 17 |
"hash" |
| 18 |
) |
| 19 |
|
| 20 |
func init() { |
| 21 |
crypto.RegisterHash(crypto.SHA384, New384) |
| 22 |
crypto.RegisterHash(crypto.SHA512, New) |
| 23 |
crypto.RegisterHash(crypto.SHA512_224, New512_224) |
| 24 |
crypto.RegisterHash(crypto.SHA512_256, New512_256) |
| 25 |
} |
| 26 |
|
| 27 |
const ( |
| 28 |
// Size is the size, in bytes, of a SHA-512 checksum. |
| 29 |
Size = 64 |
| 30 |
|
| 31 |
// Size224 is the size, in bytes, of a SHA-512/224 checksum. |
| 32 |
Size224 = 28 |
| 33 |
|
| 34 |
// Size256 is the size, in bytes, of a SHA-512/256 checksum. |
| 35 |
Size256 = 32 |
| 36 |
|
| 37 |
// Size384 is the size, in bytes, of a SHA-384 checksum. |
| 38 |
Size384 = 48 |
| 39 |
|
| 40 |
// BlockSize is the block size, in bytes, of the SHA-512/224, |
| 41 |
// SHA-512/256, SHA-384 and SHA-512 hash functions. |
| 42 |
BlockSize = 128 |
| 43 |
) |
| 44 |
|
| 45 |
// New returns a new [hash.Hash] computing the SHA-512 checksum. The Hash |
| 46 |
// also implements [encoding.BinaryMarshaler], [encoding.BinaryAppender] and |
| 47 |
// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal |
| 48 |
// state of the hash. |
| 49 |
func New() hash.Hash { |
| 50 |
if boring.Enabled { |
| 51 |
return boring.NewSHA512() |
| 52 |
} |
| 53 |
return sha512.New() |
| 54 |
} |
| 55 |
|
| 56 |
// New512_224 returns a new [hash.Hash] computing the SHA-512/224 checksum. The Hash |
| 57 |
// also implements [encoding.BinaryMarshaler], [encoding.BinaryAppender] and |
| 58 |
// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal |
| 59 |
// state of the hash. |
| 60 |
func New512_224() hash.Hash { |
| 61 |
return sha512.New512_224() |
| 62 |
} |
| 63 |
|
| 64 |
// New512_256 returns a new [hash.Hash] computing the SHA-512/256 checksum. The Hash |
| 65 |
// also implements [encoding.BinaryMarshaler], [encoding.BinaryAppender] and |
| 66 |
// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal |
| 67 |
// state of the hash. |
| 68 |
func New512_256() hash.Hash { |
| 69 |
return sha512.New512_256() |
| 70 |
} |
| 71 |
|
| 72 |
// New384 returns a new [hash.Hash] computing the SHA-384 checksum. The Hash |
| 73 |
// also implements [encoding.BinaryMarshaler], [encoding.BinaryAppender] and |
| 74 |
// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal |
| 75 |
// state of the hash. |
| 76 |
func New384() hash.Hash { |
| 77 |
if boring.Enabled { |
| 78 |
return boring.NewSHA384() |
| 79 |
} |
| 80 |
return sha512.New384() |
| 81 |
} |
| 82 |
|
| 83 |
// Sum512 returns the SHA512 checksum of the data. |
| 84 |
func Sum512(data []byte) [Size]byte { |
| 85 |
if boring.Enabled { |
| 86 |
return boring.SHA512(data) |
| 87 |
} |
| 88 |
h := New() |
| 89 |
h.Write(data) |
| 90 |
var sum [Size]byte |
| 91 |
h.Sum(sum[:0]) |
| 92 |
return sum |
| 93 |
} |
| 94 |
|
| 95 |
// Sum384 returns the SHA384 checksum of the data. |
| 96 |
func Sum384(data []byte) [Size384]byte { |
| 97 |
if boring.Enabled { |
| 98 |
return boring.SHA384(data) |
| 99 |
} |
| 100 |
h := New384() |
| 101 |
h.Write(data) |
| 102 |
var sum [Size384]byte |
| 103 |
h.Sum(sum[:0]) |
| 104 |
return sum |
| 105 |
} |
| 106 |
|
| 107 |
// Sum512_224 returns the Sum512/224 checksum of the data. |
| 108 |
func Sum512_224(data []byte) [Size224]byte { |
| 109 |
h := New512_224() |
| 110 |
h.Write(data) |
| 111 |
var sum [Size224]byte |
| 112 |
h.Sum(sum[:0]) |
| 113 |
return sum |
| 114 |
} |
| 115 |
|
| 116 |
// Sum512_256 returns the Sum512/256 checksum of the data. |
| 117 |
func Sum512_256(data []byte) [Size256]byte { |
| 118 |
h := New512_256() |
| 119 |
h.Write(data) |
| 120 |
var sum [Size256]byte |
| 121 |
h.Sum(sum[:0]) |
| 122 |
return sum |
| 123 |
} |