-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
2,911 additions
and
1,240 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
Oops, something went wrong.