Skip to content

Hash Reader / Writer that allows the calculation of the hash of the stream content

License

Notifications You must be signed in to change notification settings

gnewton/hashstream

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hashstream

Originally I wrote this to providce crypto hash Writer/Reader support for files too large to fit in memory or streaming. Inspired by Java's DigestInputStream and DigestInputStream

However, it was pointed out to me that (io.MultWriter)[http://golang.org/pkg/io/#MultiWriter] would be a better way to do this.

Yes, it was! :-)

So I have replaced my original code with an example of doing the same this with io.MultiWriter

Example

Can be found in main.go:

package main

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"io"
	"os"
	"strings"
)

const text0 = `
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit  
amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante 
hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet 
vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut 
libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, 
consectetur adipiscing elit. Aenean ut gravida lorem. Ut turpis felis, pulvinar a 
semper sed, adipiscing id dolor. Pellentesque auctor nisi id magna consequat sagittis. 
Curabitur dapibus enim sit amet elit pharetra tincidunt feugiat nisl imperdiet. Ut 
convallis libero in urna ultrices accumsan. Donec sed odio eros. Donec viverra mi quis 
quam pulvinar at malesuada arcu rhoncus. Cum sociis natoque penatibus et magnis dis 
parturient montes, nascetur ridiculus mus. In rutrum accumsan ultricies. Mauris vitae 
nisi at sem facilisis semper ac in est.
`

// From Linux command: md5sum (GNU coreutils) 8.22  on Fedora release 21 (Twenty One)
const md5Sum = "0ed8481c815f1977d7df85cf73e1c078"

func main() {
	reader := strings.NewReader(text0)

	fo, err := os.Create("out.bytes")
	if err != nil {
		panic(err)
	}

	hash := md5.New()
	writer := io.MultiWriter(fo, hash)

	writer := io.MultiWriter(fo, hash)
	if _, err := io.Copy(writer, reader) {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	fmt.Println(md5Sum)
	fmt.Println(hex.EncodeToString(hash.Sum(nil)))
}

Output:

0ed8481c815f1977d7df85cf73e1c078
0ed8481c815f1977d7df85cf73e1c078

#Variations Note that multiple hashes (should you need this) could be calculated at once, with something like:

	md5 := md5.New()
	sha256 := sha256.New()
	writer := io.MultiWriter(fo, md5, sha256)
	.
	.
	.
	fmt.Println(hex.EncodeToString(md5.Sum(nil)))
	fmt.Println(hex.EncodeToString(sha256.Sum(nil)))	

#License MIT license. See LICENSE file

Copyright 2015 Glen Newton

About

Hash Reader / Writer that allows the calculation of the hash of the stream content

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published