-#!/usr/bin/python
+#!/usr/bin/python3
import asyncio
import botocore.exceptions
import datetime
-import hashlib
import logging
-import random
-import string
-import time
from . import base
-from . import misc
from .decorators import *
)
return res.t
-
-
-def generate_password_hash(password, salt=None, algo="sha512"):
- """
- This function creates a salted digest of the given password.
- """
- # Generate the salt (length = 16) of none was given.
- if salt is None:
- salt = misc.generate_random_string(length=16)
-
- # Compute the hash.
- # <SALT> + <PASSWORD>
- if not algo in hashlib.algorithms:
- raise Exception("Unsupported password hash algorithm: %s" % algo)
-
- # Calculate the digest.
- h = hashlib.new(algo)
- h.update(salt)
- h.update(password)
-
- # Output string is of kind "<algo>$<salt>$<hash>".
- return "$".join((algo, salt, h.hexdigest()))
-
-def check_password_hash(password, password_hash):
- """
- Check a plain-text password with the given digest.
- """
- # Handle plaintext passwords (plain$<password>).
- if password_hash.startswith("plain$"):
- return password_hash[6:] == password
-
- try:
- algo, salt, digest = password_hash.split("$", 2)
- except ValueError:
- logging.warning("Unknown password hash: %s" % password_hash)
- return False
-
- # Re-generate the password hash and compare the result.
- return password_hash == generate_password_hash(password, salt=salt, algo=algo)