Первый прототип с основными возможностями
This commit is contained in:
38
services/key.go
Normal file
38
services/key.go
Normal file
@@ -0,0 +1,38 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user