Skip to content

Commit

Permalink
Merge pull request #63 from multiformats/feat/fast-hash
Browse files Browse the repository at this point in the history
use faster sha256 and blake2b implementations
  • Loading branch information
Stebalien authored Dec 17, 2017
2 parents 9f612d2 + eddd14f commit 11a0a5f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
"hash": "QmfJHywXQu98UeZtGJBQrPAR6AtmDjjbe3qjTo9piXHPnx",
"name": "murmur3",
"version": "0.0.0"
},
{
"author": "minio",
"hash": "QmXTpwq2AkzQsPjKqFQDNY2bMdsAT53hUBETeyj8QRHTZU",
"name": "sha256-simd",
"version": "0.1.1"
},
{
"author": "minio",
"hash": "QmZp3eKdYQHHAneECmeK6HhiMwTPufmjC8DuuaGKv3unvx",
"name": "blake2b-simd",
"version": "0.1.1"
}
],
"gxVersion": "0.9.0",
Expand Down
39 changes: 30 additions & 9 deletions sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package multihash

import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"errors"
"fmt"

"github.com/spaolacci/murmur3"
blake2b "golang.org/x/crypto/blake2b"
blake2b "github.com/minio/blake2b-simd"
sha256 "github.com/minio/sha256-simd"
murmur3 "github.com/spaolacci/murmur3"
blake2s "golang.org/x/crypto/blake2s"
sha3 "golang.org/x/crypto/sha3"
keccak "leb.io/hashland/keccakpg"
Expand Down Expand Up @@ -50,14 +50,11 @@ func Sum(data []byte, code uint64, length int) (Multihash, error) {
olen := code - BLAKE2B_MIN + 1
switch olen {
case 32:
out := blake2b.Sum256(data)
d = out[:]
d = sumBlake2b256(data)
case 48:
out := blake2b.Sum384(data)
d = out[:]
d = sumBlake2b384(data)
case 64:
out := blake2b.Sum512(data)
d = out[:]
d = sumBlake2b512(data)
default:
return nil, fmt.Errorf("unsupported length for blake2b: %d", olen)
}
Expand Down Expand Up @@ -115,6 +112,30 @@ func isBlake2b(code uint64) bool {
return code >= BLAKE2B_MIN && code <= BLAKE2B_MAX
}

func sumBlake2b256(data []byte) []byte {
out := blake2b.Sum256(data)
return out[:]
}

var blake2b384Config = &blake2b.Config{Size: 384 / 8}

func sumBlake2b384(data []byte) []byte {
hasher, err := blake2b.New(blake2b384Config)
if err != nil {
panic(err)
}
if _, err := hasher.Write(data); err != nil {
panic(err)
}
return hasher.Sum(nil)

}

func sumBlake2b512(data []byte) []byte {
out := blake2b.Sum512(data)
return out[:]
}

func sumID(data []byte) []byte {
return data
}
Expand Down

0 comments on commit 11a0a5f

Please sign in to comment.