2015-03-24 07:04:45 +00:00
|
|
|
package sha512
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-03-02 21:39:41 +00:00
|
|
|
"crypto"
|
2015-03-24 07:04:45 +00:00
|
|
|
"encoding/gob"
|
2015-05-22 01:44:08 +00:00
|
|
|
|
2017-03-02 21:39:41 +00:00
|
|
|
"github.com/stevvooe/resumable"
|
|
|
|
|
2015-05-22 01:44:08 +00:00
|
|
|
// import to ensure that our init function runs after the standard package
|
|
|
|
_ "crypto/sha512"
|
2015-03-24 07:04:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Len returns the number of bytes which have been written to the digest.
|
2015-05-21 06:44:08 +00:00
|
|
|
func (d *digest) Len() int64 {
|
|
|
|
return int64(d.len)
|
2015-03-24 07:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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{}{
|
2017-03-02 21:39:41 +00:00
|
|
|
d.h, d.x, d.nx, d.len, d.function,
|
2015-03-24 07:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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{}{
|
2017-03-02 21:39:41 +00:00
|
|
|
&d.h, &d.x, &d.nx, &d.len, &d.function,
|
2015-03-24 07:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, val := range vals {
|
|
|
|
if err := decoder.Decode(val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 21:39:41 +00:00
|
|
|
switch d.function {
|
|
|
|
case crypto.SHA384, crypto.SHA512, crypto.SHA512_224, crypto.SHA512_256:
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
return resumable.ErrBadState
|
|
|
|
}
|
|
|
|
|
2015-03-24 07:04:45 +00:00
|
|
|
return nil
|
|
|
|
}
|