mv crypto -> internal/crypto
This commit is contained in:
52
internal/crypto/crypto.go
Normal file
52
internal/crypto/crypto.go
Normal file
@@ -0,0 +1,52 @@
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user