initial commit
This commit is contained in:
commit
60fb40d61a
34 changed files with 2571 additions and 0 deletions
48
pkg/key/key.go
Normal file
48
pkg/key/key.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
// Copyright (C) 2023 Marius Schellenberger
|
||||
|
||||
package key
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"git.giftfish.de/ston1th/keyctl/pkg/core"
|
||||
)
|
||||
|
||||
const idSize = 16
|
||||
|
||||
func generate(size int) (buf []byte, err error) {
|
||||
buf = make([]byte, size)
|
||||
_, err = io.ReadFull(rand.Reader, buf)
|
||||
return
|
||||
}
|
||||
|
||||
func GenerateID() (string, error) {
|
||||
id, err := generate(idSize)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(id), nil
|
||||
}
|
||||
|
||||
func Generate(name string, size int, enc core.Encoding) (k core.Key, err error) {
|
||||
id, err := GenerateID()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
key, err := generate(size)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
k = core.Key{
|
||||
Name: name,
|
||||
ID: id,
|
||||
Size: size,
|
||||
Encoding: enc.String(),
|
||||
Key: enc.Encode(key),
|
||||
Created: time.Now().Unix(),
|
||||
}
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue