| 1 |
|
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import platform |
| 6 |
import sys |
| 7 |
|
| 8 |
from dataclasses import dataclass |
| 9 |
from typing import Any |
| 10 |
|
| 11 |
from .exceptions import InvalidHashError, UnsupportedParametersError |
| 12 |
from .low_level import Type |
| 13 |
|
| 14 |
|
| 15 |
NoneType = type(None) |
| 16 |
|
| 17 |
|
| 18 |
def _check_types(**kw: Any) -> str | None: |
| 19 |
""" |
| 20 |
Check each ``name: (value, types)`` in *kw*. |
| 21 |
|
| 22 |
Returns a human-readable string of all violations or `None``. |
| 23 |
""" |
| 24 |
errors = [] |
| 25 |
for name, (value, types) in kw.items(): |
| 26 |
if not isinstance(value, types): |
| 27 |
if isinstance(types, tuple): |
| 28 |
types = ", or ".join(t.__name__ for t in types) |
| 29 |
else: |
| 30 |
types = types.__name__ |
| 31 |
errors.append( |
| 32 |
f"'{name}' must be a {types} (got {type(value).__name__})" |
| 33 |
) |
| 34 |
|
| 35 |
if errors != []: |
| 36 |
return ", ".join(errors) + "." |
| 37 |
|
| 38 |
return None |
| 39 |
|
| 40 |
|
| 41 |
def _is_wasm() -> bool: |
| 42 |
return sys.platform == "emscripten" or platform.machine() in [ |
| 43 |
"wasm32", |
| 44 |
"wasm64", |
| 45 |
] |
| 46 |
|
| 47 |
|
| 48 |
def _decoded_str_len(length: int) -> int: |
| 49 |
""" |
| 50 |
Compute how long an encoded string of length *l* becomes. |
| 51 |
""" |
| 52 |
rem = length % 4 |
| 53 |
|
| 54 |
if rem == 3: |
| 55 |
last_group_len = 2 |
| 56 |
elif rem == 2: |
| 57 |
last_group_len = 1 |
| 58 |
else: |
| 59 |
last_group_len = 0 |
| 60 |
|
| 61 |
return length // 4 * 3 + last_group_len |
| 62 |
|
| 63 |
|
| 64 |
@dataclass |
| 65 |
class Parameters: |
| 66 |
""" |
| 67 |
Argon2 hash parameters. |
| 68 |
|
| 69 |
See :doc:`parameters` on how to pick them. |
| 70 |
|
| 71 |
Attributes: |
| 72 |
type: Hash type. |
| 73 |
|
| 74 |
version: Argon2 version. |
| 75 |
|
| 76 |
salt_len: Length of the salt in bytes. |
| 77 |
|
| 78 |
hash_len: Length of the hash in bytes. |
| 79 |
|
| 80 |
time_cost: Time cost in iterations. |
| 81 |
|
| 82 |
memory_cost: Memory cost in kibibytes. |
| 83 |
|
| 84 |
parallelism: Number of parallel threads. |
| 85 |
|
| 86 |
.. versionadded:: 18.2.0 |
| 87 |
""" |
| 88 |
|
| 89 |
type: Type |
| 90 |
version: int |
| 91 |
salt_len: int |
| 92 |
hash_len: int |
| 93 |
time_cost: int |
| 94 |
memory_cost: int |
| 95 |
parallelism: int |
| 96 |
|
| 97 |
__slots__ = ( |
| 98 |
"hash_len", |
| 99 |
"memory_cost", |
| 100 |
"parallelism", |
| 101 |
"salt_len", |
| 102 |
"time_cost", |
| 103 |
"type", |
| 104 |
"version", |
| 105 |
) |
| 106 |
|
| 107 |
|
| 108 |
_NAME_TO_TYPE = {"argon2id": Type.ID, "argon2i": Type.I, "argon2d": Type.D} |
| 109 |
_REQUIRED_KEYS = sorted(("v", "m", "t", "p")) |
| 110 |
|
| 111 |
|
| 112 |
def extract_parameters(hash: str) -> Parameters: |
| 113 |
""" |
| 114 |
Extract parameters from an encoded *hash*. |
| 115 |
|
| 116 |
Args: |
| 117 |
hash: An encoded Argon2 hash string. |
| 118 |
|
| 119 |
Returns: |
| 120 |
The parameters used to create the hash. |
| 121 |
|
| 122 |
.. versionadded:: 18.2.0 |
| 123 |
""" |
| 124 |
parts = hash.split("$") |
| 125 |
|
| 126 |
|
| 127 |
if len(parts) == 5: |
| 128 |
parts.insert(2, "v=18") |
| 129 |
|
| 130 |
if len(parts) != 6: |
| 131 |
raise InvalidHashError |
| 132 |
|
| 133 |
if parts[0]: |
| 134 |
raise InvalidHashError |
| 135 |
|
| 136 |
try: |
| 137 |
type = _NAME_TO_TYPE[parts[1]] |
| 138 |
|
| 139 |
kvs = { |
| 140 |
k: int(v) |
| 141 |
for k, v in ( |
| 142 |
s.split("=") for s in [parts[2], *parts[3].split(",")] |
| 143 |
) |
| 144 |
} |
| 145 |
except Exception: |
| 146 |
raise InvalidHashError from None |
| 147 |
|
| 148 |
if sorted(kvs.keys()) != _REQUIRED_KEYS: |
| 149 |
raise InvalidHashError |
| 150 |
|
| 151 |
return Parameters( |
| 152 |
type=type, |
| 153 |
salt_len=_decoded_str_len(len(parts[4])), |
| 154 |
hash_len=_decoded_str_len(len(parts[5])), |
| 155 |
version=kvs["v"], |
| 156 |
time_cost=kvs["t"], |
| 157 |
memory_cost=kvs["m"], |
| 158 |
parallelism=kvs["p"], |
| 159 |
) |
| 160 |
|
| 161 |
|
| 162 |
def validate_params_for_platform(params: Parameters) -> None: |
| 163 |
""" |
| 164 |
Validate *params* against current platform. |
| 165 |
|
| 166 |
Args: |
| 167 |
params: Parameters to be validated |
| 168 |
|
| 169 |
Returns: |
| 170 |
None |
| 171 |
""" |
| 172 |
if _is_wasm() and params.parallelism != 1: |
| 173 |
msg = "In WebAssembly environments `parallelism` must be 1." |
| 174 |
raise UnsupportedParametersError(msg) |