Refactored the BLAKE2 algorithm and updated it so it now returns the hash in hexadecimal.

main
Tylan Tyson 12 months ago
parent 3f16795eb7
commit 66556d188f

@ -3,13 +3,14 @@ package blake2
import (
// Standard
"crypto/subtle"
"encoding/hex"
// Standard Extended
"golang.org/x/crypto/blake2b"
)
// Check Hash
func (algorithm Algorithm) CheckHash(data []byte, hashToCheck []byte) (bool, error) {
func (algorithm Algorithm) CheckHash(data []byte, hashToCheck string) (bool, error) {
// Hasher
hasher, err := blake2b.New(algorithm.size, nil)
// Handle Error
@ -22,6 +23,12 @@ func (algorithm Algorithm) CheckHash(data []byte, hashToCheck []byte) (bool, err
if err != nil {
return false, err
}
// Decode Hash To Check
decodedHashToCheck, err := hex.DecodeString(hashToCheck)
// Handle Error
if err != nil {
return false, err
}
// Return Match
return subtle.ConstantTimeCompare(hasher.Sum(nil), hashToCheck) == 1, err
return subtle.ConstantTimeCompare(hasher.Sum(nil), decodedHashToCheck) == 1, err
}

@ -1,24 +1,27 @@
package blake2
import (
// Standard
"encoding/hex"
// Standard Extended
"golang.org/x/crypto/blake2b"
)
// Hash
func (algorithm Algorithm) Hash(data []byte) ([]byte, error) {
func (algorithm Algorithm) Hash(data []byte) (string, error) {
// Hasher
hasher, err := blake2b.New(algorithm.size, nil)
// Handle Error
if err != nil {
return []byte(""), err
return "", err
}
// Write To Hasher
_, err = hasher.Write(data)
// Handle Error
if err != nil {
return []byte(""), err
return "", err
}
// Return Hash
return hasher.Sum(nil), err
return hex.EncodeToString(hasher.Sum(nil)), err
}

Loading…
Cancel
Save