Esempio n. 1
0
def password_match(password_hash, password, salt=""):
    """
    Checks if the given password hash value matched
    the given password using the given (optional) salt.
    The matching process executes the original hash function
    in order to check for same results.

    :type password_hash: String
    :param password_hash: The complete password hash hexadecimal string.
    :type password: String
    :param password: The base password for checking.
    :type salt: String
    :param salt: The base salt for checking.
    :rtype: bool
    :return: The result of the password match checking.
    """

    # tries to match the base password hash
    base_password_match = PASSWORD_VALUE_REGEX.match(password_hash)

    # retrieves the base password hash and value
    base_password_hash = base_password_match.group(HASH_VALUE)
    base_password_value = base_password_match.group(VALUE_VALUE)

    # creates the password (word) from the
    # password an the salt (secure work)
    password_word = password + salt

    # sets the initial value for the passwords
    # math result
    passwords_match = False

    # in case the base password hash is of type plain
    if base_password_hash == PLAIN_VALUE:
        # checks if both passwords match
        passwords_match = password_word == base_password_value
    # otherwise it must be a general hash function
    else:
        # creates the new hash object from the
        # base password hash (method)
        hash = hashlib.new(base_password_hash)

        # updates the hash value with the
        # password word, note that the value
        # is ensured to be a valid byte value
        password_word = legacy.bytes(password_word)
        hash.update(password_word)

        # retrieves the hash value from the
        # hex digest
        hash_value = hash.hexdigest()

        # checks if both password (hashes) match
        passwords_match = hash_value == base_password_value

    # returns if both password match
    return passwords_match
Esempio n. 2
0
def password_match(password_hash, password, salt = ""):
    """
    Checks if the given password hash value matched
    the given password using the given (optional) salt.
    The matching process executes the original hash function
    in order to check for same results.

    @type password_hash: String
    @param password_hash: The complete password hash hexadecimal string.
    @type password: String
    @param password: The base password for checking.
    @type salt: String
    @param salt: The base salt for checking.
    @rtype: bool
    @return: The result of the password match checking.
    """

    # tries to match the base password hash
    base_password_match = PASSWORD_VALUE_REGEX.match(password_hash)

    # retrieves the base password hash and value
    base_password_hash = base_password_match.group(HASH_VALUE)
    base_password_value = base_password_match.group(VALUE_VALUE)

    # creates the password (word) from the
    # password an the salt (secure work)
    password_word = password + salt

    # sets the initial value for the passwords
    # math result
    passwords_match = False

    # in case the base password hash is of type plain
    if base_password_hash == PLAIN_VALUE:
        # checks if both passwords match
        passwords_match = password_word == base_password_value
    # otherwise it must be a general hash function
    else:
        # creates the new hash object from the
        # base password hash (method)
        hash = hashlib.new(base_password_hash)

        # updates the hash value with the
        # password word, note that the value
        # is ensured to be a valid byte value
        password_word = legacy.bytes(password_word)
        hash.update(password_word)

        # retrieves the hash value from the
        # hex digest
        hash_value = hash.hexdigest()

        # checks if both password (hashes) match
        passwords_match = hash_value == base_password_value

    # returns if both password match
    return passwords_match
Esempio n. 3
0
def password_crypt(password, salt="", hash_method=MD5_VALUE):
    """
    Encrypts the given password using the provided hash method.
    An optional salt may be provided for extra security.
    The generated hash is always defined in hexadecimal.

    :type password: String
    :param password: The password to be encrypted using
    the hash method.
    :type salt: String
    :param salt: The salt to be used during the encryption
    process.
    :type hash_method: String
    :param hash_method: The name of the hash method to be used
    for encryption.
    :rtype: String
    :return: The generated (complete) hash hexadecimal string.
    """

    # converts the name of the hash method to lower
    # case string
    hash_method_lower = hash_method.lower()

    # creates the password (word) from the
    # password an the salt
    password_word = password + salt

    # in case the hash method is of type plain
    if hash_method_lower == PLAIN_VALUE:
        # sets the hash value as the (base)
        # password word value (plain)
        hash_value = password_word
    # otherwise it must be a general hash function
    else:
        # creates the new hash object from the
        # hash method
        hash = hashlib.new(hash_method)

        # converts the password word into a bytes
        # based string (if required) and updates
        # the hash value with the password word
        password_word = legacy.bytes(password_word)
        hash.update(password_word)

        # retrieves the hash value from the
        # hex digest
        hash_value = hash.hexdigest()

    # creates the final password hash prepending the
    # hash method reference
    password_hash = "{" + hash_method_lower + "}" + hash_value

    # returns the password (final) hash
    # value (with the hash method prefix)
    return password_hash
