split into daemon and cli interface
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user