File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ // This program generates a password from a list of possible chars
2
+ // You must provide a minimum length and a maximum length
3
+ // This length is not fixed if you generate multiple passwords for the same range
4
+
5
+ package main
6
+
7
+ import (
8
+ crand "crypto/rand"
9
+ "fmt"
10
+ "io"
11
+ "math/rand"
12
+ "time"
13
+ )
14
+
15
+ func generatePassword (minLength int , maxLength int ) string {
16
+ var chars = []byte ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+,.?/:;{}[]`~" )
17
+
18
+ var length = rand .Intn (maxLength - minLength ) + minLength
19
+
20
+ newPassword := make ([]byte , length )
21
+ randomData := make ([]byte , length + (length / 4 ))
22
+ clen := byte (len (chars ))
23
+ maxrb := byte (256 - (256 % len (chars )))
24
+ i := 0
25
+ for {
26
+ if _ , err := io .ReadFull (crand .Reader , randomData ); err != nil {
27
+ panic (err )
28
+ }
29
+ for _ , c := range randomData {
30
+ if c >= maxrb {
31
+ continue
32
+ }
33
+ newPassword [i ] = chars [c % clen ]
34
+ i ++
35
+ if i == length {
36
+ return string (newPassword )
37
+ }
38
+ }
39
+ }
40
+ }
41
+
42
+ func main () {
43
+ rand .Seed (time .Now ().Unix ())
44
+
45
+ fmt .Print ("Please specify a minimum length: " )
46
+ var minLength int
47
+ fmt .Scanf ("%d" , & minLength )
48
+
49
+ fmt .Print ("Please specify a maximum length: " )
50
+ var maxLength int
51
+ fmt .Scanf ("%d" , & maxLength )
52
+
53
+ fmt .Printf ("Your generated password is %v\n " , generatePassword (minLength , maxLength ))
54
+ }
You can’t perform that action at this time.
0 commit comments