Open varenc opened 6 years ago
Thanks Chris!
FYI, https://github.com/tj/go-dropbox/blob/master/files.go has a shorter alternative
const hashBlockSize = 4 * 1024 * 1024
// ContentHash returns the Dropbox content_hash for a io.Reader.
// See https://www.dropbox.com/developers/reference/content-hash
func ContentHash(r io.Reader) (string, error) {
buf := make([]byte, hashBlockSize)
resultHash := sha256.New()
n, err := r.Read(buf)
if err != nil && err != io.EOF {
return "", err
}
if n > 0 {
bufHash := sha256.Sum256(buf[:n])
resultHash.Write(bufHash[:])
}
for n == hashBlockSize && err == nil {
n, err = r.Read(buf)
if err != nil && err != io.EOF {
return "", err
}
if n > 0 {
bufHash := sha256.Sum256(buf[:n])
resultHash.Write(bufHash[:])
}
}
return fmt.Sprintf("%x", resultHash.Sum(nil)), nil
}
Thanks for the note Henri!
https://github.com/ncw/rclone/blob/master/backend/dropbox/dbhash/dbhash.go
it implements the go hash interface! MIT license but still go to check with the creator