split into daemon and cli interface

This commit is contained in:
2024-10-07 14:11:49 +02:00
parent 582c90c32a
commit fcdad8c2a3
5 changed files with 539 additions and 265 deletions

View File

@@ -1,11 +1,14 @@
package crypto
import (
"math/rand"
"encoding/json"
"github.com/google/uuid"
)
const DEFAULT_LENGTH int = 25
type Password struct {
Service string `json:"Service"`
Url string `json:"Url"`
@@ -21,7 +24,7 @@ func (p *Password) ToJson() ([]byte, error) {
func GetPasswordFromJson(b []byte) (Password, error) {
var result Password
err := json.Unmarshal(b, result)
err := json.Unmarshal(b, &result)
if err != nil {
return Password{}, err
@@ -30,9 +33,20 @@ func GetPasswordFromJson(b []byte) (Password, error) {
return result, nil
}
func NewPassword() *Password {
func NewPassword(length int) *Password {
return &Password{
Id: uuid.New(),
Password: GenerateRandomString(length),
}
}
func GenerateRandomString(length int) string {
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
b := make([]byte, length)
for i := range b {
b[i] = charset[rand.Intn(len(charset))]
}
return string(b)
}