diff options
Diffstat (limited to 'hibp')
-rw-r--r-- | hibp/hash.go | 15 | ||||
-rw-r--r-- | hibp/pwned.go | 40 | ||||
-rw-r--r-- | hibp/req.go | 56 |
3 files changed, 0 insertions, 111 deletions
diff --git a/hibp/hash.go b/hibp/hash.go deleted file mode 100644 index 9b8d35c..0000000 --- a/hibp/hash.go +++ /dev/null @@ -1,15 +0,0 @@ -package hibp - -import ( - "crypto/sha1" - "encoding/hex" - "strings" -) - -// GetSHA1Hash takes a string as an input & returns SHA-1 Hash -func GetSHA1Hash(pass string) string { - alg := sha1.New() - alg.Write([]byte(pass)) - - return strings.ToUpper(hex.EncodeToString(alg.Sum(nil))) -} diff --git a/hibp/pwned.go b/hibp/pwned.go deleted file mode 100644 index 57a1727..0000000 --- a/hibp/pwned.go +++ /dev/null @@ -1,40 +0,0 @@ -package hibp - -import ( - "fmt" - "strings" -) - -// GetPwned takes SHA-1 Hash as input & returns Pwned Passwords list -// returned by the Have I Been Pwned API -func GetPwned(hsh string) (map[string]string, error) { - api := "https://api.pwnedpasswords.com/range" - list := make(map[string]string) - - pfx := hsh[:5] - - reqApi := fmt.Sprintf("%s/%s", api, pfx) - body, err := reqHIBP(reqApi) - if err != nil { - return list, fmt.Errorf("reqHIBP failed\n%s", - err.Error()) - } - - for _, v := range strings.Split(body, "\r\n") { - s := strings.Split(v, ":") - list[s[0]] = s[1] - } - return list, err -} - -// ChkPwn takes list, hash as input & returns if the hash is in list, -// the frequency -func ChkPwn(list map[string]string, hsh string) (bool, string) { - sfx := hsh[5:] - for k, fq := range list { - if sfx == k { - return true, fq - } - } - return false, "" -} diff --git a/hibp/req.go b/hibp/req.go deleted file mode 100644 index 8081d3c..0000000 --- a/hibp/req.go +++ /dev/null @@ -1,56 +0,0 @@ -package hibp - -import ( - "fmt" - "io/ioutil" - "net/http" - "time" -) - -func reqHIBP(reqApi string) (body string, err error) { - c := http.Client{ - // TODO: timeout should be configurable by the user - Timeout: time.Second * 64, - } - - req, err := http.NewRequest(http.MethodGet, reqApi, nil) - if err != nil { - err = fmt.Errorf("hibp/req.go: Failed to create request\n%s", - err.Error()) - return - } - - // User-Agent should be passed with every request to - // make work easier for the server handler. Include contact - // information along with the project name so they could reach - // you if required. - req.Header.Set("User-Agent", - "Andinus / Orion - https://andinus.nand.sh/orion") - - res, err := c.Do(req) - if err != nil { - err = fmt.Errorf("hibp/req.go: Failed to get response\n%s", - err.Error()) - return - } - defer res.Body.Close() - - if res.StatusCode != 200 { - fmt.Errorf("hibp/req.go: Unexpected response status code received: %d %s", - res.StatusCode, - http.StatusText(res.StatusCode)) - return - } - - // This will read everything to memory and is okay to use here - // because the response is expected to be small. - out, err := ioutil.ReadAll(res.Body) - if err != nil { - fmt.Errorf("hibp/req.go: Failed to read res.Body\n%s", - err.Error()) - return - } - - body = string(out) - return -} |