Skip to content

Latest commit

 

History

History
47 lines (35 loc) · 1.3 KB

README.md

File metadata and controls

47 lines (35 loc) · 1.3 KB

Build Status Coverage Report Go Reference

dchash

Package dchash implements a configurable variation of Dropbox's Content Hash algorithm in idiomatic Go.

Usage

package checksum

import (
	"crypto/sha512"
	"io"

	"github.com/azazeal/dchash"
)

// Dropbox hashes the data in src as per Dropbox's Content Hash algorithm, using
// SHA256 and over blocks of 4 MiB in size.
func Dropbox(src io.Reader) (sum []byte, err error) {
	h := dchash.New(nil, -1)

	if _, err = io.Copy(h, src); err == nil {
		sum = h.Sum(nil)
	}

	return
}

// SHA512m1 hashes the data in src as per Dropbox's Content Hash algorithm but
// with SHA512 (instead of SHA256) and over blocks of 1 (instead  of 4) MiB in
// size.
func SHA512m1(src io.Reader) (sum []byte, err error) {
	h := dchash.New(sha512.New, 1<<20)

	if _, err = io.Copy(h, src); err == nil {
		sum = h.Sum(nil)
	}

	return
}