Commit bd926e1c authored by Roger Peppe's avatar Roger Peppe Committed by Brad Fitzpatrick

crypto, hash: document marshal/unmarshal implementation

Unless you go back and read the hash package documentation, it's
not clear that all the hash packages implement marshaling and
unmarshaling. Document the behaviour specifically in each package
that implements it as it this is hidden behaviour and easy to miss.

Change-Id: Id9d3508909362f1a3e53872d0319298359e50a94
Reviewed-on: https://go-review.googlesource.com/77251Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: 's avatarJoe Tsai <thebrokentoaster@gmail.com>
parent a158382b
...@@ -64,6 +64,9 @@ func (h *hmac) Reset() { ...@@ -64,6 +64,9 @@ func (h *hmac) Reset() {
} }
// New returns a new HMAC hash using the given hash.Hash type and key. // New returns a new HMAC hash using the given hash.Hash type and key.
// Note that unlike other hash implementations in the standard library,
// the returned Hash does not implement encoding.BinaryMarshaler
// or encoding.BinaryUnmarshaler.
func New(h func() hash.Hash, key []byte) hash.Hash { func New(h func() hash.Hash, key []byte) hash.Hash {
hm := new(hmac) hm := new(hmac)
hm.outer = h() hm.outer = h()
......
...@@ -123,7 +123,9 @@ func consumeUint32(b []byte) ([]byte, uint32) { ...@@ -123,7 +123,9 @@ func consumeUint32(b []byte) ([]byte, uint32) {
return b[4:], x return b[4:], x
} }
// New returns a new hash.Hash computing the MD5 checksum. // New returns a new hash.Hash computing the MD5 checksum. The Hash also
// implements encoding.BinaryMarshaler and encoding.BinaryUnmarshaler to
// marshal and unmarshal the internal state of the hash.
func New() hash.Hash { func New() hash.Hash {
d := new(digest) d := new(digest)
d.Reset() d.Reset()
......
...@@ -113,7 +113,9 @@ func (d *digest) Reset() { ...@@ -113,7 +113,9 @@ func (d *digest) Reset() {
d.len = 0 d.len = 0
} }
// New returns a new hash.Hash computing the SHA1 checksum. // New returns a new hash.Hash computing the SHA1 checksum. The Hash also
// implements encoding.BinaryMarshaler and encoding.BinaryUnmarshaler to
// marshal and unmarshal the internal state of the hash.
func New() hash.Hash { func New() hash.Hash {
d := new(digest) d := new(digest)
d.Reset() d.Reset()
......
...@@ -164,7 +164,10 @@ func (d *digest) Reset() { ...@@ -164,7 +164,10 @@ func (d *digest) Reset() {
d.len = 0 d.len = 0
} }
// New returns a new hash.Hash computing the SHA256 checksum. // New returns a new hash.Hash computing the SHA256 checksum. The Hash
// also implements encoding.BinaryMarshaler and
// encoding.BinaryUnmarshaler to marshal and unmarshal the internal
// state of the hash.
func New() hash.Hash { func New() hash.Hash {
d := new(digest) d := new(digest)
d.Reset() d.Reset()
......
...@@ -4,6 +4,10 @@ ...@@ -4,6 +4,10 @@
// Package sha512 implements the SHA-384, SHA-512, SHA-512/224, and SHA-512/256 // Package sha512 implements the SHA-384, SHA-512, SHA-512/224, and SHA-512/256
// hash algorithms as defined in FIPS 180-4. // hash algorithms as defined in FIPS 180-4.
//
// All the hash.Hash implementations returned by this package also
// implement encoding.BinaryMarshaler and encoding.BinaryUnmarshaler to
// marshal and unmarshal the internal state of the hash.
package sha512 package sha512
import ( import (
......
...@@ -35,8 +35,11 @@ type digest uint32 ...@@ -35,8 +35,11 @@ type digest uint32
func (d *digest) Reset() { *d = 1 } func (d *digest) Reset() { *d = 1 }
// New returns a new hash.Hash32 computing the Adler-32 checksum. // New returns a new hash.Hash32 computing the Adler-32 checksum. Its
// Its Sum method will lay the value out in big-endian byte order. // Sum method will lay the value out in big-endian byte order. The
// returned Hash32 also implements encoding.BinaryMarshaler and
// encoding.BinaryUnmarshaler to marshal and unmarshal the internal
// state of the hash.
func New() hash.Hash32 { func New() hash.Hash32 {
d := new(digest) d := new(digest)
d.Reset() d.Reset()
......
...@@ -139,9 +139,11 @@ type digest struct { ...@@ -139,9 +139,11 @@ type digest struct {
tab *Table tab *Table
} }
// New creates a new hash.Hash32 computing the CRC-32 checksum // New creates a new hash.Hash32 computing the CRC-32 checksum using the
// using the polynomial represented by the Table. // polynomial represented by the Table. Its Sum method will lay the
// Its Sum method will lay the value out in big-endian byte order. // value out in big-endian byte order. The returned Hash32 also
// implements encoding.BinaryMarshaler and encoding.BinaryUnmarshaler to
// marshal and unmarshal the internal state of the hash.
func New(tab *Table) hash.Hash32 { func New(tab *Table) hash.Hash32 {
if tab == IEEETable { if tab == IEEETable {
ieeeOnce.Do(ieeeInit) ieeeOnce.Do(ieeeInit)
...@@ -149,9 +151,11 @@ func New(tab *Table) hash.Hash32 { ...@@ -149,9 +151,11 @@ func New(tab *Table) hash.Hash32 {
return &digest{0, tab} return &digest{0, tab}
} }
// NewIEEE creates a new hash.Hash32 computing the CRC-32 checksum // NewIEEE creates a new hash.Hash32 computing the CRC-32 checksum using
// using the IEEE polynomial. // the IEEE polynomial. Its Sum method will lay the value out in
// Its Sum method will lay the value out in big-endian byte order. // big-endian byte order. The returned Hash32 also implements
// encoding.BinaryMarshaler and encoding.BinaryUnmarshaler to marshal
// and unmarshal the internal state of the hash.
func NewIEEE() hash.Hash32 { return New(IEEETable) } func NewIEEE() hash.Hash32 { return New(IEEETable) }
func (d *digest) Size() int { return Size } func (d *digest) Size() int { return Size }
......
...@@ -80,9 +80,11 @@ type digest struct { ...@@ -80,9 +80,11 @@ type digest struct {
tab *Table tab *Table
} }
// New creates a new hash.Hash64 computing the CRC-64 checksum // New creates a new hash.Hash64 computing the CRC-64 checksum using the
// using the polynomial represented by the Table. // polynomial represented by the Table. Its Sum method will lay the
// Its Sum method will lay the value out in big-endian byte order. // value out in big-endian byte order. The returned Hash64 also
// implements encoding.BinaryMarshaler and encoding.BinaryUnmarshaler to
// marshal and unmarshal the internal state of the hash.
func New(tab *Table) hash.Hash64 { return &digest{0, tab} } func New(tab *Table) hash.Hash64 { return &digest{0, tab} }
func (d *digest) Size() int { return Size } func (d *digest) Size() int { return Size }
......
...@@ -6,6 +6,10 @@ ...@@ -6,6 +6,10 @@
// created by Glenn Fowler, Landon Curt Noll, and Phong Vo. // created by Glenn Fowler, Landon Curt Noll, and Phong Vo.
// See // See
// https://en.wikipedia.org/wiki/Fowler-Noll-Vo_hash_function. // https://en.wikipedia.org/wiki/Fowler-Noll-Vo_hash_function.
//
// All the hash.Hash implementations returned by this package also
// implement encoding.BinaryMarshaler and encoding.BinaryUnmarshaler to
// marshal and unmarshal the internal state of the hash.
package fnv package fnv
import ( import (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment