Quellcode durchsuchen

some spelling fixes, rolling index scenarios, and fine tuning

bmallred vor 10 Jahren
Ursprung
Commit
1d2edad0bd
4 geänderte Dateien mit 34 neuen und 8 gelöschten Zeilen
  1. 12 1
      main.go
  2. 9 2
      main_test.go
  3. 5 2
      resources.go
  4. 8 3
      site.go

+ 12 - 1
main.go

3
import (
3
import (
4
	"log"
4
	"log"
5
	"net/http"
5
	"net/http"
6
	"os"
6
)
7
)
7
8
8
func main() {
9
func main() {
14
	http.HandleFunc("/book", BookHandler)
15
	http.HandleFunc("/book", BookHandler)
15
	http.HandleFunc("/", DefaultHandler)
16
	http.HandleFunc("/", DefaultHandler)
16
17
17
	log.Fatal(http.ListenAndServe("localhost:8080", nil))
18
	log.Fatal(http.ListenAndServe(address(), nil))
19
}
20
21
// Retrieve the web server address from the environment variable ENIGMA_SERVER if possible.
22
// If the environment variable is not set then default to "localhost:8080".
23
func address() string {
24
	env := os.Getenv("ENIGMA_SERVER")
25
	if env == "" {
26
		return "localhost:8080"
27
	}
28
	return env
18
}
29
}

+ 9 - 2
main_test.go

19
	}
19
	}
20
	i := 0
20
	i := 0
21
21
22
	expected := "C\\041|e1dc001da138"
22
	expected := "Ce\\41ae|dc001da138"
