Skip to content

Commit

Permalink
Added hashing to teacher password
Browse files Browse the repository at this point in the history
  • Loading branch information
jonager committed Aug 31, 2017
1 parent a1be175 commit 5b26041
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
24 changes: 24 additions & 0 deletions hash_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from hashlib import sha256
from random import choice
POSTS_PER_PAGE = 3


def make_salt():
'''returns a salt '''
alpha_digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
return ''.join([choice(alpha_digits) for x in range(16)])

def make_hash(value, salt = None):
''' returns value hashed and salted, and salt'''
if not salt:
salt = make_salt()
hashed = sha256(str.encode(value + salt)).hexdigest()
return '{0},{1}'.format(hashed, salt)

def check_hash(user_input, hash_db):
'''returns True if both values are equal'''
salt = hash_db.split(',')[1]
if make_hash(user_input, salt) == hash_db:
return True
else:
return False
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# from sqlalchemy.sql import func
from app import app, db
from models import Student, Teacher, Attendance
from hash_tools import make_hash, check_hash

# Main View
@app.route('/')
Expand Down
3 changes: 2 additions & 1 deletion models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from app import db
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime, date
from hash_tools import make_hash, check_hash

class Student(db.Model):
id = db.Column(db.Integer, primary_key=True)
Expand Down Expand Up @@ -31,7 +32,7 @@ def __init__(self, first_name, last_name, email, password):
self.first_name = first_name
self.last_name = last_name
self.email = email
self.password = password
self.password = make_hash(password)


class Attendance(db.Model):
Expand Down

0 comments on commit 5b26041

Please sign in to comment.