From 0c185d23a5fe3be278339fb9bf0bfa23c88da380 Mon Sep 17 00:00:00 2001 From: The-Tysonator Date: Wed, 11 Oct 2023 21:01:44 +0100 Subject: [PATCH] Added the SHA256 algorithm. --- README.md | 1 + algorithms/sha256/CheckHash.go | 21 +++++++++++++++++++++ algorithms/sha256/Hash.go | 20 ++++++++++++++++++++ algorithms/sha256/algorithm.go | 10 ++++++++++ 4 files changed, 52 insertions(+) create mode 100644 algorithms/sha256/CheckHash.go create mode 100644 algorithms/sha256/Hash.go create mode 100644 algorithms/sha256/algorithm.go diff --git a/README.md b/README.md index 7bd9594..f3460bb 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ GCL is a Go library designed to offer a robust collection of cryptographic algor GCL offers support for a diverse range of cryptographic algorithms, ensuring that your cryptographic requirements are comprehensively addressed. Our library includes: - [X] BLAKE2 +- [X] SHA256 ## Installation diff --git a/algorithms/sha256/CheckHash.go b/algorithms/sha256/CheckHash.go new file mode 100644 index 0000000..5f3b23c --- /dev/null +++ b/algorithms/sha256/CheckHash.go @@ -0,0 +1,21 @@ +package sha256 + +import ( + // Standard + "crypto/sha256" + "crypto/subtle" +) + +// Check Hash +func (Algorithm) CheckHash(data []byte, hashToCheck []byte) (bool, error) { + // Hasher + hasher := sha256.New() + // Write To Hasher + _, err := hasher.Write(data) + // Handle Error + if err != nil { + return false, err + } + // Return Hash + return subtle.ConstantTimeCompare(hasher.Sum(nil), hashToCheck) == 1, err +} diff --git a/algorithms/sha256/Hash.go b/algorithms/sha256/Hash.go new file mode 100644 index 0000000..885acd0 --- /dev/null +++ b/algorithms/sha256/Hash.go @@ -0,0 +1,20 @@ +package sha256 + +import ( + // Standard + "crypto/sha256" +) + +// Hash +func (Algorithm) Hash(data []byte) ([]byte, error) { + // Hasher + hasher := sha256.New() + // Write To Hasher + _, err := hasher.Write(data) + // Handle Error + if err != nil { + return []byte(""), err + } + // Return Hash + return hasher.Sum(nil), err +} diff --git a/algorithms/sha256/algorithm.go b/algorithms/sha256/algorithm.go new file mode 100644 index 0000000..f878f28 --- /dev/null +++ b/algorithms/sha256/algorithm.go @@ -0,0 +1,10 @@ +package sha256 + +// Algorithm +type Algorithm struct{} + +// New +func New(size int) Algorithm { + // Return Algorithm + return Algorithm{} +}