Exemplo n.º 1
0
def test_determine_pbkdf2_rounds():
    method = "hmac-sha1"
    for arguments in [
        (1.0, 1, 1, method, 1),
        (1, 1, 1.0, method, 1),
        ]:
        with pytest.raises(TypeError):
            determine_pbkdf2_rounds(*arguments)
Exemplo n.º 2
0
def compile(application_config):
    """
    Compiles `application_config` and returns a deployment config, raises
    :exc:`ValueError` if `application_config` is outdated or has been created
    with a more recent incompatible version of pwhash.
    """
    # If you change `config` and nobody documented incrementing
    # DEPLOYMENT_VERSION in CHANGELOG.rst, increment DEPLOYMENT_VERSION and
    # document that in CHANGELOG.rst.
    if application_config["application_version"] != APPLICATION_VERSION:
        raise ValueError("application_config outdated or pwhash outdated")
    pbkdf2_method = "hmac-sha1"
    config = {
        "version": DEPLOYMENT_VERSION,
        "application": application_config,
        "hashers": {
            "pbkdf2": {
                "rounds": determine_pbkdf2_rounds(
                    application_config["min_password_length"],
                    hashers.DEFAULT_SALT_LENGTH,
                    hashers.DIGEST_SIZES[pbkdf2_method],
                    pbkdf2_method,
                    application_config["duration"],
                ),
                "method": pbkdf2_method,
                "salt_length": hashers.DEFAULT_SALT_LENGTH,
            }
        },
    }
    try:
        config["hashers"]["bcrypt"] = {
            "cost": determine_bcrypt_cost(application_config["min_password_length"], application_config["duration"])
        }
    except RuntimeError:
        pass
    for name, hasher in hashers.ALL_HASHERS.items():
        if name.startswith("salted") or name.startswith("hmac"):
            config["hashers"][name] = {"salt_length": hashers.DEFAULT_SALT_LENGTH}
    return config