23
	for i = 0; i < 2; i++ {
23
	for i = 0; i < 2; i++ {
24
		b, _ := generatePassphrase(profile, passphrase, site)
24
		b := site.generatePassphrase(profile, passphrase)
25
		actual := fmt.Sprintf("%s", string(b))
25
		actual := fmt.Sprintf("%s", string(b))
26
		if actual != expected {
26
		if actual != expected {
27
			t.FailNow()
27
			t.FailNow()
88
	if actual != expected {
88
	if actual != expected {
89
		t.FailNow()
89
		t.FailNow()
90
	}
90
	}
91
92
	special = " "
93
	expected = true
94
	actual = containsSpecialCharacters([]byte("Blah BLa l "), special, 2)
95
	if actual != expected {
96
		t.FailNow()
97
	}
91
}
98
}
92
99
93
func TestLength(t *testing.T) {
100
func TestLength(t *testing.T) {

+ 5 - 2
resources.go

106
        .tab-content > .tab-pane {
106
        .tab-content > .tab-pane {
107
            padding: 1em;
107
            padding: 1em;
108
        }
108
        }
109
		span.password {
110
			margin-left: 1em;
111
		}
109
    </style>
112
    </style>
110
113
111
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
114
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
154
                                        <input name="host" type="hidden" value="{{ .Host }}" />
157
                                        <input name="host" type="hidden" value="{{ .Host }}" />
155
										<button class="btn btn-default btn-xs" title="Generate a new password"><span class="glyphicon glyphicon-refresh" aria-hidden="true"></span></button>
158
										<button class="btn btn-default btn-xs" title="Generate a new password"><span class="glyphicon glyphicon-refresh" aria-hidden="true"></span></button>
156
                                    </form>
159
                                    </form>
157
                                    <span>{{ .Password }}</span>
160
                                    <span class="password">{{ .Password }}</span>
158
                                </td>
161
                                </td>
159
                                <td class="text-right">
162
                                <td class="text-right">
160
                                    <form class="form form-horizontal" action="/api/remove" method="post">
163
                                    <form class="form form-horizontal" action="/api/remove" method="post">
311
                                    <button name="cmd" value="update" type="submit" class="btn btn-default" title="Update profile passphrase">Update Passphrase</button>
314
                                    <button name="cmd" value="update" type="submit" class="btn btn-default" title="Update profile passphrase">Update Passphrase</button>
312
                                </div>
315
                                </div>
313
                                <div class="col-xs-3 text-right">
316
                                <div class="col-xs-3 text-right">
314
									<button name="cmd" value="delete" type="submit" class="btn btn-danger" title="Permanenantly delete this account">Delete Profile</button>
317
									<button name="cmd" value="delete" type="submit" class="btn btn-danger" title="Permanently delete this account">Delete Profile</button>
315
                                </div>
318
                                </div>
316
                            </div>
319
                            </div>
317
                        </form>
320
                        </form>

+ 8 - 3
site.go

4
	"crypto/sha512"
4
	"crypto/sha512"
5
	"fmt"
5
	"fmt"
6
	"log"
6
	"log"
7
	"math"
7
	"regexp"
8
	"regexp"
8
	"strings"
9
	"strings"
9
)
10
)
37
38
38
func (s *Site) applyCriteria(sha []byte) []byte {
39
func (s *Site) applyCriteria(sha []byte) []byte {
39
	hash := []byte(fmt.Sprintf("%x", sha))
40
	hash := []byte(fmt.Sprintf("%x", sha))
41
	hashUpper := []byte(fmt.Sprintf("%X", sha))
40
42
41
	if !containsUppercase(hash, s.NumberOfUpperCase) {
43
	if !containsUppercase(hash, s.NumberOfUpperCase) {
42
		i := 0
44
		i := 0
46
		if matches = r.FindAllIndex(hash, -1); matches != nil {
48
		if matches = r.FindAllIndex(hash, -1); matches != nil {
47
			for _, v := range matches {
49
			for _, v := range matches {
48
				if i < s.NumberOfUpperCase {
50
				if i < s.NumberOfUpperCase {
49
					c := strings.ToUpper(string(hash[v[0]]))
51
					c := string(hashUpper[v[0]])
50
					hash[v[0]] = []byte(c)[0]
52
					hash[v[0]] = []byte(c)[0]
51
					i += 1
53
					i += 1
52
				}
54
				}
62
		if matches = r.FindAllIndex(hash, -1); matches != nil {
64
		if matches = r.FindAllIndex(hash, -1); matches != nil {
63
			for _, v := range matches {
65
			for _, v := range matches {
64
				if i < s.NumberOfDigits {
66
				if i < s.NumberOfDigits {
65
					hash[v[0]] = byte(i)
67
					ceiling := float64(10)
68
					hash[v[0]] = byte(ceiling - math.Mod(float64(i), ceiling) - 1)
66
					i += 1
69
					i += 1
67
				}
70
				}
68
			}
71
			}
72
	if !containsSpecialCharacters(hash, s.SpecialCharacters, s.NumberOfSpecialCharacters) {
75
	if !containsSpecialCharacters(hash, s.SpecialCharacters, s.NumberOfSpecialCharacters) {
73
		i := 0
76
		i := 0
74
		r := regexp.MustCompile(`[0-9]+`)
77
		r := regexp.MustCompile(`[0-9]+`)
78
		l := len(s.SpecialCharacters)
75
79
76
		var matches [][]int
80
		var matches [][]int
77
		if matches = r.FindAllIndex(hash, -1); matches != nil {
81
		if matches = r.FindAllIndex(hash, -1); matches != nil {
78
			for _, v := range matches {
82
			for _, v := range matches {
79
				if i < s.NumberOfSpecialCharacters {
83
				if i < s.NumberOfSpecialCharacters {
80
					i += 1
84
					i += 1
81
					hash[v[0]] = []byte(s.SpecialCharacters)[len(s.SpecialCharacters)-i]
85
					idx := l - int(math.Mod(float64(i), float64(l)))
86
					hash[v[0]] = []byte(s.SpecialCharacters)[idx]
82
				}
87
				}
83
			}
88
			}
84
		}
89
		}