Files
pentapass/crypto/crypto.go

53 lines
945 B
Go

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"`
Username string `json:"Username"`
Password string `json:"Password"`
Tags []string `json:"Tags"`
Id uuid.UUID `json:"Id"`
}
func (p *Password) ToJson() ([]byte, error) {
return json.Marshal(p)
}
func GetPasswordFromJson(b []byte) (Password, error) {
var result Password
err := json.Unmarshal(b, &result)
if err != nil {
return Password{}, err
}
return result, nil
}
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)
}