Browse Source

clean-up and included more comments

bmallred 10 years ago
parent
commit
b7450ce128
2 changed files with 17 additions and 6 deletions
  1. 15 2
      main.go
  2. 2 4
      main_test.go

+ 15 - 2
main.go

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

+ 2 - 4
main_test.go

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