Bladeren bron

generation of passphrase applies site settings

bmallred 10 jaren geleden
bovenliggende
commit
dbed310a77
2 gewijzigde bestanden met toevoegingen van 96 en 3 verwijderingen
  1. 66 2
      main.go
  2. 30 1
      main_test.go

+ 66 - 2
main.go

159
159
160
	sha := sha512.New()
160
	sha := sha512.New()
161
	sha.Write([]byte(clearText))
161
	sha.Write([]byte(clearText))
162
	return nil, nil
162
	hash := sha.Sum(nil)
163
	hash = []byte(fmt.Sprintf("%x", hash))
164
165
	// Apply site criteria
166
	applySiteSettings(hash, settings)
167
168
	// Ensure the length is adequate
169
	if settings.MaximumLength > -1 {
170
		hash = hash[:settings.MaximumLength]
171
	}
172
	if !validateLength(hash, settings.MinimumLength, settings.MaximumLength) {
173
		log.Println("Does not meed the length requirements")
174
	}
175
176
	return hash, nil
177
}
178
179
func applySiteSettings(source []byte, settings Site) []byte {
180
	if !containsUppercase(source, settings.NumberOfUpperCase) {
181
		i := 0
182
		r := regexp.MustCompile(`[a-z]+`)
183
184
		var matches [][]int
185
		if matches = r.FindAllIndex(source, -1); matches != nil {
186
			for _, v := range matches {
187
				if i < settings.NumberOfUpperCase {
188
					c := strings.ToUpper(string(source[v[0]]))
189
					source[v[0]] = []byte(c)[0]
190
					i += 1
191
				}
192
			}
193
		}
194
	}
195
196
	if !containsDigits(source, settings.NumberOfDigits) {
197
		i := 0
198
		r := regexp.MustCompile(`[a-z]+`)
199
200
		var matches [][]int
201
		if matches = r.FindAllIndex(source, -1); matches != nil {
202
			for _, v := range matches {
203
				if i < settings.NumberOfDigits {
204
					source[v[0]] = byte(i)
205
					i += 1
206
				}
207
			}
208
		}
209
	}
210
211
	if !containsSpecialCharacters(source, settings.SpecialCharacters, settings.NumberOfSpecialCharacters) {
212
		i := 0
213
		r := regexp.MustCompile(`[a-z]+`)
214
215
		var matches [][]int
216
		if matches = r.FindAllIndex(source, -1); matches != nil {
217
			for _, v := range matches {
218
				if i < settings.NumberOfSpecialCharacters {
219
					i += 1
220
					source[v[0]] = []byte(settings.SpecialCharacters)[len(settings.SpecialCharacters)-i]
221
				}
222
			}
223
		}
224
	}
225
226
	return source
163
}
227
}
164
228
165
func containsDigits(source []byte, minOccurrences int) bool {
229
func containsDigits(source []byte, minOccurrences int) bool {
166
	r := regexp.MustCompile(`\d+`)
230
	r := regexp.MustCompile(`\d`)
167
231
168
	var matches [][]byte
232
	var matches [][]byte
169
	if matches = r.FindAll(source, -1); matches == nil {
233
	if matches = r.FindAll(source, -1); matches == nil {

+ 30 - 1
main_test.go

1
package main
1
package main
2
2
3
import "testing"
3
import (
4
	"fmt"
5
	"log"
6
	"testing"
7
)
8
9
func TestGeneratePassphrase(t *testing.T) {
10
	profile := "guest"
11
	//passphrase := "secret"
12
	site := Site{
13
		Host:                      "google.com",
14
		MinimumLength:             6,
15
		MaximumLength:             18,
16
		SpecialCharacters:         " !@#$%^&*()_+-=<>,./|\\",
17
		NumberOfSpecialCharacters: 2,
18
		NumberOfDigits:            2,
19
		NumberOfUpperCase:         1,
20
	}
21
	i := 0
22
23
	expected := "15898B4\\3738|f656d"
24
	for i = 0; i < 2; i++ {
25
		b, _ := generatePassphrase(profile, site)
26
		actual := fmt.Sprintf("%s", string(b))
27
		if actual != expected {
28
			log.Println(actual)
29
			t.FailNow()
30
		}
31
	}
32
}
4
33
5
func TestContainsDigits(t *testing.T) {
34
func TestContainsDigits(t *testing.T) {
6
	expected := true
35
	expected := true