Initial checkin

This commit is contained in:
Winni Neessen 2021-09-19 18:10:12 +02:00
parent 61ef8912c4
commit f2941917d0
7 changed files with 90 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View file

@ -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/

9
.idea/go-hibp.iml Normal file
View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View file

@ -0,0 +1,12 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="GrazieInspection" enabled="false" level="TYPO" enabled_by_default="false" />
<inspection_tool class="LanguageDetectionInspection" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
<option name="processCode" value="true" />
<option name="processLiterals" value="true" />
<option name="processComments" value="true" />
</inspection_tool>
</profile>
</component>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/go-hibp.iml" filepath="$PROJECT_DIR$/.idea/go-hibp.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module github.com/wneessen/go-hibp
go 1.17

44
hibp.go Normal file
View file

@ -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
}