Skip to content

Commit

Permalink
Revamp
Browse files Browse the repository at this point in the history
  • Loading branch information
jedisct1 committed Apr 4, 2018
1 parent 6381135 commit 63ae3e8
Show file tree
Hide file tree
Showing 24 changed files with 2,911 additions and 1,240 deletions.
15 changes: 15 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"


[[constraint]]
branch = "master"
name = "golang.org/x/crypto"
78 changes: 78 additions & 0 deletions progressiveHash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package progressiveHash

import (
"errors"
"fmt"

"golang.org/x/crypto/blake2b"
)

// Hash - Hash `in` with personalisation `personal`, an initial number of rounds `initialRounds`,
// and increase the work factor `progressiveLength` times to eventually produce an `outLength` bit-long digest.
func Hash(in []byte, personal []byte, initialRounds uint64, progressiveLength int, outLength int) ([]byte, error) {
return hash(in, personal, initialRounds, progressiveLength, outLength, nil)
}

// Verify - Verify a previously computed hash `expected` using the input `in`, a personalisation `personal`,
// increasing the work factor `progressiveLength` times to match the initial hash length `outLength` bits.
func Verify(in []byte, personal []byte, initialRounds uint64, progressiveLength int, outLength int, expected []byte) error {
_, err := hash(in, personal, initialRounds, progressiveLength, outLength, &expected)
return err
}

func hash(in []byte, personal []byte, initialRounds uint64, progressiveLength int, outLength int, expected *[]byte) ([]byte, error) {
if progressiveLength < 0 || outLength < 1 {
return nil, errors.New("invalid output length")
}
if len(personal) > 127 {
return nil, errors.New("personalization too long")
}
if outLength < progressiveLength {
outLength = progressiveLength
}
xin := make([]byte, 128+len(in))
copy(xin, personal)
copy(xin[128:], in)
h := blake2b.Sum512(xin)
var out []byte
if expected != nil {
if len(*expected) != outLength>>3 {
return out, errors.New("expected length not matching the given parameters")
}
} else {
out = make([]byte, outLength>>3)
if len(out) > len(h) {
return nil, errors.New("output too long")
}
}
xrounds := initialRounds
i := uint(0)
for ; i < uint(progressiveLength); i++ {
fmt.Println(i)
for j := uint64(0); j < xrounds; j++ {
h = blake2b.Sum512(h[:])
}
if expected != nil {
if ((*expected)[i>>3]>>(i&7))&1 != uint8(h[0]&1) {
return out, errors.New("mismatch")
}
} else {
out[i>>3] |= uint8(h[0]&1) << (i & 7)
}
xrounds *= 2
}
if expected != nil {
ck := uint8(0)
for ; i < uint(outLength); i++ {
ck |= ((*expected)[i>>3]>>(i&7))&1 ^ uint8((h[i>>3]>>(i&7))&1)
}
if ck != 0 {
return out, errors.New("mismatch")
}
} else {
for ; i < uint(outLength); i++ {
out[i>>3] |= uint8((h[i>>3]>>(i&7))&1) << (i & 7)
}
}
return out, nil
}
23 changes: 23 additions & 0 deletions progressiveHash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package progressiveHash

import "testing"

func testHash(t *testing.T) {
personalisation := []byte("test application")
in := []byte("input string")

h, err := Hash(in, personalisation, 50000, 8, 256)
if err != nil {
panic(err)
}

err = Verify(in, personalisation, 50000, 8, 256, h)
if err != nil {
panic(err)
}

err = Verify(in, personalisation, 10000, 8, 256, h)
if err != nil {
panic("verification shouldn't have passed")
}
}
Loading

0 comments on commit 63ae3e8

Please sign in to comment.