43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
package usecase
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"golang.org/x/exp/rand"
|
|
)
|
|
|
|
// GenNewPassword generates new password, writes it to the memory and returns.
|
|
func (u *Usecase) GenNewPassword() (string, error) {
|
|
rand.Seed(uint64(time.Now().UnixNano()))
|
|
// gen new password
|
|
pass := genPasswordString()
|
|
// save to the memory
|
|
if err := u.memory.SavePassword(pass); err != nil {
|
|
return "", fmt.Errorf("failed to save password in the memory: %w", err)
|
|
}
|
|
return pass, nil
|
|
}
|
|
|
|
const (
|
|
passwordLen = 8
|
|
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
|
|
)
|
|
|
|
func genPasswordString() string {
|
|
b := make([]byte, passwordLen)
|
|
for i := range b {
|
|
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
// PasswordExists checks that password exists in the memory.
|
|
func (u *Usecase) PasswordExists(password string) (bool, error) {
|
|
exists, err := u.memory.PasswordExists(password)
|
|
if err != nil {
|
|
return false, fmt.Errorf("failed to check password in the memory: %w", err)
|
|
}
|
|
return exists, nil
|
|
}
|