-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (133 loc) · 3.77 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"github.com/olekukonko/tablewriter"
"github.com/schollz/progressbar/v2"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
)
type PasswordFile struct {
Passwords []Password `json:"AUTHENTIFIANT"`
}
type Password struct {
Password string `json:"password"`
Title string `json:"title"`
}
type PasswordCheckResult uint8
const (
ResultSafe PasswordCheckResult = 0
ResultCompromised PasswordCheckResult = 1
ResultUnchecked PasswordCheckResult = 2
)
func main() {
validateArgs()
passwordFile := fetchPasswordFile(os.Args[1])
compromised, unchecked, safe := processPasswords(passwordFile)
renderResults(compromised, unchecked, safe)
}
func validateArgs() {
flag.Parse()
if flag.NArg() < 1 || flag.Arg(0) == "" {
log.Fatal("Usage: ./dashlane-have-i-been-pwned /path/to/passwords.json")
}
}
func fetchPasswordFile(passwordFileLocation string) *PasswordFile {
passwordFile := PasswordFile{}
passwordFileBytes, err := ioutil.ReadFile(passwordFileLocation)
if err != nil {
log.Fatalf("Unable to read file %s", passwordFileLocation)
}
err = json.Unmarshal(passwordFileBytes, &passwordFile)
if err != nil {
log.Fatalf("Unable to parse password file. Is it valid JSON?")
}
return &passwordFile
}
func processPasswords(passwordFile *PasswordFile) ([]string, []string, int) {
bar := progressbar.New(len(passwordFile.Passwords))
var compromised []string
var unchecked []string
safe := 0
for _, password := range passwordFile.Passwords {
result := checkPasswordHash(password.Title, password.Password)
switch result {
case ResultCompromised:
compromised = append(compromised, fmt.Sprintf("%s: %s", password.Title, password.Password))
checkErr(bar.Add(1))
case ResultUnchecked:
unchecked = append(unchecked, password.Title)
checkErr(bar.Add(1))
case ResultSafe:
safe++
checkErr(bar.Add(1))
default:
checkErr(bar.Add(1))
}
}
return compromised, unchecked, safe
}
func checkPasswordHash(title string, password string) PasswordCheckResult {
hash := sha1.New()
passwordBytes := []byte(password)
hash.Write(passwordBytes)
passwordHash := hex.EncodeToString(hash.Sum(nil))
resp, err := http.Get("https://api.pwnedpasswords.com/range/" + passwordHash[:5])
checkErr(err)
if resp.StatusCode == 200 {
bodyBytes, err := ioutil.ReadAll(resp.Body)
checkErr(err)
lines := strings.Split(strings.ToLower(string(bodyBytes)), "\n")
for _, line := range lines {
remoteHash := line[:35]
if strings.EqualFold(remoteHash, passwordHash[5:40]) {
return ResultCompromised
}
}
return ResultSafe
} else if resp.StatusCode == 429 {
time.Sleep(1500)
return checkPasswordHash(title, password)
} else {
return ResultUnchecked
}
}
func renderResults(compromised []string, unchecked []string, safe int) {
fmt.Println()
fmt.Println()
renderStatsTable(compromised, unchecked, safe)
renderTitleTable("Compromised", compromised)
renderTitleTable("Unchecked", unchecked)
}
func renderStatsTable(compromised []string, unchecked []string, safe int) {
writer := tablewriter.NewWriter(os.Stdout)
writer.SetHeaderAlignment(tablewriter.ALIGN_CENTER)
writer.SetHeader([]string{"Stats"})
writer.Append([]string{"Compromised", fmt.Sprintf("%v", len(compromised))})
writer.Append([]string{"Unchecked", fmt.Sprintf("%v", len(unchecked))})
writer.Append([]string{"Safe", fmt.Sprintf("%v", safe)})
writer.Render()
}
func renderTitleTable(title string, values []string) {
if len(values) > 0 {
writer := tablewriter.NewWriter(os.Stdout)
writer.SetHeaderAlignment(tablewriter.ALIGN_CENTER)
writer.SetHeader([]string{title})
for _, value := range values {
writer.Append([]string{value})
}
writer.Render()
}
}
func checkErr(err error) {
if err != nil {
log.Panic(err)
}
}