Adding minio-hash with streaming crypto hashes

This commit is contained in:
Frederick F. Kautz IV
2014-12-21 13:01:27 +13:00
parent aa6cd015fa
commit 2278df9910
11 changed files with 333 additions and 2 deletions

View File

@@ -17,6 +17,10 @@ package sha256
// #define SHA256_H6 0x1f83d9abUL
// #define SHA256_H7 0x5be0cd19UL
import "C"
import (
gosha256 "crypto/sha256"
"io"
)
/*
func Sha256(buffer []byte) ([]uint32, error) {
@@ -37,3 +41,19 @@ func Sha256(buffer []byte) ([]uint32, error) {
}
}
*/
func Sum(reader io.Reader) ([]byte, error) {
hash := gosha256.New()
var err error
for err == nil {
length := 0
byteBuffer := make([]byte, 1024*1024)
length, err = reader.Read(byteBuffer)
byteBuffer = byteBuffer[0:length]
hash.Write(byteBuffer)
}
if err != io.EOF {
return nil, err
}
return hash.Sum(nil), nil
}

View File

@@ -0,0 +1,23 @@
package sha256
import (
"bytes"
"encoding/hex"
"testing"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
func (s *MySuite) TestSha256Stream(c *C) {
testString := []byte("Test string")
expectedHash, _ := hex.DecodeString("a3e49d843df13c2e2a7786f6ecd7e0d184f45d718d1ac1a8a63e570466e489dd")
hash, err := Sum(bytes.NewBuffer(testString))
c.Assert(err, IsNil)
c.Assert(bytes.Equal(expectedHash, hash), Equals, true)
}