-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (68 loc) · 1.65 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"bufio"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
)
var wordlist10kHashes []string
func passwordCheckHandler(response http.ResponseWriter, request *http.Request) {
responseMap := map[string]interface{}{}
if request.Method == "POST" {
request.ParseForm()
testHash := request.Form.Get("hash")
if len(testHash) == 0 {
testWord := request.Form.Get("password")
if len(testWord) == 0 {
response.WriteHeader(400)
return
}
testHash = hashPassword(testWord)
}
responseMap["common"] = findHash(testHash)
} else {
responseMap["error"] = "Bad Request"
response.WriteHeader(404)
}
// Convert response to JSON and send it
responseJson, _ := json.Marshal(responseMap)
response.Write([]byte(responseJson))
}
func hashPassword(testWord string) string {
hasher := sha1.New()
hasher.Write([]byte(testWord))
return hex.EncodeToString(hasher.Sum(nil))
}
func findHash(testHash string) bool {
// Look for it in the list of hashes
for _, hash := range wordlist10kHashes {
if hash == testHash {
return true
}
}
return false
}
func loadHashes() {
fmt.Println("Reading Hash List...")
file, err := os.Open("data/10k-most-common-sha1.txt")
if err != nil {
log.Fatalf("couldn't open wordlist")
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
wordlist10kHashes = append(wordlist10kHashes, scanner.Text())
}
}
func main() {
loadHashes()
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir("static")))
mux.HandleFunc("/password-check", passwordCheckHandler)
log.Fatal(http.ListenAndServe(":8080", mux))
}