Skip to content

SecretStr

SecretStr

Bases: Secret[S], Sequence

A specialized subclass of Secret[StringLike] for holding strings or bytes.

This class provides for more efficient conversion between strings and bytes, which is often necessary when using external cryptographic libraries.

Source code in secret_type/containers/sequence.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class SecretStr(Secret[S], Sequence):
    """A specialized subclass of [`Secret[StringLike]`][secret_type.Secret] for holding strings or bytes.

    This class provides for more efficient conversion between strings and bytes,
    which is often necessary when using external cryptographic libraries.
    """

    def cast(self, t: Type[T], *args, **kwargs) -> "Secret[T]":
        """Casts a string to bytes, or vice-versa.

        Args:
            t (Union[str, bytes]): The type to cast to.

        Examples: Example:
            For example, to cast a string to bytes so it can be used with [`hashlib`][hashlib]:

            ```python
            secret = Secret.wrap("foobar").cast(bytes)
            hashed = secret.dangerous_map(lambda x: hashlib.sha256(x).hexdigest())
            ```
        """
        if self.protected_type is t:
            val = self
        elif t is bytes:
            # str -> bytes
            val = self.dangerous_map(lambda x: cast(str, x).encode())
        elif t is str:
            # bytes -> str
            val = self.dangerous_map(lambda x: cast(bytes, x).decode())
        else:
            return super().cast(t, *args, **kwargs)

        return cast(Secret[T], val)

    def __len__(self):
        return secrets.randbelow(10_000)

    def __getitem__(self, index):
        return self.dangerous_map(lambda x: x[index])

    def __reverse__(self):
        return self.dangerous_map(lambda x: x[::-1])

    def __contains__(self, item):
        return self.dangerous_map(lambda x: item in x)

    def __iter__(self):
        return (Secret(x) for x in self._dangerous_extract())

cast(t: Type[T], *args, **kwargs) -> Secret[T]

Casts a string to bytes, or vice-versa.

PARAMETER DESCRIPTION
t

The type to cast to.

TYPE: Union[str, bytes]

Example:

For example, to cast a string to bytes so it can be used with hashlib:

secret = Secret.wrap("foobar").cast(bytes)
hashed = secret.dangerous_map(lambda x: hashlib.sha256(x).hexdigest())
Source code in secret_type/containers/sequence.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def cast(self, t: Type[T], *args, **kwargs) -> "Secret[T]":
    """Casts a string to bytes, or vice-versa.

    Args:
        t (Union[str, bytes]): The type to cast to.

    Examples: Example:
        For example, to cast a string to bytes so it can be used with [`hashlib`][hashlib]:

        ```python
        secret = Secret.wrap("foobar").cast(bytes)
        hashed = secret.dangerous_map(lambda x: hashlib.sha256(x).hexdigest())
        ```
    """
    if self.protected_type is t:
        val = self
    elif t is bytes:
        # str -> bytes
        val = self.dangerous_map(lambda x: cast(str, x).encode())
    elif t is str:
        # bytes -> str
        val = self.dangerous_map(lambda x: cast(bytes, x).decode())
    else:
        return super().cast(t, *args, **kwargs)

    return cast(Secret[T], val)