-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (56 loc) · 1.15 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
package main
import (
"bufio"
"context"
"crypto/sha1"
"encoding/hex"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strings"
"golang.org/x/term"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
log.SetFlags(0)
fmt.Print("Password to check: ")
password, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Fatalln(err)
}
h := sha1.New()
h.Write(password)
hash := h.Sum(nil)
hashedPassword := strings.ToUpper(hex.EncodeToString(hash))
fmt.Println("**********")
prefix := hashedPassword[:5]
suffix := hashedPassword[5:]
baseURL := "https://api.pwnedpasswords.com/range"
fullURL := fmt.Sprintf("%s/%s", baseURL, prefix)
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fullURL,
http.NoBody,
)
if err != nil {
log.Fatalln(err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalln(err)
}
defer res.Body.Close()
scanner := bufio.NewScanner(res.Body)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, suffix) {
fmt.Println("Uhoh, password is pwned")
return
}
}
fmt.Println("Password is safe!")
}