39 lines
735 B
Go
39 lines
735 B
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"math/rand"
|
|
"time"
|
|
|
|
"code.gurenya.net/carlsmei/muninn-aio/db"
|
|
"code.gurenya.net/carlsmei/muninn-aio/ent/link"
|
|
)
|
|
|
|
// Key Generation Algorithm 6:51 MSK 29.07.24
|
|
|
|
var numChars = 8
|
|
var keyAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
|
var seededRand *rand.Rand = rand.New(
|
|
rand.NewSource(time.Now().UnixNano()))
|
|
|
|
func GenerateKey(ctx context.Context) string {
|
|
client := db.GetEntClient()
|
|
cond := false
|
|
key := make([]byte, numChars)
|
|
|
|
for !cond {
|
|
for i := range key {
|
|
key[i] = keyAlphabet[seededRand.Intn(len(keyAlphabet))]
|
|
}
|
|
|
|
exists, _ := client.Link.
|
|
Query().
|
|
Where(link.Key(string(key))).
|
|
Exist(ctx)
|
|
|
|
cond = !exists
|
|
}
|
|
|
|
return string(key)
|
|
}
|