Skip to content

Commit

Permalink
Merge pull request TheAlgorithms#10 from CodeB9/master
Browse files Browse the repository at this point in the history
Fix organization in folders and standardization of filenames; Add password generator algorithm
  • Loading branch information
dynamitechetan authored Oct 11, 2017
2 parents 23a496d + bc63c89 commit 89ce82b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
File renamed without changes.
54 changes: 54 additions & 0 deletions other/PasswordGenerator.go
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.

0 comments on commit 89ce82b

Please sign in to comment.