U
    æ¢-dÆ  ã                   @   s*   d dl Z dZdZdd„ Zdefdd„ZdS )	é    NZ0123456789abcdefz@0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_c                 C   sL   | dkr|d S t |ƒ}d}| r>||t| | ƒ 7 }| | } q|ddd… S )aj  Encode integer into string, using digit encoding table.

    You can encode integer into hexadecimal string:

    >>> int_to_enc(20190925, enc_table_16)
    '13416cd'

    To verify use, python's hex() function:

    >>> hex(20190925)
    '0x13416cd'

    You can encode integer using 64 digit table:

    >>> int_to_enc(20190925, enc_table_64)
    '1d1rd'

    r   Ú Néÿÿÿÿ)ÚlenÚint)ÚnÚ	enc_tableÚbaseÚdigits© r
   ú./util/cmf_hashlib.pyÚ
int_to_enc   s    
r   é   c                 C   sH   |dkrt d |¡ƒ‚t |  ¡ ¡}| ¡ }tt|dƒ|ƒ}|d|… S )a”  Geneate string hash with given length, using specified encoding table.

    Example 1:

    Generating hash with length of 8 characters, using hexadecimal encoding table:

    >>> short_str_enc("hello world", 8, enc_table_16)
    '309ecc48'

    Generating hash with length of 8 charactes, using extended 64-digit encoding table:

    >>> short_str_enc("hello world", 8, enc_table_64)
    'MDIN8D1b'
    é€   zchar_length {} exceeds 128é   r   )Ú
ValueErrorÚformatÚhashlibZsha512ÚencodeZ	hexdigestr   r   )ÚsZchar_lengthr   Zhash_objectZhash_hexZhash_encr
   r
   r   Úshort_str_enc&   s    r   )r   Zenc_table_16Zenc_table_64r   r   r
   r
   r
   r   Ú<module>   s   