Quellcode durchsuchen

clean-up and included more comments

bmallred vor 10 Jahren
Ursprung
Commit
b7450ce128
2 geänderte Dateien mit 17 neuen und 6 gelöschten Zeilen
  1. 15 2
      main.go
  2. 2 4
      main_test.go

+ 15 - 2
main.go

@ -103,6 +103,7 @@ func Save(file string, sites []Site) error {
103 103
	return nil
104 104
}
105 105
106
// Read the password book
106 107
func Read(file string) ([]Site, error) {
107 108
	// If the file doesn't exist yet no worries
108 109
	if _, err := os.Stat(file); os.IsNotExist(err) {
@ -136,21 +137,25 @@ func Read(file string) ([]Site, error) {
136 137
	return sites, nil
137 138
}
138 139
140
// Get the book name
139 141
func getBookname(profile string) []byte {
140 142
	sha := sha1.New()
141 143
	sha.Write([]byte(profile))
142 144
	return sha.Sum(nil)
143 145
}
144 146
147
// Encrypt the password book
145 148
func encrypt(clearText, profile, passphrase string) ([]byte, error) {
146 149
	return nil, nil
147 150
}
148 151
152
// Decrypt the password book
149 153
func decrypt(encryptedText, profile, passphrase string) ([]byte, error) {
150 154
	return nil, nil
151 155
}
152 156
153
func generatePassphrase(profile string, settings Site) ([]byte, error) {
157
// Generate the passphrase
158
func generatePassphrase(profile, passphrase string, settings Site) ([]byte, error) {
154 159
	clearText := fmt.Sprintf(
155 160
		"%s-%s-%s",
156 161
		strings.ToLower(profile),
@ -165,10 +170,12 @@ func generatePassphrase(profile string, settings Site) ([]byte, error) {
165 170
	// Apply site criteria
166 171
	applySiteSettings(hash, settings)
167 172
168
	// Ensure the length is adequate
173
	// If there is a maximum length truncate the hash
169 174
	if settings.MaximumLength > -1 {
170 175
		hash = hash[:settings.MaximumLength]
171 176
	}
177
178
	// Ensure the length is adequate
172 179
	if !validateLength(hash, settings.MinimumLength, settings.MaximumLength) {
173 180
		log.Println("Does not meed the length requirements")
174 181
	}
@ -176,6 +183,7 @@ func generatePassphrase(profile string, settings Site) ([]byte, error) {
176 183
	return hash, nil
177 184
}
178 185
186
// Apply site settings to the hashed value
179 187
func applySiteSettings(source []byte, settings Site) []byte {
180 188
	if !containsUppercase(source, settings.NumberOfUpperCase) {
181 189
		i := 0
@ -226,6 +234,7 @@ func applySiteSettings(source []byte, settings Site) []byte {
226 234
	return source
227 235
}
228 236
237
// Determine if the hash currently contains the appropriate amount of digits
229 238
func containsDigits(source []byte, minOccurrences int) bool {
230 239
	r := regexp.MustCompile(`\d`)
231 240
@ -237,6 +246,7 @@ func containsDigits(source []byte, minOccurrences int) bool {
237 246
	return len(matches) >= minOccurrences
238 247
}
239 248
249
// Determine if the hash currently contains the appropriate amount of uppercase characters
240 250
func containsUppercase(source []byte, minOccurrences int) bool {
241 251
	r := regexp.MustCompile(`[A-Z]+`)
242 252
@ -248,6 +258,8 @@ func containsUppercase(source []byte, minOccurrences int) bool {
248 258
	return len(matches) >= minOccurrences
249 259
}
250 260
261
// Determine if the hash currently contains the appropriate amount of special characters from the allowed
262
// character set
251 263
func containsSpecialCharacters(source []byte, specialCharacters string, minOccurrences int) bool {
252 264
	s := specialCharacters
253 265
	s = strings.Replace(s, "\\", "\\\\", -1)
@ -267,6 +279,7 @@ func containsSpecialCharacters(source []byte, specialCharacters string, minOccur
267 279
	return len(matches) >= minOccurrences
268 280
}
269 281
282
// Determine if the hash currently abides by the length restrictions
270 283
func validateLength(source []byte, minimum, maximum int) bool {
271 284
	if minimum > -1 && len(source) < minimum {
272 285
		return false

+ 2 - 4
main_test.go

@ -2,13 +2,12 @@ package main
2 2
3 3
import (
4 4
	"fmt"
5
	"log"
6 5
	"testing"
7 6
)
8 7
9 8
func TestGeneratePassphrase(t *testing.T) {
10 9
	profile := "guest"
11
	//passphrase := "secret"
10
	passphrase := "secret"
12 11
	site := Site{
13 12
		Host:                      "google.com",
14 13
		MinimumLength:             6,
@ -22,10 +21,9 @@ func TestGeneratePassphrase(t *testing.T) {
22 21
23 22
	expected := "15898B4\\3738|f656d"
24 23
	for i = 0; i < 2; i++ {
25
		b, _ := generatePassphrase(profile, site)
24
		b, _ := generatePassphrase(profile, passphrase, site)
26 25
		actual := fmt.Sprintf("%s", string(b))
27 26
		if actual != expected {
28
			log.Println(actual)
29 27
			t.FailNow()
30 28
		}
31 29
	}