This repository was archived by the owner on Mar 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpasswordcreator.go
97 lines (74 loc) · 2.26 KB
/
passwordcreator.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
/*Copyright (C) 2010 Andreas Sinz
This file is part of GoPasswordCreator.
GoPasswordCreator is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; only version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"bytes"
"crypto/rand"
"errors"
"os"
"strings"
)
type Creator struct {
characters string
file *os.File
}
const (
letters = "abcdefghijklmnopqrstuvwxyz"
numbers = "0123456789"
special = ",.-"
)
func NewCreator(file *os.File, lowerCase, upperCase, numerals, specialCharacters bool, userCharacters string) (creator *Creator, err error) {
if file == nil {
return nil, errors.New("File is nil!")
}
characters := ""
if lowerCase {
characters += letters
}
if upperCase {
characters += strings.ToUpper(letters)
}
if numerals {
characters += numbers
}
if specialCharacters {
characters += special
}
characters += userCharacters
if len(characters) <= 1 {
err = errors.New("Not enough Characters specified to generate passwords")
return nil, err
}
return &Creator{characters, file}, err
}
func (creator *Creator) CreatePassword(length int) (password []byte, err error) {
passwordBuffer := new(bytes.Buffer)
//Create a byte-array and fill it with random bytes
randbytes := make([]byte, length)
if _, err := rand.Read(randbytes); err == nil {
for j := 0; j < length; j++ {
tmpindex := int(randbytes[j]) % len(creator.characters)
char := creator.characters[tmpindex]
//Append the character at the end of the password
passwordBuffer.WriteString(string(char))
}
return passwordBuffer.Bytes(), nil
}
return nil, err
}
func (creator *Creator) WritePasswords(length, count int) error {
for i := 0; i < count; i++ {
if pass, err := creator.CreatePassword(length); err == nil {
creator.file.Write(append(pass, byte('\n')))
} else {
return err
}
}
return nil
}