password generator (#395)

This commit is contained in:
Asim Aslam
2022-03-02 10:11:46 +00:00
committed by GitHub
parent 9b61a8d28e
commit 7ab702dd5b
13 changed files with 466 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
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
}