From 82d5136c1b03efbb99931884dd75ab1160689ceb Mon Sep 17 00:00:00 2001 From: Saumit Dinesan Date: Tue, 28 Nov 2023 15:17:18 +0530 Subject: Refactoring project - Introducing handlers,models and utils- Updating previous hasher and idgen func + formatting --- utils/blake2b-hasher.go | 20 ++++++++++++++++++++ utils/genid.go | 15 +++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 utils/blake2b-hasher.go create mode 100644 utils/genid.go (limited to 'utils') diff --git a/utils/blake2b-hasher.go b/utils/blake2b-hasher.go new file mode 100644 index 0000000..38d13a1 --- /dev/null +++ b/utils/blake2b-hasher.go @@ -0,0 +1,20 @@ +package utils + +import ( + "fmt" + "io" + + "golang.org/x/crypto/blake2b" +) + +func GenerateHash(file io.Reader) (string, error) { + //hasher is an instance hash writer provided by the blake2b, nil - no key provided + hasher, err := blake2b.New256(nil) + if err != nil { + return "", err + } + if _, err := io.Copy(hasher, file); err != nil { + return "", err + } + return fmt.Sprintf("%x", hasher.Sum(nil)), nil +} diff --git a/utils/genid.go b/utils/genid.go new file mode 100644 index 0000000..8525125 --- /dev/null +++ b/utils/genid.go @@ -0,0 +1,15 @@ +package utils + +import ( + "crypto/rand" + "fmt" +) + +// GenerateID creates a random 6-digit ID +func GenerateID() (string, error) { + b := make([]byte, 3) // 3 bytes make 6 hex characters + if _, err := rand.Read(b); err != nil { + return "", err + } + return fmt.Sprintf("%x", b), nil +} -- cgit v1.2.3