forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request TheAlgorithms#10 from CodeB9/master
Fix organization in folders and standardization of filenames; Add password generator algorithm
- Loading branch information
Showing
5 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// This program generates a password from a list of possible chars | ||
// You must provide a minimum length and a maximum length | ||
// This length is not fixed if you generate multiple passwords for the same range | ||
|
||
package main | ||
|
||
import ( | ||
crand "crypto/rand" | ||
"fmt" | ||
"io" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
func generatePassword(minLength int, maxLength int) string { | ||
var chars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+,.?/:;{}[]`~") | ||
|
||
var length = rand.Intn(maxLength-minLength) + minLength | ||
|
||
newPassword := make([]byte, length) | ||
randomData := make([]byte, length+(length/4)) | ||
clen := byte(len(chars)) | ||
maxrb := byte(256 - (256 % len(chars))) | ||
i := 0 | ||
for { | ||
if _, err := io.ReadFull(crand.Reader, randomData); err != nil { | ||
panic(err) | ||
} | ||
for _, c := range randomData { | ||
if c >= maxrb { | ||
continue | ||
} | ||
newPassword[i] = chars[c%clen] | ||
i++ | ||
if i == length { | ||
return string(newPassword) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
rand.Seed(time.Now().Unix()) | ||
|
||
fmt.Print("Please specify a minimum length: ") | ||
var minLength int | ||
fmt.Scanf("%d", &minLength) | ||
|
||
fmt.Print("Please specify a maximum length: ") | ||
var maxLength int | ||
fmt.Scanf("%d", &maxLength) | ||
|
||
fmt.Printf("Your generated password is %v\n", generatePassword(minLength, maxLength)) | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.