Skip to content

Commit

Permalink
Merge pull request #51 from JustinDrake/master
Browse files Browse the repository at this point in the history
Add identity
  • Loading branch information
Lars Gierth authored May 10, 2017
2 parents 625115a + 16d3eac commit 7aa9f26
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions multihash.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (e ErrInconsistentLen) Error() string {

// constants
const (
ID = 0x00
SHA1 = 0x11
SHA2_256 = 0x12
SHA2_512 = 0x13
Expand Down Expand Up @@ -84,6 +85,7 @@ func init() {

// Names maps the name of a hash to the code
var Names = map[string]uint64{
"id": ID,
"sha1": SHA1,
"sha2-256": SHA2_256,
"sha2-512": SHA2_512,
Expand All @@ -104,6 +106,7 @@ var Names = map[string]uint64{

// Codes maps a hash code to it's name
var Codes = map[uint64]string{
ID: "id",
SHA1: "sha1",
SHA2_256: "sha2-256",
SHA2_512: "sha2-512",
Expand All @@ -123,6 +126,7 @@ var Codes = map[uint64]string{

// DefaultLengths maps a hash code to it's default length
var DefaultLengths = map[uint64]int{
ID: 32,
SHA1: 20,
SHA2_256: 32,
SHA2_512: 64,
Expand Down
2 changes: 2 additions & 0 deletions multihash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
// maybe silly, but makes it so changing
// the table accidentally has to happen twice.
var tCodes = map[uint64]string{
0x00: "id",
0x11: "sha1",
0x12: "sha2-256",
0x13: "sha2-512",
Expand All @@ -35,6 +36,7 @@ type TestCase struct {
}

var testCases = []TestCase{
TestCase{"2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae", 0x00, "id"},
TestCase{"0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33", 0x11, "sha1"},
TestCase{"0beec7b5", 0x11, "sha1"},
TestCase{"2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae", 0x12, "sha2-256"},
Expand Down
6 changes: 6 additions & 0 deletions sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func Sum(data []byte, code uint64, length int) (Multihash, error) {
}
default:
switch code {
case ID:
d = sumID(data)
case SHA1:
d = sumSHA1(data)
case SHA2_256:
Expand Down Expand Up @@ -110,6 +112,10 @@ func isBlake2b(code uint64) bool {
return code >= BLAKE2B_MIN && code <= BLAKE2B_MAX
}

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

func sumSHA1(data []byte) []byte {
a := sha1.Sum(data)
return a[0:20]
Expand Down
1 change: 1 addition & 0 deletions sum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type SumTestCase struct {
}

var sumTestCases = []SumTestCase{
SumTestCase{ID, 3, "foo", "0003666f6f"},
SumTestCase{SHA1, -1, "foo", "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"},
SumTestCase{SHA1, 10, "foo", "110a0beec7b5ea3f0fdbc95d"},
SumTestCase{SHA2_256, -1, "foo", "12202c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"},
Expand Down

0 comments on commit 7aa9f26

Please sign in to comment.