<commit_before>import contextlib
import os
import re

from argon2.low_level import (
    ARGON2_VERSION,
    Type,
    core,
    ffi,
    lib,
)

from .utils import check_types, ensure_bytes, b64_decode_raw, b64_encode_raw
from .exceptions import PorridgeError, MissingKeyError, ParameterError

# TODO: Compute these dynamically for the target environment. Prefer magic to people having to configure cryptographic parameters.
DEFAULT_RANDOM_SALT_LENGTH = 16
DEFAULT_HASH_LENGTH = 32
DEFAULT_TIME_COST = 2
DEFAULT_MEMORY_COST = 512
DEFAULT_PARALLELISM = 2
DEFAULT_FLAGS = lib.ARGON2_FLAG_CLEAR_PASSWORD | lib.ARGON2_FLAG_CLEAR_SECRET

# This regex validates the spec from
# https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md
ENCODED_HASH_RE = re.compile(r''.join([
    # TODO: Validate max lengths of fields
        r'^\$argon2i\$',
        r'(?:v=(?P<version>[0-9]+)\$)?',
        r''.join([
            r'm=(?P<memory_cost>[0-9]{1,10})',
            r',t=(?P<time_cost>[0-9]{1,10})',
            r',p=(?P<parallelism>[0-9]{1,3})',
            r'(?:,keyid=(?P<keyid>[a-zA-Z0-9+/]+))?', # optional
            r'(?:,data=(?P<data>[a-zA-Z0-9+/]+))?', # optional, unused
        ]),
        r'\$(?P<salt>[a-zA-Z0-9+/]+)\$',
        r'(?P<hash>[a-zA-Z0-9+/]+)',
    ]) + r'$'
)


class Porridge(object):<commit_msg>Skip magic for regularly updating lib<commit_after>import contextlib
import os
import re

from argon2.low_level import (
    ARGON2_VERSION,
    Type,
    core,
    ffi,
    lib,
)

from .utils import check_types, ensure_bytes, b64_decode_raw, b64_encode_raw
from .exceptions import PorridgeError, MissingKeyError, ParameterError

# These parameters should be increased regularly to keep boiling slow
# on new hardware
DEFAULT_RANDOM_SALT_LENGTH = 16
DEFAULT_HASH_LENGTH = 32
DEFAULT_TIME_COST = 2
DEFAULT_MEMORY_COST = 512
DEFAULT_PARALLELISM = 4
DEFAULT_FLAGS = lib.ARGON2_FLAG_CLEAR_PASSWORD | lib.ARGON2_FLAG_CLEAR_SECRET

# This regex validates the spec from
# https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md
ENCODED_HASH_RE = re.compile(r''.join([
    # TODO: Validate max lengths of fields
        r'^\$argon2i\$',
        r'(?:v=(?P<version>[0-9]+)\$)?',
        r''.join([
            r'm=(?P<memory_cost>[0-9]{1,10})',
            r',t=(?P<time_cost>[0-9]{1,10})',
            r',p=(?P<parallelism>[0-9]{1,3})',
            r'(?:,keyid=(?P<keyid>[a-zA-Z0-9+/]+))?', # optional
            r'(?:,data=(?P<data>[a-zA-Z0-9+/]+))?', # optional, unused
        ]),
        r'\$(?P<salt>[a-zA-Z0-9+/]+)\$',
        r'(?P<hash>[a-zA-Z0-9+/]+)',
    ]) + r'$'
)


class Porridge(object):
{'fraction_non_alphanumeric': 0.17872018174933738, 'fraction_numerical': 0.03180613404013631, 'mean_word_length': 3.4106844741235394, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 2, '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': '5176214', 'n_tokens_mistral': 1088, 'n_tokens_neox': 1010, 'n_words': 176}