diff --git a/pkg/utils.go b/pkg/utils.go index 39e4f06..ac1788a 100644 --- a/pkg/utils.go +++ b/pkg/utils.go @@ -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 @@ -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)) +}