From 66556d188f4b0459def6754560da188e63eec1ba Mon Sep 17 00:00:00 2001 From: The-Tysonator Date: Thu, 12 Oct 2023 10:48:22 +0100 Subject: [PATCH] Refactored the BLAKE2 algorithm and updated it so it now returns the hash in hexadecimal. --- algorithms/blake2/{algorithm.go => Algorithm.go} | 0 algorithms/blake2/CheckHash.go | 11 +++++++++-- algorithms/blake2/Hash.go | 11 +++++++---- 3 files changed, 16 insertions(+), 6 deletions(-) rename algorithms/blake2/{algorithm.go => Algorithm.go} (100%) diff --git a/algorithms/blake2/algorithm.go b/algorithms/blake2/Algorithm.go similarity index 100% rename from algorithms/blake2/algorithm.go rename to algorithms/blake2/Algorithm.go diff --git a/algorithms/blake2/CheckHash.go b/algorithms/blake2/CheckHash.go index c4a6271..6f2043f 100644 --- a/algorithms/blake2/CheckHash.go +++ b/algorithms/blake2/CheckHash.go @@ -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 } diff --git a/algorithms/blake2/Hash.go b/algorithms/blake2/Hash.go index 84a99cb..a599e53 100644 --- a/algorithms/blake2/Hash.go +++ b/algorithms/blake2/Hash.go @@ -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 }