8000 Merge pull request #10 from CodeB9/master · SelfCodeLearning/Go@89ce82b · GitHub
[go: up one dir, main page]

Skip to content

Commit 89ce82b

Browse files
Merge pull request TheAlgorithms#10 from CodeB9/master
Fix organization in folders and standardization of filenames; Add password generator algorithm
2 parents 23a496d + bc63c89 commit 89ce82b

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed
File renamed without changes.

other/PasswordGenerator.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)
0