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

Loading…
Cancel
Save