diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/go-hibp.iml b/.idea/go-hibp.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/go-hibp.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..09dbac8 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,12 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..082fe35 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a65045d --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/wneessen/go-hibp + +go 1.17 diff --git a/hibp.go b/hibp.go new file mode 100644 index 0000000..b3fee5a --- /dev/null +++ b/hibp.go @@ -0,0 +1,44 @@ +package go_hibp + +import ( + "bufio" + "crypto/sha1" + "fmt" + "net/http" + "strings" + "time" +) + +// HIBPUrl represents the main API url for the HIBP password API +const HIBPUrl = "https://api.pwnedpasswords.com/range/" + +// Check queries the HIBP database and checks if a given string is was found +func Check(p string) (ip bool, err error) { + shaSum := fmt.Sprintf("%x", sha1.Sum([]byte(p))) + fp := shaSum[0:5] + sp := shaSum[5:] + ip = false + + httpClient := &http.Client{Timeout: time.Second * 2} + httpRes, err := httpClient.Get(HIBPUrl + fp) + if err != nil { + return false, err + } + defer func() { + err = httpRes.Body.Close() + }() + + scanObj := bufio.NewScanner(httpRes.Body) + for scanObj.Scan() { + scanLine := strings.SplitN(scanObj.Text(), ":", 2) + if strings.ToLower(scanLine[0]) == sp { + ip = true + break + } + } + if err := scanObj.Err(); err != nil { + return ip, err + } + + return ip, nil +}