Skip to content

Commit

Permalink
use faster sha256 and blake2b implementations
Browse files Browse the repository at this point in the history
fixes #61
  • Loading branch information
Stebalien committed Dec 17, 2017
1 parent 9f612d2 commit db7fbde
Show file tree
Hide file tree
Showing 2 changed files with 44 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": "QmYmZHLQ4ctyTv84Qfrw39jWzSWvbjo8cb6rkb6oWwzC6T",
"name": "sha256-simd",
"version": "0.1.0"
},
{
"author": "minio",
"hash": "QmctbhjPfFUCFWemafc7LGXUKJuoRm8baJaiQEELMgbUFV",
"name": "blake2b-simd",
"version": "0.1.0"
}
],
"gxVersion": "0.9.0",
Expand Down
41 changes: 32 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,32 @@ func isBlake2b(code uint64) bool {
return code >= BLAKE2B_MIN && code <= BLAKE2B_MAX
}

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

const blake2b384Size = 384 / 8

var blake2b384Config = &blake2b.Config{Size: blake2b384Size}

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

}

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

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

0 comments on commit db7fbde

Please sign in to comment.