...
1## Golang Password Generator
2
3[](https://travis-ci.org/sethvargo/go-password)
4[](https://godoc.org/github.com/sethvargo/go-password)
5
6This library implements generation of random passwords with provided
7requirements as described by [AgileBits
81Password](https://discussions.agilebits.com/discussion/23842/how-random-are-the-generated-passwords)
9in pure Golang. The algorithm is commonly used when generating website
10passwords.
11
12The library uses crypto/rand for added randomness.
13
14Sample example passwords this library may generate:
15
16```text
170N[k9PhDqmmfaO`p_XHjVv`HTq|zsH4XiH8umjg9JAGJ#\Qm6lZ,28XF4{X?3sHj
187@90|0H7!4p\,c<!32:)0.9N
19UlYuRtgqyWEivlXnLeBpZvIQ
20Q795Im1VR5h363s48oZGaLDa
21wpvbxlsc
22```
23
24## Installation
25
26```sh
27$ go get -u github.com/sethvargo/go-password/password
28```
29
30## Usage
31
32```golang
33package main
34
35import (
36 "log"
37
38 "github.com/sethvargo/go-password/password"
39)
40
41func main() {
42 // Generate a password that is 64 characters long with 10 digits, 10 symbols,
43 // allowing upper and lower case letters, disallowing repeat characters.
44 res, err := password.Generate(64, 10, 10, false, false)
45 if err != nil {
46 log.Fatal(err)
47 }
48 log.Printf(res)
49}
50```
51
52See the [GoDoc](https://godoc.org/github.com/sethvargo/go-password) for more
53information.
54
55## Testing
56
57For testing purposes, instead of accepted a `*password.Generator` struct, accept
58a `password.PasswordGenerator` interface:
59
60```go
61// func MyFunc(p *password.Generator)
62func MyFunc(p password.PasswordGenerator) {
63 // ...
64}
65```
66
67Then, in tests, use a mocked password generator with stubbed data:
68
69```go
70func TestMyFunc(t *testing.T) {
71 gen := password.NewMockGenerator("canned-response", false)
72 MyFunc(gen)
73}
74```
75
76In this example, the mock generator will always return the value
77"canned-response", regardless of the provided parameters.
78
79## License
80
81This code is licensed under the MIT license.
View as plain text