Source code for mimesis.providers.cryptographic

# -*- coding: utf-8 -*-

"""Cryptographic data provider."""

import hashlib
import secrets
from typing import Any, Final, Optional
from uuid import UUID, uuid4

from mimesis.data.int.cryptographic import WORDLIST
from mimesis.enums import Algorithm
from mimesis.providers.base import BaseProvider

__all__ = ["Cryptographic"]


[docs]class Cryptographic(BaseProvider): """Class that provides cryptographic data."""
[docs] def __init__(self, *args: Any, **kwargs: Any) -> None: """Initialize attributes. :param seed: Seed. """ super().__init__(*args, **kwargs)
[docs] class Meta: """Class for metadata.""" name: Final[str] = "cryptographic"
[docs] @staticmethod def uuid_object() -> UUID: """Generate UUID4 object. :return: UUID4 object. """ return uuid4()
[docs] def uuid(self) -> str: """Generate UUID4 string. :return: UUID4 as string. """ return str(self.uuid_object())
[docs] def hash(self, algorithm: Optional[Algorithm] = None) -> str: # noqa: A003 """Generate random hash. To change hashing algorithm, pass parameter ``algorithm`` with needed value of the enum object :class:`~mimesis.enums.Algorithm` .. warning:: Seed is not applicable to this method, because of its cryptographic-safe nature. :param algorithm: Enum object :class:`~mimesis.enums.Algorithm`. :return: Hash. :raises NonEnumerableError: When algorithm is unsupported. """ key = self.validate_enum(algorithm, Algorithm) if hasattr(hashlib, key): fn = getattr(hashlib, key) return fn(self.uuid().encode()).hexdigest() # type: ignore
[docs] @staticmethod def token_bytes(entropy: int = 32) -> bytes: """Generate byte string containing ``entropy`` bytes. The string has ``entropy`` random bytes, each byte converted to two hex digits. .. warning:: Seed is not applicable to this method, because of its cryptographic-safe nature. :param entropy: Number of bytes (default: 32). :return: Random bytes. """ return secrets.token_bytes(entropy)
[docs] @staticmethod def token_hex(entropy: int = 32) -> str: """Return a random text string, in hexadecimal. The string has *entropy* random bytes, each byte converted to two hex digits. If *entropy* is ``None`` or not supplied, a reasonable default is used. .. warning:: Seed is not applicable to this method, because of its cryptographic-safe nature. :param entropy: Number of bytes (default: 32). :return: Token. """ return secrets.token_hex(entropy)
[docs] @staticmethod def token_urlsafe(entropy: int = 32) -> str: """Return a random URL-safe text string, in Base64 encoding. The string has *entropy* random bytes. If *entropy* is ``None`` or not supplied, a reasonable default is used. .. warning:: Seed is not applicable to this method, because of its cryptographic-safe nature. :param entropy: Number of bytes (default: 32). :return: URL-safe token. """ return secrets.token_urlsafe(entropy)
[docs] def mnemonic_phrase(self) -> str: """Generate BIP-39-compatible mnemonic phrase. :return: Mnemonic phrase. """ length = self.random.choice([12, 24]) phrases = self.random.choices(WORDLIST, k=length) return " ".join(phrases)