-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpassword_generator.go
132 lines (107 loc) · 3.43 KB
/
password_generator.go
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"crypto/rand"
"fmt"
"log"
"math/big"
"strings"
"time"
)
// Character sets
const (
lowerChars = "abcdefghijklmnopqrstuvwxyz"
upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numberChars = "0123456789"
specialChars = "!@#$%^&*()-_=+[]{}<>?"
)
// Function to generate a secure random password
func generatePassword(length int, includeUpper, includeNumbers, includeSpecial bool) (string, error) {
// Validate length
if length <= 0 {
return "", fmt.Errorf("password length must be greater than zero")
}
// Build charset and guarantee inclusion of at least one of each selected type
var charset strings.Builder
var guaranteedChars []byte
charset.WriteString(lowerChars) // Lowercase is always included
guaranteedChars = append(guaranteedChars, randomChar(lowerChars))
if includeUpper {
charset.WriteString(upperChars)
guaranteedChars = append(guaranteedChars, randomChar(upperChars))
}
if includeNumbers {
charset.WriteString(numberChars)
guaranteedChars = append(guaranteedChars, randomChar(numberChars))
}
if includeSpecial {
charset.WriteString(specialChars)
guaranteedChars = append(guaranteedChars, randomChar(specialChars))
}
// Fill the rest of the password with random characters from charset
password := make([]byte, length)
for i := range password {
password[i] = randomChar(charset.String())
}
// Replace random positions with guaranteed character types to meet criteria
for i, char := range guaranteedChars {
randomIndex := randomIndex(length)
password[randomIndex] = char
}
// Shuffle the final password for added randomness
shuffle(password)
return string(password), nil
}
// Helper function to pick a random character from a string
func randomChar(charset string) byte {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
if err != nil {
log.Fatal("Failed to generate random character:", err)
}
return charset[num.Int64()]
}
// Helper function to generate a random index within a given range
func randomIndex(max int) int {
num, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
if err != nil {
log.Fatal("Failed to generate random index:", err)
}
return int(num.Int64())
}
// Shuffle function to randomize the final password
func shuffle(password []byte) {
for i := range password {
j := randomIndex(len(password))
password[i], password[j] = password[j], password[i]
}
}
func main() {
var length int
var includeUpper, includeNumbers, includeSpecial bool
// Get user input for password configuration
fmt.Print("Enter password length: ")
fmt.Scan(&length)
fmt.Print("Include uppercase letters? (true/false): ")
fmt.Scan(&includeUpper)
fmt.Print("Include numbers? (true/false): ")
fmt.Scan(&includeNumbers)
fmt.Print("Include special characters? (true/false): ")
fmt.Scan(&includeSpecial)
// Generate the password
password, err := generatePassword(length, includeUpper, includeNumbers, includeSpecial)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Generated Password:", password)
}
// SAMPLE INPUT OUTPUT AND EXPLANATION
// Sample Input:
// Password length: 12
// Include uppercase letters: true
// Include numbers: true
// Include special characters: true
// Sample Output:
// Generated Password: G9@bC2!qP$e1
// Explanation:
// Input: The desired length and character options for the password.
// Output: A random, secure password containing uppercase letters, numbers, and special characters, based on the specified criteria.