mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-12 03:05:14 +00:00
password generator (#395)
This commit is contained in:
49
password/handler/password.go
Normal file
49
password/handler/password.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user