Skip to content

Commit 5582fe7

Browse files
committed
William Spader | is-my-password-safe folder, Main.go, README.md | adding is-my-password-safe folder, project code file Main.go and README.md
1 parent b18233d commit 5582fe7

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

is-my-password-safe/Main.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package main
2+
3+
import (
4+
"crypto/sha1"
5+
"encoding/hex"
6+
"encoding/json"
7+
"io/ioutil"
8+
"log"
9+
"net/http"
10+
"strings"
11+
12+
"github.com/aws/aws-lambda-go/events"
13+
"github.com/aws/aws-lambda-go/lambda"
14+
)
15+
16+
type pwdRequest struct {
17+
Password string `json:"password"`
18+
}
19+
20+
type pwdResponse struct {
21+
Password string `json:"password"`
22+
Occurrences string `json:"occurrences"`
23+
Sha1 string `json:"sha1"`
24+
}
25+
26+
func router(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
27+
log.Printf("req-method: %s", req.HTTPMethod)
28+
if req.Path == "/check-password" {
29+
if req.HTTPMethod == "POST" {
30+
return checkPasswordHandler(req)
31+
}
32+
}
33+
return events.APIGatewayProxyResponse{
34+
StatusCode: http.StatusMethodNotAllowed,
35+
Body: http.StatusText(http.StatusMethodNotAllowed),
36+
}, nil
37+
}
38+
39+
func checkPasswordHandler(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
40+
var request pwdRequest
41+
err := json.Unmarshal([]byte(req.Body), &request)
42+
if err != nil {
43+
return events.APIGatewayProxyResponse{
44+
StatusCode: http.StatusInternalServerError,
45+
Body: http.StatusText(http.StatusInternalServerError),
46+
}, nil
47+
}
48+
response, err := json.Marshal(checkPasswordService(request))
49+
if err != nil {
50+
return events.APIGatewayProxyResponse{
51+
StatusCode: http.StatusInternalServerError,
52+
Body: http.StatusText(http.StatusInternalServerError),
53+
}, nil
54+
}
55+
return events.APIGatewayProxyResponse{
56+
StatusCode: http.StatusOK,
57+
Body: string(response),
58+
}, nil
59+
}
60+
61+
func checkPasswordService(request pwdRequest) pwdResponse {
62+
var api string = "https://api.pwnedpasswords.com/range/"
63+
pwd := request.Password
64+
hash := sha1.New()
65+
hash.Write([]byte(pwd))
66+
result := strings.ToUpper(hex.EncodeToString(hash.Sum(nil)))
67+
response, err := http.Get(api + result[0:5])
68+
if err != nil {
69+
log.Fatalln(err)
70+
}
71+
defer response.Body.Close()
72+
orig := result
73+
result = result[5:len(result)]
74+
contents, err := ioutil.ReadAll(response.Body)
75+
if err != nil {
76+
log.Fatalln(err)
77+
}
78+
passwords := strings.Split(string(contents), "\n")
79+
pwdMap := make(map[string]string)
80+
var currentSplit []string
81+
for i := 0; i < len(passwords); i++ {
82+
currentSplit = strings.Split(passwords[i], ":")
83+
pwdMap[currentSplit[0]] = currentSplit[1][0 : len(currentSplit[1])-1]
84+
}
85+
checkPwd := pwdMap[result]
86+
var httpResponse pwdResponse
87+
if pwdMap[result] != "" {
88+
httpResponse.Password = pwd
89+
httpResponse.Occurrences = checkPwd
90+
httpResponse.Sha1 = orig
91+
}
92+
return httpResponse
93+
}
94+
func main() {
95+
lambda.Start(router)
96+
}

is-my-password-safe/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# IS MY PASWORD SAFE
2+
3+
### What is it
4+
5+
Return how many times a password has been leaked. It uses haveibeenpwned API.
6+
7+
### Amazon Lambda
8+
9+
Send a HTTP Post to the below AWS Lambda
10+
11+
API link: https://1lt61j4agg.execute-api.sa-east-1.amazonaws.com/prod/check-password
12+
13+
#### Input
14+
```
15+
{
16+
"password": "Password1"
17+
}
18+
```
19+
#### Output
20+
```
21+
{
22+
"password": "Password1",
23+
"occurrences": "118930",
24+
"sha1": "70CCD9007338D6D81DD3B6271621B9CF9A97EA00"
25+
}
26+
```

0 commit comments

Comments
 (0)