Skip to content

Commit

Permalink
#added ULID and Hash generation function
Browse files Browse the repository at this point in the history
  • Loading branch information
JonyBepary committed Oct 29, 2023
1 parent df628be commit f1ce06a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pkg/utils.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package pkg

import (
"crypto/sha256"
"fmt"
"os"
"time"

"golang.org/x/exp/rand"

"github.com/SohelAhmedJoni/Awazz-Backend/internal/model"
ulid "github.com/oklog/ulid/v2"
)

func GetUlid() string {
r := rand.New(new(rand.LockedSource))
return ulid.MustNew(ulid.Timestamp(time.Now()), ulid.Monotonic(r, 0)).String()
}

// read file from path to blob
func ReadFile(path string) []byte {
// Read the contents of the file into a byte slice
Expand Down Expand Up @@ -38,3 +49,19 @@ func WriteServerPKI() (*model.AKS, error) {
}
return pk, nil
}

func StringHashGeneration(str string) string {
digest := sha256.New()
digest.Write([]byte(str))
return fmt.Sprintf("%x", digest.Sum(nil))
}

func FileHashGeneration(filename string) string {
hash := sha256.New()
file, err := os.ReadFile(filename)
if err != nil {
return ""
}
hash.Write([]byte(fmt.Sprintf("%v", file)))
return fmt.Sprintf("%x", hash.Sum(nil))
}

0 comments on commit f1ce06a

Please sign in to comment.