Advertisement
Guest User

Untitled

a guest
Mar 16th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import hashlib
  5. import os
  6. import sqlite3
  7. import sys
  8. from getpass import getpass
  9.  
  10. from config import DB_PATH, PASSWORD_LENGTH_MIN, HASH_ALGORITHM
  11.  
  12.  
  13. if len(sys.argv) != 2:
  14. print("USAGE: %s <username>" % sys.argv[0])
  15. sys.exit(1)
  16. if not os.path.exists(DB_PATH):
  17. print("ERROR: Database not found: %s" % DB_PATH)
  18.  
  19. hash_func = getattr(hashlib, HASH_ALGORITHM, None)
  20. if hash_func is None:
  21. print("ERROR: Hashing algorithm '%s' not found" % HASH_ALGORITHM)
  22. sys.exit(2)
  23.  
  24. username = sys.argv[1]
  25. password_ok = False
  26. while not password_ok:
  27. password = getpass()
  28. if len(password) < PASSWORD_LENGTH_MIN:
  29. print("ERROR: password must be at least %d characters long" % PASSWORD_LENGTH_MIN)
  30. continue
  31. password_confirm = getpass('Confirm: ')
  32. if password == password_confirm:
  33. password_ok = True
  34. else:
  35. print("ERROR: passwords don't match")
  36.  
  37. password = hash_func(password.encode("UTF-8")).hexdigest()
  38.  
  39. db = sqlite3.connect(DB_PATH)
  40. cursor = db.cursor()
  41. try:
  42. cursor.execute("INSERT INTO users VALUES (?, ?);", (username, password))
  43. except sqlite3.IntegrityError:
  44. print("ERROR: user '%s' already exists" % username)
  45. sys.exit(2)
  46. db.commit()
  47.  
  48. print("* User %s successfully created" % username)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement