-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword.go
More file actions
107 lines (77 loc) · 2.53 KB
/
Copy pathpassword.go
File metadata and controls
107 lines (77 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"strconv"
"time"
)
var (
lowerCharSet = "abcdefghijklmnopqrstuvwxyz"
upperCharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
specialCharSet string = "!@#$%^&*()-"
numberCharSet = "123456789"
minSpecialChar = 2
minUpperChar = 2
minNumberChar = 2
passwordLength = 12
)
func main() {
// check if password length meets the criteria
totalCharLenWithoutLowerChar := minUpperChar + minSpecialChar + minNumberChar
if totalCharLenWithoutLowerChar >= passwordLength {
fmt.Println("Please provide valid password length.")
os.Exit(1)
}
// Get user input - target folder needs to be organized
scanner := bufio.NewScanner(os.Stdin)
fmt.Printf("How many passwords do you want to generate? - ")
scanner.Scan()
numberOfPasswords, err := strconv.Atoi(scanner.Text())
if err != nil {
fmt.Println("Please prvovide correct value for the number of passwords.")
}
// it generate random number e
rand.Seed(time.Now().Unix())
for i := 0; i < numberOfPasswords; i++ {
password := generatePassword()
fmt.Printf("Password %v is %v \n", i+1, password)
}
}
func generatePassword() string {
// declare empty password variable
password := ""
// generate random special character based on minSpecialChar
for i := 0; i < minSpecialChar; i++ {
random := rand.Intn(len(specialCharSet))
//fmt.Println(specialCharSet[random])
//fmt.Printf("%v and %T \n", random, specialCharSet[random])
password = password + string(specialCharSet[random])
}
// generate random upper character based on minUpperChar
for i := 0; i < minUpperChar; i++ {
random := rand.Intn(len(upperCharSet))
password = password + string(upperCharSet[random])
}
// generate random upper character based on minNumberChar
for i := 0; i < minNumberChar; i++ {
random := rand.Intn(len(numberCharSet))
password = password + string(numberCharSet[random])
}
// find remaining lowerChar
totalCharLenWithoutLowerChar := minUpperChar + minSpecialChar + minNumberChar
remainingCharLen := passwordLength - totalCharLenWithoutLowerChar
// generate random lower character based on remainingCharLen
for i := 0; i < remainingCharLen; i++ {
random := rand.Intn(len(lowerCharSet))
password = password + string(lowerCharSet[random])
}
// shuffle the password string
passwordRune := []rune(password)
rand.Shuffle(len(passwordRune), func(i, j int) {
passwordRune[i], passwordRune[j] = passwordRune[j], passwordRune[i]
})
password = string(passwordRune)
return password
}