浏览代码

added check for digit count

bmallred 10 年之前
父节点
当前提交
e77100decb
共有 2 个文件被更改,包括 66 次插入0 次删除
  1. 43 0
      main.go
  2. 23 0
      main_test.go

+ 43 - 0
main.go

@ -3,12 +3,15 @@ package main
3 3
import (
4 4
	"bytes"
5 5
	"compress/gzip"
6
	"crypto/sha1"
7
	"crypto/sha512"
6 8
	"encoding/json"
7 9
	"fmt"
8 10
	"io/ioutil"
9 11
	"log"
10 12
	"net/http"
11 13
	"os"
14
	"regexp"
12 15
	"strings"
13 16
)
14 17
@ -20,9 +23,12 @@ type Site struct {
20 23
	NumberOfSpecialCharacters int    `json:numberOfSpecialCharacters`
21 24
	NumberOfUpperCase         int    `json:numberOfUpperCase`
22 25
	NumberOfDigits            int    `json:numberOfDigits`
26
	Revision                  int    `json:revision`
23 27
}
24 28
25 29
func main() {
30
	log.Printf("%x", getBookname("bmallred"))
31
26 32
	http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
27 33
	})
28 34
@ -131,3 +137,40 @@ func Read(file string) ([]Site, error) {
131 137
132 138
	return sites, nil
133 139
}
140
141
func getBookname(profile string) []byte {
142
	sha := sha1.New()
143
	sha.Write([]byte(profile))
144
	return sha.Sum(nil)
145
}
146
147
func encrypt(clearText, profile, passphrase string) ([]byte, error) {
148
	return nil, nil
149
}
150
151
func decrypt(encryptedText, profile, passphrase string) ([]byte, error) {
152
	return nil, nil
153
}
154
155
func generatePassphrase(profile string, settings Site) ([]byte, error) {
156
	clearText := fmt.Sprintf(
157
		"%s-%s-%s",
158
		strings.ToLower(profile),
159
		strings.ToLower(settings.Host),
160
		settings.Revision)
161
162
	sha := sha512.New()
163
	sha.Write([]byte(clearText))
164
	return nil, nil
165
}
166
167
func containsDigits(source []byte, minOccurrences int) bool {
168
	r := regexp.MustCompile(`\d+`)
169
170
	var matches [][]byte
171
	if matches = r.FindAll(source, -1); matches == nil {
172
		return false
173
	}
174
175
	return len(matches) >= minOccurrences
176
}

+ 23 - 0
main_test.go

@ -0,0 +1,23 @@
1
package main
2
3
import "testing"
4
5
func TestContainsDigits(t *testing.T) {
6
	expected := true
7
	actual := containsDigits([]byte("blah0blah2"), 2)
8
	if actual != expected {
9
		t.FailNow()
10
	}
11
12
	expected = false
13
	actual = containsDigits([]byte("blah0blah"), 2)
14
	if actual != expected {
15
		t.FailNow()
16
	}
17
18
	expected = true
19
	actual = containsDigits([]byte("blah0blah12"), 2)
20
	if actual != expected {
21
		t.FailNow()
22
	}
23
}