summary refs log tree commit diff stats
path: root/hibp/pwned.go
diff options
context:
space:
mode:
authorAndinus <andinus@inventati.org>2020-03-16 17:29:15 +0530
committerAndinus <andinus@inventati.org>2020-03-16 17:29:15 +0530
commitb75359931e3c5baaded6d0addb6d28f750dcd518 (patch)
treed21197d18dad2bc58d081263c0192abc7110edaa /hibp/pwned.go
parent553079dc017a44164cd04d4c067c061003e1684f (diff)
downloadorion-b75359931e3c5baaded6d0addb6d28f750dcd518.tar.gz
Add initial version of orion v0.1.0
Diffstat (limited to 'hibp/pwned.go')
-rw-r--r--hibp/pwned.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/hibp/pwned.go b/hibp/pwned.go
new file mode 100644
index 0000000..57a1727
--- /dev/null
+++ b/hibp/pwned.go
@@ -0,0 +1,40 @@
+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, ""
+}