How to use the emmett._shortcuts.to_bytes function in emmett

To help you get started, we’ve selected a few emmett examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github emmett-framework / emmett / emmett / security.py View on Github external
"""
    Generates hash with the given text using the specified
    digest hashing algorithm
    """
    if not digest_alg:
        raise RuntimeError("simple_hash with digest_alg=None")
    elif not isinstance(digest_alg, str):  # manual approach
        h = digest_alg(text + key + salt)
    elif digest_alg.startswith('pbkdf2'):  # latest and coolest!
        iterations, keylen, alg = digest_alg[7:-1].split(',')
        return pbkdf2_hex(
            to_bytes(text), to_bytes(salt), int(iterations), int(keylen),
            get_digest(alg))
    elif key:  # use hmac
        digest_alg = get_digest(digest_alg)
        h = hmac.new(to_bytes(key + salt), msg=to_bytes(text), digestmod=digest_alg)
    else:  # compatible with third party systems
        h = hashlib.new(digest_alg)
        h.update(to_bytes(text + salt))
    return h.hexdigest()
github emmett-framework / emmett / emmett / security.py View on Github external
"""
    if not digest_alg:
        raise RuntimeError("simple_hash with digest_alg=None")
    elif not isinstance(digest_alg, str):  # manual approach
        h = digest_alg(text + key + salt)
    elif digest_alg.startswith('pbkdf2'):  # latest and coolest!
        iterations, keylen, alg = digest_alg[7:-1].split(',')
        return pbkdf2_hex(
            to_bytes(text), to_bytes(salt), int(iterations), int(keylen),
            get_digest(alg))
    elif key:  # use hmac
        digest_alg = get_digest(digest_alg)
        h = hmac.new(to_bytes(key + salt), msg=to_bytes(text), digestmod=digest_alg)
    else:  # compatible with third party systems
        h = hashlib.new(digest_alg)
        h.update(to_bytes(text + salt))
    return h.hexdigest()
github emmett-framework / emmett / emmett / security.py View on Github external
def secure_loads(data, encryption_key, hash_key=None, compression_level=None):
    if ':' not in data:
        return None
    if not hash_key:
        hash_key = hashlib_sha1(encryption_key).hexdigest()
    signature, encrypted_data = data.split(':', 1)
    actual_signature = hmac.new(
        to_bytes(hash_key), msg=to_bytes(encrypted_data), digestmod='md5').hexdigest()
    if signature != actual_signature:
        return None
    key = _pad(to_bytes(encryption_key[:32]))
    aes = pyaes.AESModeOfOperationCFB(key, iv=key[:16], segment_size=8)
    try:
        data = aes.decrypt(base64.urlsafe_b64decode(to_bytes(encrypted_data)))
        data = data.rstrip(to_bytes(' '))
        if compression_level:
            data = zlib.decompress(data)
        return pickle.loads(data)
    except (TypeError, pickle.UnpicklingError):
        return None
github emmett-framework / emmett / emmett / security.py View on Github external
def secure_loads(data, encryption_key, hash_key=None, compression_level=None):
    if ':' not in data:
        return None
    if not hash_key:
        hash_key = hashlib_sha1(encryption_key).hexdigest()
    signature, encrypted_data = data.split(':', 1)
    actual_signature = hmac.new(
        to_bytes(hash_key), msg=to_bytes(encrypted_data), digestmod='md5').hexdigest()
    if signature != actual_signature:
        return None
    key = _pad(to_bytes(encryption_key[:32]))
    aes = pyaes.AESModeOfOperationCFB(key, iv=key[:16], segment_size=8)
    try:
        data = aes.decrypt(base64.urlsafe_b64decode(to_bytes(encrypted_data)))
        data = data.rstrip(to_bytes(' '))
        if compression_level:
            data = zlib.decompress(data)
        return pickle.loads(data)
    except (TypeError, pickle.UnpicklingError):
        return None
github emmett-framework / emmett / emmett / security.py View on Github external
def secure_loads(data, encryption_key, hash_key=None, compression_level=None):
    if ':' not in data:
        return None
    if not hash_key:
        hash_key = hashlib_sha1(encryption_key).hexdigest()
    signature, encrypted_data = data.split(':', 1)
    actual_signature = hmac.new(
        to_bytes(hash_key), msg=to_bytes(encrypted_data), digestmod='md5').hexdigest()
    if signature != actual_signature:
        return None
    key = _pad(to_bytes(encryption_key[:32]))
    aes = pyaes.AESModeOfOperationCFB(key, iv=key[:16], segment_size=8)
    try:
        data = aes.decrypt(base64.urlsafe_b64decode(to_bytes(encrypted_data)))
        data = data.rstrip(to_bytes(' '))
        if compression_level:
            data = zlib.decompress(data)
        return pickle.loads(data)
    except (TypeError, pickle.UnpicklingError):
        return None
github emmett-framework / emmett / emmett / security.py View on Github external
def simple_hash(text, key='', salt='', digest_alg='md5'):
    """
    Generates hash with the given text using the specified
    digest hashing algorithm
    """
    if not digest_alg:
        raise RuntimeError("simple_hash with digest_alg=None")
    elif not isinstance(digest_alg, str):  # manual approach
        h = digest_alg(text + key + salt)
    elif digest_alg.startswith('pbkdf2'):  # latest and coolest!
        iterations, keylen, alg = digest_alg[7:-1].split(',')
        return pbkdf2_hex(
            to_bytes(text), to_bytes(salt), int(iterations), int(keylen),
            get_digest(alg))
    elif key:  # use hmac
        digest_alg = get_digest(digest_alg)
        h = hmac.new(to_bytes(key + salt), msg=to_bytes(text), digestmod=digest_alg)
    else:  # compatible with third party systems
        h = hashlib.new(digest_alg)
        h.update(to_bytes(text + salt))
    return h.hexdigest()
github emmett-framework / emmett / emmett / security.py View on Github external
def secure_dumps(data, encryption_key, hash_key=None, compression_level=None):
    if not hash_key:
        hash_key = hashlib_sha1(encryption_key).hexdigest()
    dump = pickle.dumps(data)
    if compression_level:
        dump = zlib.compress(dump, compression_level)
    key = _pad(to_bytes(encryption_key[:32]))
    aes = pyaes.AESModeOfOperationCFB(key, iv=key[:16], segment_size=8)
    encrypted_data = base64.urlsafe_b64encode(aes.encrypt(_pad(dump)))
    signature = hmac.new(to_bytes(hash_key), msg=encrypted_data, digestmod='md5').hexdigest()
    return signature + ':' + encrypted_data.decode('utf8')
github emmett-framework / emmett / emmett / security.py View on Github external
def _pad(s, n=32, padchar='.'):
    expected_len = ((len(s) + n) - len(s) % n)
    return s.ljust(expected_len, to_bytes(padchar))
    #return s + (32 - len(s) % 32) * padchar