mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 19:04:35 +00:00
50 lines
949 B
Go
50 lines
949 B
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
|
|
pb "github.com/micro/services/password/proto"
|
|
)
|
|
|
|
|
|
var (
|
|
minLength = 8
|
|
|
|
special = "!@#$%&*"
|
|
numbers = "0123456789"
|
|
lowercase = "abcdefghijklmnopqrstuvwxyz"
|
|
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
allChars = special + numbers + lowercase + uppercase
|
|
)
|
|
|
|
func random(chars string, i int) string {
|
|
bytes := make([]byte, i)
|
|
|
|
for {
|
|
rand.Read(bytes)
|
|
for i, b := range bytes {
|
|
bytes[i] = chars[b%byte(len(chars))]
|
|
}
|
|
break
|
|
}
|
|
|
|
return string(bytes)
|
|
}
|
|
|
|
|
|
type Password struct{}
|
|
|
|
func (p *Password) Generate(ctx context.Context, req *pb.GenerateRequest, rsp *pb.GenerateResponse) error {
|
|
if req.Length <= 0 {
|
|
req.Length = int32(minLength)
|
|
}
|
|
|
|
// TODO; allow user to specify types of characters
|
|
|
|
// generate and return password
|
|
rsp.Password = random(allChars, int(req.Length))
|
|
|
|
return nil
|
|
}
|