diff options
author | terminaldweller <devi@terminaldweller.com> | 2025-08-05 00:06:39 +0000 |
---|---|---|
committer | terminaldweller <devi@terminaldweller.com> | 2025-08-05 00:06:39 +0000 |
commit | bcabab78a132f958cb66cf8b6ac20a328b1549ea (patch) | |
tree | 34c418a306b668d90e383bf57ba4f81c5e5a08e9 | |
parent | added apikeys (diff) | |
download | hived-bcabab78a132f958cb66cf8b6ac20a328b1549ea.tar.gz hived-bcabab78a132f958cb66cf8b6ac20a328b1549ea.zip |
Diffstat (limited to '')
-rw-r--r-- | hived/apikey.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/hived/apikey.go b/hived/apikey.go new file mode 100644 index 0000000..e0b9271 --- /dev/null +++ b/hived/apikey.go @@ -0,0 +1,43 @@ +package main + +import ( + "crypto/rand" + "encoding/hex" + "log" + + "github.com/google/uuid" + "golang.org/x/crypto/bcrypt" +) + +func genRandomString() string { + return uuid.New().String() +} + +func genRandomString2() string { + bytes := make([]byte, 32) + + if _, err := rand.Read(bytes); err != nil { + log.Fatal(err) + } + return hex.EncodeToString(bytes) +} + +func encrypt(plaintext string) (string, error) { + cypherText, err := bcrypt.GenerateFromPassword([]byte(plaintext), 10) + if err != nil { + return "", err + } + + return string(cypherText), nil +} + +func GenAPIKey() (string, error) { + apiKey := genRandomString2() + log.Print("Generated APIKEY: ", apiKey) + encrypted, err := encrypt(apiKey) + if err != nil { + return "", err + } + + return encrypted, nil +} |