simplecrypter 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
simplecrypter/core.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""Password-based authenticated encryption built on cryptography primitives."""
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
import binascii
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
from cryptography.fernet import Fernet, InvalidToken
|
|
8
|
+
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
|
|
9
|
+
|
|
10
|
+
_PREFIX = "simplecrypter:v1:"
|
|
11
|
+
_SALT_BYTES = 16
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _require_text(value: str, name: str) -> None:
|
|
15
|
+
if not isinstance(value, str):
|
|
16
|
+
raise TypeError(f"{name} must be a string")
|
|
17
|
+
if not value:
|
|
18
|
+
raise ValueError(f"{name} must not be empty")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _key_from_password(password: str, salt: bytes) -> bytes:
|
|
22
|
+
_require_text(password, "password")
|
|
23
|
+
key = Scrypt(salt=salt, length=32, n=2**14, r=8, p=1).derive(password.encode("utf-8"))
|
|
24
|
+
return base64.urlsafe_b64encode(key)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def easy_encrypt(phrase: str, password: str) -> str:
|
|
28
|
+
"""Encrypt a text phrase with a password and return a self-contained string."""
|
|
29
|
+
_require_text(phrase, "phrase")
|
|
30
|
+
salt = os.urandom(_SALT_BYTES)
|
|
31
|
+
encrypted = Fernet(_key_from_password(password, salt)).encrypt(phrase.encode("utf-8"))
|
|
32
|
+
encoded_salt = base64.urlsafe_b64encode(salt).decode("ascii")
|
|
33
|
+
return f"{_PREFIX}{encoded_salt}:{encrypted.decode('ascii')}"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def easy_decrypt(ciphertext: str, password: str) -> str:
|
|
37
|
+
"""Decrypt text returned by :func:`easy_encrypt` using its original password."""
|
|
38
|
+
_require_text(ciphertext, "ciphertext")
|
|
39
|
+
_require_text(password, "password")
|
|
40
|
+
|
|
41
|
+
try:
|
|
42
|
+
if not ciphertext.startswith(_PREFIX):
|
|
43
|
+
raise ValueError
|
|
44
|
+
encoded_salt, encrypted = ciphertext.removeprefix(_PREFIX).split(":", 1)
|
|
45
|
+
if not encrypted:
|
|
46
|
+
raise ValueError
|
|
47
|
+
salt = base64.urlsafe_b64decode(encoded_salt.encode("ascii"))
|
|
48
|
+
if len(salt) != _SALT_BYTES:
|
|
49
|
+
raise ValueError
|
|
50
|
+
return Fernet(_key_from_password(password, salt)).decrypt(encrypted.encode("ascii")).decode("utf-8")
|
|
51
|
+
except (ValueError, UnicodeError, binascii.Error, InvalidToken) as error:
|
|
52
|
+
raise ValueError("ciphertext is invalid or the password is incorrect") from error
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: simplecrypter
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Small, password-based authenticated encryption for Python strings.
|
|
5
|
+
Author: Muck
|
|
6
|
+
License: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
11
|
+
Classifier: Topic :: Security :: Cryptography
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Requires-Dist: cryptography>=42
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# simplecrypter
|
|
17
|
+
|
|
18
|
+
Password-based authenticated encryption for short text values.
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install simplecrypter
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Use
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
import simplecrypter
|
|
30
|
+
|
|
31
|
+
encrypted = simplecrypter.easy_encrypt("phrase", "password")
|
|
32
|
+
original = simplecrypter.easy_decrypt(encrypted, "password")
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Short aliases are available:
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
encrypted = simplecrypter.ez_e("phrase", "password")
|
|
39
|
+
original = simplecrypter.ez_d(encrypted, "password")
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Each encryption uses a new random salt. The encrypted text includes that salt, so only the encrypted value and the password are needed to decrypt it. A wrong password or altered encrypted text raises `ValueError`.
|
|
43
|
+
|
|
44
|
+
## Notes
|
|
45
|
+
|
|
46
|
+
- Use a long, unique password and store it in a password manager or secret manager.
|
|
47
|
+
- This package encrypts text, not files or database records.
|
|
48
|
+
- Losing the password means the data cannot be recovered.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
simplecrypter/__init__.py,sha256=1N8XoWQuzGNSerAAGw9Fr9LbNwpU9xiEy9to-QsJXvE,220
|
|
2
|
+
simplecrypter/core.py,sha256=XMPXNi2tVReCgSKMt31HBK4FDsAVANxo20YSYMnRYV8,2026
|
|
3
|
+
simplecrypter-0.1.0.dist-info/METADATA,sha256=Y3zWHceDhcVBvHkFJVRo4cwQyp9hazETRvF0Na7tfUc,1356
|
|
4
|
+
simplecrypter-0.1.0.dist-info/WHEEL,sha256=THafob7ofN-NsuMN7Mg4qZyHaQI7KkD-QlcQatYhXPo,87
|
|
5
|
+
simplecrypter-0.1.0.dist-info/licenses/LICENSE,sha256=F_4KeGprt40t5Of62-s2dKrsWvMlLvg-aupLPfFqrtA,1061
|
|
6
|
+
simplecrypter-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Muck
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|