File size: 772 Bytes
2abfccb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding: utf-8 -*-
import hashlib

from petrel_client.common.exception import ConfigKeyValueError

_SUPPORTED_TYPES = ('blake2b', 'blake2s', 'md5', 'pbkdf2_hmac', 'sha1', 'sha224', 'sha256',
                    'sha384', 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512', 'sha512', 'shake_128', 'shake_256')


def get_hash_fn(hash_type):
    if hash_type in _SUPPORTED_TYPES:
        return getattr(hashlib, hash_type)
    else:
        raise ConfigKeyValueError(f"'{hash_type}' is not a valid hash type.")


def to_bytes(key):
    if isinstance(key, str):
        key = key.encode('utf-8')
    else:
        assert isinstance(key, bytes)
    return key


def hexdigest(key, hash_fn):
    key = to_bytes(key)
    m = hash_fn()
    m.update(key)
    return m.hexdigest()