LEFT | RIGHT |
1 // Copyright 2014 The Go Authors. All rights reserved. | 1 // Copyright 2014 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package sha3 | 5 package sha3 |
6 | 6 |
7 // This file defines the ShakeHash interface, and provides | 7 // This file defines the ShakeHash interface, and provides |
8 // functions for creating SHAKE instances, as well as utility | 8 // functions for creating SHAKE instances, as well as utility |
9 // functions for hashing bytes to arbitrary-length output. | 9 // functions for hashing bytes to arbitrary-length output. |
10 | 10 |
11 import ( | 11 import ( |
12 "io" | 12 "io" |
13 ) | 13 ) |
14 | 14 |
15 // Shake defines the interface to hash functions that | 15 // ShakeHash defines the interface to hash functions that |
16 // support arbitrary-length output. | 16 // support arbitrary-length output. |
17 type ShakeHash interface { | 17 type ShakeHash interface { |
18 » // Write absorbs more data into the hash's state. | 18 » // Write absorbs more data into the hash's state. It returns an |
19 » // It never returns an error. | 19 » // error is input is written to it after output has been read from |
| 20 » // it. |
20 io.Writer | 21 io.Writer |
21 // Read reads more output from the hash; reading affects the hash's | 22 // Read reads more output from the hash; reading affects the hash's |
22 » // state. (Shake.Read is thus very different from Hash.Sum) | 23 » // state. (ShakeHash.Read is thus very different from Hash.Sum) |
23 // It never returns an error. | 24 // It never returns an error. |
24 io.Reader | 25 io.Reader |
25 | 26 |
26 » // Barrier overwrites (200 - rate) bytes of the hash's state | 27 » // Clone returns a copy of the ShakeHash in its current state. |
27 » // with zeros, making it computationally infeasible to recover | 28 » Clone() ShakeHash |
28 » // previous inputs. | |
29 » Barrier() | |
30 | 29 |
31 » // Reset resets the Hash to its initial state. | 30 » // Reset resets the ShakeHash to its initial state. |
32 Reset() | 31 Reset() |
33 } | 32 } |
34 | 33 |
35 // NewShake128 creates a new SHAKE128 variable-output-length Sponge. | 34 // Clone returns a copy of the ShakeHash. |
| 35 func (d *state) Clone() ShakeHash { |
| 36 » // Make a copy of the original hash so that caller can keep writing |
| 37 » // and summing. |
| 38 » dup := *d |
| 39 » return &dup |
| 40 } |
| 41 |
| 42 // NewShake128 creates a new SHAKE128 variable-output-length ShakeHash. |
36 // Its generic security strength is 128 bits against all attacks if at | 43 // Its generic security strength is 128 bits against all attacks if at |
37 // least 32 bytes of its output are used. | 44 // least 32 bytes of its output are used. |
38 func NewShake128() ShakeHash { return &state{rate: 168, dsbyte: 0x1f} } | 45 func NewShake128() ShakeHash { return &state{rate: 168, dsbyte: 0x1f} } |
39 | 46 |
40 // NewShake256 creates a new SHAKE128 variable-output-length Sponge. | 47 // NewShake256 creates a new SHAKE128 variable-output-length ShakeHash. |
41 // Its generic security strength is 256 bits against all attacks if | 48 // Its generic security strength is 256 bits against all attacks if |
42 // at least 64 bytes of its output are used. | 49 // at least 64 bytes of its output are used. |
43 func NewShake256() ShakeHash { return &state{rate: 136, dsbyte: 0x1f} } | 50 func NewShake256() ShakeHash { return &state{rate: 136, dsbyte: 0x1f} } |
44 | 51 |
45 // ShakeSum128 writes an arbitrary-length digest of data into hash. | 52 // ShakeSum128 writes an arbitrary-length digest of data into hash. |
46 func ShakeSum128(hash, data []byte) { | 53 func ShakeSum128(hash, data []byte) { |
47 h := NewShake128() | 54 h := NewShake128() |
48 h.Write(data) | 55 h.Write(data) |
49 h.Read(hash) | 56 h.Read(hash) |
50 } | 57 } |
51 | 58 |
52 // ShakeSum256 writes an arbitrary-length digest of data into hash. | 59 // ShakeSum256 writes an arbitrary-length digest of data into hash. |
53 func ShakeSum256(hash, data []byte) { | 60 func ShakeSum256(hash, data []byte) { |
54 h := NewShake256() | 61 h := NewShake256() |
55 h.Write(data) | 62 h.Write(data) |
56 h.Read(hash) | 63 h.Read(hash) |
57 } | 64 } |
LEFT | RIGHT |