-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create PADHasher & TreeHasher interface to allow developers to use th…
…eir own hash function. * Convert the default hash function to SHA-512/256 * Part of #6 Credit to KT's team
- Loading branch information
Showing
10 changed files
with
137 additions
and
107 deletions.
There are no files selected for viewing
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,92 @@ | ||
package hasher | ||
|
||
import ( | ||
"crypto" | ||
|
||
ccrypto "github.com/coniks-sys/coniks-go/crypto" | ||
"github.com/coniks-sys/coniks-go/utils" | ||
) | ||
|
||
const ( | ||
emptyIdentifier = 'E' | ||
leafIdentifier = 'L' | ||
) | ||
|
||
// Hash represents the output of the used hash function. | ||
type Hash [ccrypto.DefaultHashSizeByte]byte | ||
|
||
// Default is the standard CONIKS hasher. | ||
var Default = New(crypto.SHA512_256) | ||
|
||
// PADHasher provides hash functions for the PAD implementations. | ||
type PADHasher interface { | ||
Digest(ms ...[]byte) []byte | ||
TreeHasher | ||
} | ||
|
||
// TreeHasher provides hash functions for tree implementations. | ||
type TreeHasher interface { | ||
ID() string | ||
Size() int | ||
HashInterior(left, right []byte) []byte | ||
HashLeaf(nonce []byte, index []byte, level uint32, data []byte) []byte | ||
HashEmpty(nonce []byte, index []byte, level uint32) []byte | ||
} | ||
|
||
type coniksHasher struct { | ||
crypto.Hash | ||
} | ||
|
||
// New creates a new PADHasher using the passed in hash function. | ||
func New(h crypto.Hash) PADHasher { | ||
return &coniksHasher{Hash: h} | ||
} | ||
|
||
// Digest hashes all passed byte slices. | ||
// The passed slices won't be mutated. | ||
func (ch *coniksHasher) Digest(ms ...[]byte) []byte { | ||
h := ch.New() | ||
for _, m := range ms { | ||
h.Write(m) | ||
} | ||
return h.Sum(nil) | ||
} | ||
|
||
// ID returns the name of the cryptographic hash function in string. | ||
func (coniksHasher) ID() string { | ||
return "SHA-512/256" | ||
} | ||
|
||
// Size returns the size of the hash output in bytes. | ||
func (ch *coniksHasher) Size() int { | ||
return ch.Size() | ||
} | ||
|
||
// HashInterior computes the hash of an interior node: | ||
// H(left || right) | ||
func (ch *coniksHasher) HashInterior(left, right []byte) []byte { | ||
return ch.Digest(left, right) | ||
} | ||
|
||
// HashLeaf computes the hash of a user leaf node: | ||
// H(Identifier || nonce || index || level || commit) | ||
func (ch *coniksHasher) HashLeaf(nonce []byte, index []byte, level uint32, commit []byte) []byte { | ||
return ch.Digest( | ||
[]byte{leafIdentifier}, | ||
nonce, | ||
index, | ||
utils.UInt32ToBytes(level), | ||
commit, | ||
) | ||
} | ||
|
||
// HashEmpty computes the hash of an empty leaf node: | ||
// H(Identifier || nonce || index || level) | ||
func (ch *coniksHasher) HashEmpty(nonce []byte, index []byte, level uint32) []byte { | ||
return ch.Digest( | ||
[]byte{emptyIdentifier}, | ||
nonce, | ||
index, | ||
utils.UInt32ToBytes(level), | ||
) | ||
} |
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
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
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
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
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
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
Oops, something went wrong.