2022-02-16 16:16:29 +05:00
|
|
|
package zkv
|
2022-02-16 16:08:20 +05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
2022-02-16 16:38:50 +05:00
|
|
|
"encoding/gob"
|
2022-02-16 16:08:20 +05:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
func encode(value interface{}) ([]byte, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
2022-02-16 16:38:50 +05:00
|
|
|
err := gob.NewEncoder(buf).Encode(value)
|
2022-02-16 16:08:20 +05:00
|
|
|
return buf.Bytes(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func decode(b []byte, value interface{}) error {
|
2022-02-16 16:38:50 +05:00
|
|
|
return gob.NewDecoder(bytes.NewReader(b)).Decode(value)
|
2022-02-16 16:08:20 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
func hashInterface(value interface{}) ([]byte, error) {
|
|
|
|
valueBytes, err := encode(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashBytes(valueBytes), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hashBytes(b []byte) []byte {
|
|
|
|
bytes := sha256.Sum224(b)
|
|
|
|
|
|
|
|
return bytes[:]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func skip(r io.Reader, count int64) (err error) {
|
|
|
|
switch r := r.(type) {
|
|
|
|
case io.Seeker:
|
|
|
|
_, err = r.Seek(count, io.SeekCurrent)
|
|
|
|
default:
|
2022-12-02 20:32:09 +05:00
|
|
|
_, err = io.CopyN(io.Discard, r, count)
|
2022-02-16 16:08:20 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|