Skip to content

Commit

Permalink
Merge pull request #34 from multiformats/feat/dbl-sha
Browse files Browse the repository at this point in the history
add a double sha256 hash method for supporting bitcoin
  • Loading branch information
whyrusleeping committed Nov 10, 2016
2 parents 5c64ea9 + afd20e6 commit 3922c53
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
41 changes: 23 additions & 18 deletions multihash.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,41 @@ const (
SHA3 = 0x14
BLAKE2B = 0x40
BLAKE2S = 0x41

DBL_SHA2_256 = 0x56
)

// Names maps the name of a hash to the code
var Names = map[string]int{
"sha1": SHA1,
"sha2-256": SHA2_256,
"sha2-512": SHA2_512,
"sha3": SHA3,
"blake2b": BLAKE2B,
"blake2s": BLAKE2S,
"sha1": SHA1,
"sha2-256": SHA2_256,
"sha2-512": SHA2_512,
"sha3": SHA3,
"blake2b": BLAKE2B,
"blake2s": BLAKE2S,
"dbl-sha2-256": DBL_SHA2_256,
}

// Codes maps a hash code to it's name
var Codes = map[int]string{
SHA1: "sha1",
SHA2_256: "sha2-256",
SHA2_512: "sha2-512",
SHA3: "sha3",
BLAKE2B: "blake2b",
BLAKE2S: "blake2s",
SHA1: "sha1",
SHA2_256: "sha2-256",
SHA2_512: "sha2-512",
SHA3: "sha3",
BLAKE2B: "blake2b",
BLAKE2S: "blake2s",
DBL_SHA2_256: "dbl-sha2-256",
}

// DefaultLengths maps a hash code to it's default length
var DefaultLengths = map[int]int{
SHA1: 20,
SHA2_256: 32,
SHA2_512: 64,
SHA3: 64,
BLAKE2B: 64,
BLAKE2S: 32,
SHA1: 20,
SHA2_256: 32,
SHA2_512: 64,
SHA3: 64,
BLAKE2B: 64,
BLAKE2S: 32,
DBL_SHA2_256: 32,
}

type DecodedMultihash struct {
Expand Down
1 change: 1 addition & 0 deletions multihash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var tCodes = map[int]string{
0x14: "sha3",
0x40: "blake2b",
0x41: "blake2s",
0x56: "dbl-sha2-256",
}

type TestCase struct {
Expand Down
2 changes: 2 additions & 0 deletions sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func Sum(data []byte, code int, length int) (Multihash, error) {
d = sumSHA512(data)
case SHA3:
d, err = sumSHA3(data)
case DBL_SHA2_256:
d = sumSHA256(sumSHA256(data))
default:
return m, ErrSumNotSupported
}
Expand Down
1 change: 1 addition & 0 deletions sum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var sumTestCases = []SumTestCase{
SumTestCase{SHA2_512, 32, "foo", "1320f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc663832"},
SumTestCase{SHA3, -1, "foo", "14404bca2b137edc580fe50a88983ef860ebaca36c857b1f492839d6d7392452a63c82cbebc68e3b70a2a1480b4bb5d437a7cba6ecf9d89f9ff3ccd14cd6146ea7e7"},
SumTestCase{SHA3, 32, "foo", "14204bca2b137edc580fe50a88983ef860ebaca36c857b1f492839d6d7392452a63c"},
SumTestCase{DBL_SHA2_256, 32, "foo", "5620c7ade88fc7a21498a6a5e5c385e1f68bed822b72aa63c4a9a48a02c2466ee29e"},
}

func TestSum(t *testing.T) {
Expand Down

0 comments on commit 3922c53

Please sign in to comment.