-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
hibp.go
49 lines (39 loc) · 789 Bytes
/
hibp.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
package crunchy
import (
"crypto/sha1"
"encoding/hex"
"io/ioutil"
"net"
"net/http"
"strings"
"time"
)
var HttpClient = &http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
}).Dial,
ResponseHeaderTimeout: 10 * time.Second,
},
}
func foundInHIBP(s string) error {
h := sha1.New()
h.Write([]byte(s))
result := hex.EncodeToString(h.Sum(nil))
firstFive := result[0:5]
restOfHash := strings.ToUpper(result[5:])
url := "https://api.pwnedpasswords.com/range/" + firstFive
resp, err := HttpClient.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if strings.Index(string(body), restOfHash) > -1 {
return ErrFoundHIBP
}
return nil
}