-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_passwords.py
More file actions
21 lines (18 loc) · 1.01 KB
/
hash_passwords.py
File metadata and controls
21 lines (18 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from app import app # Import your Flask app instance
from models import db, User # Adjust the import based on your project structure
from werkzeug.security import generate_password_hash
def hash_existing_passwords():
"""Hash plaintext passwords for all users and update the database."""
with app.app_context(): # Ensure the Flask app context is active
users = User.query.all() # Fetch all users in the database
for user in users:
# Check if the password is already hashed
if len(user.password) < 60: # Assume plaintext passwords are shorter than 60 characters
hashed_password = generate_password_hash(user.password) # Use default 'pbkdf2:sha256'
user.password = hashed_password
print(f"Updated password for user: {user.username}")
# Commit the changes to the database
db.session.commit()
print("All plaintext passwords have been hashed!")
if __name__ == "__main__":
hash_existing_passwords()