dropbox-api-content-hasher icon indicating copy to clipboard operation
dropbox-api-content-hasher copied to clipboard

add this Go implementation!

Open varenc opened this issue 7 years ago • 3 comments

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

varenc avatar Mar 21 '18 00:03 varenc

Thanks Chris!

greg-db avatar Mar 21 '18 12:03 greg-db

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
}

hbbio avatar Feb 11 '19 16:02 hbbio

Thanks for the note Henri!

greg-db avatar Feb 11 '19 16:02 greg-db