dropbox / dropbox-api-content-hasher

Code to compute the Dropbox API's "content_hash"
Other
69 stars 27 forks source link

add this Go implementation! #8

Open varenc opened 6 years ago

varenc commented 6 years ago

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

greg-db commented 6 years ago

Thanks Chris!

hbbio commented 5 years ago

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
}
greg-db commented 5 years ago

Thanks for the note Henri!