Created
July 11, 2018 00:04
-
-
Save dhensby/59036ecffda6c02a72e9859a87188599 to your computer and use it in GitHub Desktop.
Pwned Passwords API - Go CLI script to test your passwords locally
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
humanize "github.com/dustin/go-humanize" | |
) | |
var ( | |
password string | |
) | |
func main() { | |
if len(os.Args) != 2 { | |
fmt.Printf("Usage: %s '<password>'\n", os.Args[0]) | |
os.Exit(1) | |
} | |
password = os.Args[1] | |
hash := getSha1(password) | |
count := getHashHits(hash) | |
if count > 0 { | |
fmt.Printf("HIT: %s hit", humanize.Comma(count)) | |
if count != 1 { | |
fmt.Print("s") | |
} | |
fmt.Println("") | |
os.Exit(1) | |
} else { | |
fmt.Println("MISS") | |
os.Exit(0) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/hex" | |
"log" | |
"net/http" | |
"crypto/sha1" | |
"strings" | |
"bufio" | |
"strconv" | |
) | |
const ( | |
apiURL = "https://api.pwnedpasswords.com/range/" | |
) | |
func getHashHits(hash string) (int64) { | |
resp, err := http.Get(apiURL + hash[0:5]) | |
if err != nil { | |
log.Fatalf("Error getting hash") | |
} | |
defer resp.Body.Close() | |
scanner := bufio.NewScanner(resp.Body) | |
for scanner.Scan() { | |
candidate, count := splitLine(scanner.Text()) | |
if candidate == hash[5:] { | |
countInt, err := strconv.ParseInt(count, 10, 64) | |
if err != nil { | |
log.Fatalf("Couldn't convert count to int") | |
} | |
return countInt | |
} | |
} | |
return 0 | |
} | |
func getSha1(tohash string) (string) { | |
data := []byte(tohash) | |
h := sha1.New() | |
h.Write(data) | |
return strings.ToUpper(hex.EncodeToString(h.Sum(nil))); | |
} | |
func splitLine(line string) (string, string) { | |
x := strings.SplitN(line, ":", 2) | |
return x[0], x[1] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment