dchash

package module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2022 License: MIT Imports: 3 Imported by: 0

README

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
}

Documentation

Overview

Package dchash implements a configurable variation of Dropbox's Content Hash.

Index

Constants

View Source
const (
	// DefaultHash denotes the default Hash.
	DefaultHash = crypto.SHA256

	// DefaultBlockSize denotes the default block size.
	DefaultBlockSize = 1 << 22
)

Variables

This section is empty.

Functions

func New

func New(newFunc func() hash.Hash, blockSize int) hash.Hash

New returns a Hash which yields checksums according to Dropbox's Content Hash algorithm, more details on which may be found here:

https://www.dropbox.com/developers/reference/content-hash

Should newFunc be nil, DefaultHash.New will be used in its place.

Should blockSize be less than 1, DefaultBlockSize will be used instead.

Hashes returned by New are NOT safe for concurrent use by multiple goroutines.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL