Skip to content

Commit 1ac6fc0

Browse files
committedMar 19, 2025
hashing
1 parent 7da73a1 commit 1ac6fc0

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed
 

‎README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
3. [Insert & Select](https://github.com/eniompw/FlaskLogin/blob/master/first_db.py)
66
4. [Just Signup](https://github.com/eniompw/FlaskLogin/blob/master/signup.py)
77
5. [Signup & Login](https://github.com/eniompw/FlaskLogin/blob/master/signup%26login.py)
8-
6. [Sessions](https://github.com/eniompw/FlaskLogin/blob/master/sessions.py)
9-
7. [Full Example](https://github.com/eniompw/FlaskLogin/blob/master/main.py)
8+
6. [Hashing](https://github.com/eniompw/FlaskLogin/blob/master/hash.py)
9+
7. [Sessions](https://github.com/eniompw/FlaskLogin/blob/master/sessions.py)
10+
8. [Full Example](https://github.com/eniompw/FlaskLogin/blob/master/main.py)
1011

1112
* [Forms in Flask](https://github.com/eniompw/FormsInFlask)
1213
* [Postgres on Heroku](https://github.com/eniompw/FlaskPostgres)

‎hash.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from flask import Flask, render_template, request
2+
import sqlite3
3+
import hashlib
4+
5+
app = Flask(__name__)
6+
con = sqlite3.connect("Database.db")
7+
cur = con.cursor()
8+
cur.execute(""" CREATE TABLE IF NOT EXISTS User(
9+
Username VARCHAR(20) NOT NULL PRIMARY KEY,
10+
Password VARCHAR(64) NOT NULL);
11+
""")
12+
con.commit()
13+
con.close()
14+
15+
@app.route("/signup", methods=["GET", "POST"])
16+
def signup():
17+
if request.method == "GET":
18+
return render_template("signup.html")
19+
else:
20+
con = sqlite3.connect("login.db")
21+
cur = con.cursor()
22+
encoded = request.form['Password'].encode()
23+
hash = hashlib.sha256(encoded).hexdigest()
24+
cur.execute("INSERT INTO User (Username, Password) VALUES (?,?)",
25+
(request.form['Username'], hash))
26+
con.commit()
27+
con.close()
28+
return "Signup Successful"
29+
30+
@app.route("/", methods=["GET", "POST"])
31+
def login():
32+
if request.method == "GET":
33+
return render_template("index.html")
34+
else:
35+
con = sqlite3.connect('login.db')
36+
cur = con.cursor()
37+
encoded = request.form['Password'].encode()
38+
hash = hashlib.sha256(encoded).hexdigest()
39+
cur.execute("SELECT * FROM User WHERE Username=? AND Password=?",
40+
(request.form['Username'], hash))
41+
if len(cur.fetchall()) == 0:
42+
return "Wrong username and password"
43+
else:
44+
return "Welcome " + request.form['Username']
45+
46+
if __name__ == "__main__":
47+
app.run(debug=True)

0 commit comments

Comments
 (0)
Please sign in to comment.