<commit_before># encoding: utf-8
'''
A collection of hashing and encoding functions
'''
import base64
import hashlib
import hmac


def base64_encode(infile, outfile):
    '''
    Encode a file as base64 and write the result to a new file

    .. versionadded:: Helium

    CLI Example:

    .. code-block::

        salt '*' hashutil.base64_encode /path/to/file1 /path/to/encoded_file1
    '''
    return base64.encode(infile, outfile)


def base64_decode(infile, outfile):
    '''
    Decode a base64-encoded file and write the result to a new file

    .. versionadded:: Helium

    CLI Example:

    .. code-block:: bash

        salt '*' hashutil.base64_decode /path/to/encoded_file1 /path/to/file1
    '''
    return base64.decode(infile, outfile)


def base64_encodestring(instr):
    '''
    Encode a string as base64

    .. versionadded:: Helium

    CLI Example:

    .. code-block:: bash

        salt '*' hashutil.base64_encodestring 'get salted'
    '''
    return base64.encodestring(instr)


def base64_decodestring(instr):
    '''
    Decode a base64-encoded string

    .. versionadded:: Helium

    CLI Example:

    .. code-block:: bash

        salt '*' hashutil.base64_decodestring 'Z2V0IHNhbHRlZA==\n'
    '''
    return base64.decodestring(instr)


def md5_digest(instr):
    '''
    Generate an md5 hash of a given string

    .. versionadded:: Helium

    CLI Example:

    .. code-block:: bash

        salt '*' hashutil.md5_digest 'get salted'
    '''
    return hashlib.md5(instr).hexdigest()


def sha256_digest(instr):
    '''
    Generate an sha256 hash of a given string

    .. versionadded:: Helium

    CLI Example:

    .. code-block:: bash

        salt '*' hashutil.sha256_digest 'get salted'
    '''
    return hashlib.sha256(instr).hexdigest()


def sha512_digest(instr):
    '''
    Generate an sha512 hash of a given string

    .. versionadded:: Helium

    CLI Example:

    .. code-block:: bash

        salt '*' hashutil.sha512_digest 'get salted'
    '''
    return hashlib.sha512(instr).hexdigest()


def hmac_signature(string, shared_secret, challenge_hmac):
    '''
    Verify a challenging hmac signature against a string / shared-secret

    .. versionadded:: Helium

    Returns a boolean if the verification succeeded or failed.

    CLI Example:

    .. code-block:: bash

        salt '*' hashutil.hmac_signature 'get salted' 'shared secret' 'NS2BvKxFRk+rndAlFbCYIFNVkPtI/3KiIYQw4okNKU8='
    '''
    hmac_hash = hmac.new(string, shared_secret, hashlib.sha256)
    valid_hmac = base64.b64encode(hmac_hash.digest())
    return valid_hmac == challenge_hmac
<commit_msg>Removed hashutils functions without tests<commit_after># encoding: utf-8
'''
A collection of hashing and encoding functions
'''
import base64
import hashlib
import hmac


def base64_encodestring(instr):
    '''
    Encode a string as base64

    .. versionadded:: Helium

    CLI Example:

    .. code-block:: bash

        salt '*' hashutil.base64_encodestring 'get salted'
    '''
    return base64.encodestring(instr)


def base64_decodestring(instr):
    '''
    Decode a base64-encoded string

    .. versionadded:: Helium

    CLI Example:

    .. code-block:: bash

        salt '*' hashutil.base64_decodestring 'Z2V0IHNhbHRlZA==\n'
    '''
    return base64.decodestring(instr)


def md5_digest(instr):
    '''
    Generate an md5 hash of a given string

    .. versionadded:: Helium

    CLI Example:

    .. code-block:: bash

        salt '*' hashutil.md5_digest 'get salted'
    '''
    return hashlib.md5(instr).hexdigest()


def sha256_digest(instr):
    '''
    Generate an sha256 hash of a given string

    .. versionadded:: Helium

    CLI Example:

    .. code-block:: bash

        salt '*' hashutil.sha256_digest 'get salted'
    '''
    return hashlib.sha256(instr).hexdigest()


def sha512_digest(instr):
    '''
    Generate an sha512 hash of a given string

    .. versionadded:: Helium

    CLI Example:

    .. code-block:: bash

        salt '*' hashutil.sha512_digest 'get salted'
    '''
    return hashlib.sha512(instr).hexdigest()


def hmac_signature(string, shared_secret, challenge_hmac):
    '''
    Verify a challenging hmac signature against a string / shared-secret

    .. versionadded:: Helium

    Returns a boolean if the verification succeeded or failed.

    CLI Example:

    .. code-block:: bash

        salt '*' hashutil.hmac_signature 'get salted' 'shared secret' '<KEY>
    '''
    hmac_hash = hmac.new(string, shared_secret, hashlib.sha256)
    valid_hmac = base64.b64encode(hmac_hash.digest())
    return valid_hmac == challenge_hmac

{'fraction_non_alphanumeric': 0.10911062906724511, 'fraction_numerical': 0.02950108459869848, 'mean_word_length': 2.788824979457683, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2939528', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1459, 'n_words': 384}