Skip to content

Instantly share code, notes, and snippets.

@dhensby
Created July 11, 2018 00:04
Show Gist options
  • Select an option

  • Save dhensby/59036ecffda6c02a72e9859a87188599 to your computer and use it in GitHub Desktop.

Select an option

Save dhensby/59036ecffda6c02a72e9859a87188599 to your computer and use it in GitHub Desktop.

Revisions

  1. dhensby created this gist Jul 11, 2018.
    36 changes: 36 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    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)
    }
    }
    52 changes: 52 additions & 0 deletions password.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    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]
    }