Esempio n. 4
0
def password_crypt(password, salt = "", hash_method = MD5_VALUE):
    """
    Encrypts the given password using the provided hash method.
    An optional salt may be provided for extra security.
    The generated hash is always defined in hexadecimal.

    @type password: String
    @param password: The password to be encrypted using
    the hash method.
    @type salt: String
    @param salt: The salt to be used during the encryption
    process.
    @type hash_method: String
    @param hash_method: The name of the hash method to be used
    for encryption.
    @rtype: String
    @return: The generated (complete) hash hexadecimal string.
    """

    # converts the name of the hash method to lower
    # case string
    hash_method_lower = hash_method.lower()

    # creates the password (word) from the
    # password an the salt
    password_word = password + salt

    # in case the hash method is of type plain
    if hash_method_lower == PLAIN_VALUE:
        # sets the hash value as the (base)
        # password word value (plain)
        hash_value = password_word
    # otherwise it must be a general hash function
    else:
        # creates the new hash object from the
        # hash method
        hash = hashlib.new(hash_method)

        # converts the password word into a bytes
        # based string (if required) and updates
        # the hash value with the password word
        password_word = legacy.bytes(password_word)
        hash.update(password_word)

        # retrieves the hash value from the
        # hex digest
        hash_value = hash.hexdigest()

    # creates the final password hash prepending the
    # hash method reference
    password_hash = "{" + hash_method_lower + "}" + hash_value

    # returns the password (final) hash
    # value (with the hash method prefix)
    return password_hash
Esempio n. 5
0
__license__ = "Apache License, Version 2.0"
""" The license for the module """

from colony.base import legacy

from . import string_buffer_util

QUOTE_SAFE_CHAR = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-"
""" The string containing all the safe characters to be quoted """

QUOTE_SAFE_MAPS = {}
""" The map of cached (buffered) safe lists to be quoted """

HEX_TO_CHAR_MAP = dict(
    (legacy.bytes("%02x" % i), legacy.bytes(chr(i))) for i in range(256))
""" The map associating the hexadecimal byte (256) values
with the integers, the association is done using byte values """

# updates the map with the upper case values
HEX_TO_CHAR_MAP.update(
    (legacy.bytes("%02X" % i), legacy.bytes(chr(i))) for i in range(256))


def quote(string_value, safe="/"):
    """
    Quotes the given string value according to
    the url encoding specification.
    The implementation is based on the python base library.

    @type string_value: String
Esempio n. 6
0
""" The copyright for the module """

__license__ = "Apache License, Version 2.0"
""" The license for the module """

from colony.base import legacy

from . import string_buffer_util

QUOTE_SAFE_CHAR = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-"
""" The string containing all the safe characters to be quoted """

QUOTE_SAFE_MAPS = {}
""" The map of cached (buffered) safe lists to be quoted """

HEX_TO_CHAR_MAP = dict((legacy.bytes("%02x" % i), legacy.bytes(chr(i))) for i in range(256))
""" The map associating the hexadecimal byte (256) values
with the integers, the association is done using byte values """

# updates the map with the upper case values
HEX_TO_CHAR_MAP.update((legacy.bytes("%02X" % i), legacy.bytes(chr(i))) for i in range(256))

def quote(string_value, safe = "/"):
    """
    Quotes the given string value according to
    the url encoding specification.
    The implementation is based on the python base library.

    @type string_value: String
    @param string_value: The string value to be quoted.
    @rtype: String