-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (58 loc) · 1.71 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
// Takes a password as a command line arg and prints the number of times it has been pwned according to haveibeenpwned
// Example: ./haveIBeenPwned Password1
// Output: Your password has been pwned 111658 times
package main
import (
"crypto/sha1"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
/*
* TODO: Readme
*/
// CreateHash returns an sha1 hash of string.
func CreateHash(str string) string {
hash := sha1.New()
hash.Write([]byte(str))
return strings.ToUpper(fmt.Sprintf("%x", hash.Sum(nil)))
}
// GetPwnedHashes gets a list of hashes from the pwnedpasswords api that are similair to the hastr.
// The pwnedpasswords api only requires the first 5 characters of the hash and returns all similair hashes.
func GetPwnedHashes(hashstr string) string {
res, err := http.Get(fmt.Sprintf("https://api.pwnedpasswords.com/range/%s", hashstr[0:5]))
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
return string(body)
}
// FindPwnedPassword finds the given hash in a string of pwnedHashes.
// The pwnedHashes are expected to be formatted as HASH:TIMES_PWNED\r\n
func FindPwnedPassword(pwnedHashes string, hashstr string) string {
for _, value := range strings.Split(pwnedHashes, "\r\n") {
pwnedHash := strings.Split(value, ":")
if pwnedHash[0] == hashstr[5:] {
return pwnedHash[1]
}
}
return ""
}
func main() {
input := os.Args[1]
hashstr := CreateHash(input)
pwnedHashes := GetPwnedHashes(hashstr)
passwordPwnedCount := FindPwnedPassword(pwnedHashes, hashstr)
if passwordPwnedCount != "" {
fmt.Printf("Your password has been pwned %s times", passwordPwnedCount)
} else {
fmt.Printf("Your password has not been pwned")
}
}