eee6cad2cf
The change relies on a refactor of the upstream resumable sha256/sha512 package that opts to register implementations with the standard library. This allows the resumable support to be detected where it matters, avoiding unnecessary and complex code. It also ensures that consumers of the digest package don't need to depend on the forked sha implementations. We also get an optimization with this change. If the size of data written to a digester is the same as the file size, we check to see if the digest has been verified. This works if the blob is written and committed in a single request. Signed-off-by: Stephen J Day <stephen.day@docker.com>
50 lines
1 KiB
Go
50 lines
1 KiB
Go
package sha512
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/gob"
|
|
)
|
|
|
|
// Len returns the number of bytes which have been written to the digest.
|
|
func (d *digest) Len() int64 {
|
|
return int64(d.len)
|
|
}
|
|
|
|
// State returns a snapshot of the state of the digest.
|
|
func (d *digest) State() ([]byte, error) {
|
|
var buf bytes.Buffer
|
|
encoder := gob.NewEncoder(&buf)
|
|
|
|
// We encode this way so that we do not have
|
|
// to export these fields of the digest struct.
|
|
vals := []interface{}{
|
|
d.h, d.x, d.nx, d.len, d.is384,
|
|
}
|
|
|
|
for _, val := range vals {
|
|
if err := encoder.Encode(val); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
// Restore resets the digest to the given state.
|
|
func (d *digest) Restore(state []byte) error {
|
|
decoder := gob.NewDecoder(bytes.NewReader(state))
|
|
|
|
// We decode this way so that we do not have
|
|
// to export these fields of the digest struct.
|
|
vals := []interface{}{
|
|
&d.h, &d.x, &d.nx, &d.len, &d.is384,
|
|
}
|
|
|
|
for _, val := range vals {
|
|
if err := decoder.Decode(val); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|