Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create send_email.py #1748

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions python/scripts/password_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python3

import requests
import hashlib
import sys


def request_api_data(query_char):
url = "https://api.pwnedpasswords.com/range/" + query_char
res = requests.get(url)
if res.status_code != 200:
raise RuntimeError(
f"Error fetching: {res.status_code}, check the api and try again"
)
return res


def get_password_leaks_count(hashes, hash_to_check):
hashes = (line.split(":") for line in hashes.text.splitlines())
for h, count in hashes:
if h == hash_to_check:
return count
return 0


def pwned_api_check(password):
sha1password = hashlib.sha1(password.encode("utf-8")).hexdigest().upper()
first5_char, tail = sha1password[:5], sha1password[5:]
response = request_api_data(first5_char)
return get_password_leaks_count(response, tail)


def main(args):
"""
API for password checker
"""
for password in args:
count = pwned_api_check(password)
if count:
print(
f"{password} was found {count} times... you should probably change your password!"
)
else:
print(f"{password} was NOT found. Carry on!")
return "done!"


if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
25 changes: 25 additions & 0 deletions python/scripts/send_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python3

import smtplib
from email.message import EmailMessage
from string import Template
from pathlib import Path


def send_email():
"""
Send email to a user
"""
html = Template(Path("index.html").read_text())
email = EmailMessage()
email["from"] = "John Doe"
email["to"] = "<to email address>"
email["subject"] = "You won 1,000,000 dollars!"

email.set_content(html.substitute({"name": "Jenny Doe"}), "html")

with smtplib.SMTP(host="smtp.gmail.com", port=587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.login("<your email address>", "<your password>")
smtp.send_message(email)