pydantic_encryption 0.0.2a0__tar.gz → 0.0.2a2__tar.gz

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.
@@ -0,0 +1,253 @@
1
+ Metadata-Version: 2.3
2
+ Name: pydantic_encryption
3
+ Version: 0.0.2a2
4
+ Summary: Encryption and hashing models for Pydantic
5
+ Author: Julien Kmec
6
+ Author-email: me@julien.dev
7
+ Requires-Python: >=3.9,<4.0.0
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.9
10
+ Classifier: Programming Language :: Python :: 3.10
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Provides-Extra: all
15
+ Provides-Extra: evervault
16
+ Provides-Extra: generics
17
+ Provides-Extra: test
18
+ Requires-Dist: bcrypt (>=4.3.0,<5.0.0)
19
+ Requires-Dist: coverage (>=7.8.0) ; extra == "all"
20
+ Requires-Dist: coverage (>=7.8.0) ; extra == "test"
21
+ Requires-Dist: evervault (>=4.4.1) ; extra == "all"
22
+ Requires-Dist: evervault (>=4.4.1) ; extra == "evervault"
23
+ Requires-Dist: pydantic (>=2.10.6)
24
+ Requires-Dist: pydantic-settings (>=2.9.1)
25
+ Requires-Dist: pytest (>=8.3.5) ; extra == "all"
26
+ Requires-Dist: pytest (>=8.3.5) ; extra == "test"
27
+ Requires-Dist: python-generics (>=0.2.3) ; extra == "all"
28
+ Requires-Dist: python-generics (>=0.2.3) ; extra == "generics"
29
+ Description-Content-Type: text/markdown
30
+
31
+ # Encryption and hashing models for Pydantic
32
+
33
+ This package provides Pydantic field annotations that encrypt, decrypt, and hash field values.
34
+
35
+ ## Installation
36
+
37
+ Install with Pip:
38
+ ```bash
39
+ pip install pydantic_encryption[evervault,generics]
40
+ ```
41
+
42
+ ## Features
43
+
44
+ - Encrypt and decrypt specific fields
45
+ - Hash specific fields
46
+ - Support for generics
47
+
48
+ ## Example
49
+
50
+ ```python
51
+ from pydantic_encryption import BaseModel, Encrypt, Hash, Annotated
52
+
53
+ class User(BaseModel):
54
+ name: str
55
+ address: Annotated[str, Encrypt] # This field will be encrypted
56
+ password: Annotated[str, Hash] # This field will be hashed
57
+
58
+ user = User(name="John Doe", address="123456", password="secret123")
59
+
60
+ print(user.name) # plaintext (untouched)
61
+ print(user.address) # encrypted
62
+ print(user.password) # hashed
63
+ ```
64
+
65
+ ## Choose an Encryption Method
66
+
67
+ ### Evervault
68
+
69
+ If you install this package with the `evervault` extra, you can use [Evervault](https://evervault.com/) to encrypt and decrypt fields.
70
+
71
+ You need to set the following environment variables or add them to your `.env` file:
72
+
73
+ ```bash
74
+ EVERVAULT_APP_ID=your_app_id
75
+ EVERVAULT_API_KEY=your_api_key
76
+ EVERVAULT_ENCRYPTION_ROLE=your_encryption_role
77
+ ```
78
+
79
+ ### Custom Encryption
80
+
81
+ You can define your own encryption and decryption methods by subclassing `SecureModel`. `SecureModel` provides you with the utilities to handle encryption, decryption, and hashing.
82
+
83
+ `self.pending_encryption_fields`, `self.pending_decryption_fields`, and `self.pending_hash_fields` are dictionaries of field names to field values that need to be encrypted, decrypted, or hashed, i.e., fields annotated with `Encrypt`, `Decrypt`, or `Hash`.
84
+
85
+ You can override the `encrypt_data`, `decrypt_data`, and `hash_data` methods to implement your own encryption, decryption, and hashing logic. You then need to override `model_post_init` to call these methods.
86
+
87
+ First, define a custom secure model:
88
+
89
+ ```python
90
+ from typing import Any, override
91
+ from pydantic import BaseModel as PydanticBaseModel
92
+ from pydantic_encryption import SecureModel
93
+
94
+ class MySecureModel(PydanticBaseModel, SecureModel):
95
+ @override
96
+ def encrypt_data(self) -> None:
97
+ # Your encryption logic here
98
+ pass
99
+
100
+ @override
101
+ def decrypt_data(self) -> None:
102
+ # Your decryption logic here
103
+ pass
104
+
105
+ @override
106
+ def hash_data(self) -> None:
107
+ # Your hashing logic here
108
+ pass
109
+
110
+ @override
111
+ def model_post_init(self, context: Any, /) -> None:
112
+ if not self._disable:
113
+ if self.pending_decryption_fields:
114
+ self.decrypt_data()
115
+
116
+ if self.pending_encryption_fields:
117
+ self.encrypt_data()
118
+
119
+ if self.pending_hash_fields:
120
+ self.hash_data()
121
+
122
+ super().model_post_init(context)
123
+ ```
124
+
125
+ Then use it:
126
+
127
+ ```python
128
+ from typing import Annotated
129
+ from pydantic import BaseModel # Here, we don't use the BaseModel provided by the library, but the native one from Pydantic
130
+ from pydantic_encryption import Encrypt
131
+
132
+ class MyModel(BaseModel, MySecureModel):
133
+ username: str
134
+ address: Annotated[str, Encrypt]
135
+
136
+ model = MyModel(username="john_doe", address="123456")
137
+ print(model.address) # encrypted
138
+ ```
139
+
140
+ ## Encryption
141
+
142
+ You can encrypt any field by using the `Encrypt` annotation with `Annotated` and inheriting from `BaseModel`.
143
+
144
+ ```python
145
+ from typing import Annotated
146
+ from pydantic_encryption import Encrypt, BaseModel
147
+
148
+ class User(BaseModel):
149
+ name: str
150
+ address: Annotated[str, Encrypt] # This field will be encrypted
151
+
152
+ user = User(name="John Doe", address="123456")
153
+ print(user.address) # encrypted
154
+ print(user.name) # plaintext (untouched)
155
+ ```
156
+
157
+ The fields marked with `Encrypt` are automatically encrypted during model initialization.
158
+
159
+ ## Decryption
160
+
161
+ Similar to encryption, you can decrypt any field by using the `Decrypt` annotation with `Annotated` and inheriting from `BaseModel`.
162
+
163
+ ```python
164
+ from typing import Annotated
165
+ from pydantic_encryption import Decrypt, BaseModel
166
+
167
+ class UserResponse(BaseModel):
168
+ name: str
169
+ address: Annotated[str, Decrypt] # This field will be decrypted
170
+
171
+ user = UserResponse(**user_data) # encrypted value
172
+ print(user.address) # decrypted
173
+ print(user.name) # plaintext (untouched)
174
+ ```
175
+
176
+ Fields marked with `Decrypt` are automatically decrypted during model initialization.
177
+
178
+
179
+ ## Hashing
180
+
181
+ You can hash sensitive data like passwords by using the `Hash` annotation.
182
+
183
+ ```python
184
+ from typing import Annotated
185
+ from pydantic_encryption import Hash, BaseModel
186
+
187
+ class User(BaseModel):
188
+ username: str
189
+ password: Annotated[str, Hash] # This field will be hashed
190
+
191
+ user = User(username="john_doe", password="secret123")
192
+ print(user.password) # hashed value
193
+ ```
194
+
195
+ Fields marked with `Hash` are automatically hashed using bcrypt during model initialization.
196
+
197
+ ## Disable Auto Processing
198
+
199
+ You can disable automatic encryption/decryption/hashing by setting `disable` to `True` in the class definition.
200
+
201
+ ```python
202
+ from typing import Annotated
203
+ from pydantic_encryption import Encrypt, BaseModel
204
+
205
+ class UserResponse(BaseModel, disable=True):
206
+ name: str
207
+ address: Annotated[str, Encrypt]
208
+
209
+ # To encrypt/decrypt/hash, call the respective methods manually:
210
+ user = UserResponse(name="John Doe", address="123 Main St")
211
+
212
+ # Manual encryption
213
+ user.encrypt_data()
214
+ print(user.address) # encrypted
215
+
216
+ # Or user.decrypt_data() to decrypt and user.hash_data() to hash
217
+ ```
218
+
219
+ ## Generics
220
+
221
+ Each BaseModel has an additional helpful method that will tell you its generic type.
222
+
223
+ To use generics, you must install this package with the `generics` extra: `pip install pydantic_encryption[generics]`.
224
+
225
+ ```py
226
+ from pydantic_encryption import BaseModel
227
+
228
+ class MyModel[T](BaseModel):
229
+ value: T
230
+
231
+ model = MyModel[str](value="Hello")
232
+ print(model.get_type()) # <class 'str'>
233
+ ```
234
+
235
+ ## Run Tests
236
+
237
+ Install [Poetry](https://python-poetry.org/docs/) and run:
238
+
239
+ ```bash
240
+ poetry install --with test
241
+ poetry run coverage run -m pytest -v -s
242
+ ```
243
+
244
+ ## Roadmap
245
+
246
+ This is an early development version. I am considering the following features:
247
+
248
+ - [ ] Add optional support for other encryption providers beyond Evervault
249
+
250
+ ## Feature Requests
251
+
252
+ If you have any feature requests, please open an issue.
253
+
@@ -0,0 +1,222 @@
1
+ # Encryption and hashing models for Pydantic
2
+
3
+ This package provides Pydantic field annotations that encrypt, decrypt, and hash field values.
4
+
5
+ ## Installation
6
+
7
+ Install with Pip:
8
+ ```bash
9
+ pip install pydantic_encryption[evervault,generics]
10
+ ```
11
+
12
+ ## Features
13
+
14
+ - Encrypt and decrypt specific fields
15
+ - Hash specific fields
16
+ - Support for generics
17
+
18
+ ## Example
19
+
20
+ ```python
21
+ from pydantic_encryption import BaseModel, Encrypt, Hash, Annotated
22
+
23
+ class User(BaseModel):
24
+ name: str
25
+ address: Annotated[str, Encrypt] # This field will be encrypted
26
+ password: Annotated[str, Hash] # This field will be hashed
27
+
28
+ user = User(name="John Doe", address="123456", password="secret123")
29
+
30
+ print(user.name) # plaintext (untouched)
31
+ print(user.address) # encrypted
32
+ print(user.password) # hashed
33
+ ```
34
+
35
+ ## Choose an Encryption Method
36
+
37
+ ### Evervault
38
+
39
+ If you install this package with the `evervault` extra, you can use [Evervault](https://evervault.com/) to encrypt and decrypt fields.
40
+
41
+ You need to set the following environment variables or add them to your `.env` file:
42
+
43
+ ```bash
44
+ EVERVAULT_APP_ID=your_app_id
45
+ EVERVAULT_API_KEY=your_api_key
46
+ EVERVAULT_ENCRYPTION_ROLE=your_encryption_role
47
+ ```
48
+
49
+ ### Custom Encryption
50
+
51
+ You can define your own encryption and decryption methods by subclassing `SecureModel`. `SecureModel` provides you with the utilities to handle encryption, decryption, and hashing.
52
+
53
+ `self.pending_encryption_fields`, `self.pending_decryption_fields`, and `self.pending_hash_fields` are dictionaries of field names to field values that need to be encrypted, decrypted, or hashed, i.e., fields annotated with `Encrypt`, `Decrypt`, or `Hash`.
54
+
55
+ You can override the `encrypt_data`, `decrypt_data`, and `hash_data` methods to implement your own encryption, decryption, and hashing logic. You then need to override `model_post_init` to call these methods.
56
+
57
+ First, define a custom secure model:
58
+
59
+ ```python
60
+ from typing import Any, override
61
+ from pydantic import BaseModel as PydanticBaseModel
62
+ from pydantic_encryption import SecureModel
63
+
64
+ class MySecureModel(PydanticBaseModel, SecureModel):
65
+ @override
66
+ def encrypt_data(self) -> None:
67
+ # Your encryption logic here
68
+ pass
69
+
70
+ @override
71
+ def decrypt_data(self) -> None:
72
+ # Your decryption logic here
73
+ pass
74
+
75
+ @override
76
+ def hash_data(self) -> None:
77
+ # Your hashing logic here
78
+ pass
79
+
80
+ @override
81
+ def model_post_init(self, context: Any, /) -> None:
82
+ if not self._disable:
83
+ if self.pending_decryption_fields:
84
+ self.decrypt_data()
85
+
86
+ if self.pending_encryption_fields:
87
+ self.encrypt_data()
88
+
89
+ if self.pending_hash_fields:
90
+ self.hash_data()
91
+
92
+ super().model_post_init(context)
93
+ ```
94
+
95
+ Then use it:
96
+
97
+ ```python
98
+ from typing import Annotated
99
+ from pydantic import BaseModel # Here, we don't use the BaseModel provided by the library, but the native one from Pydantic
100
+ from pydantic_encryption import Encrypt
101
+
102
+ class MyModel(BaseModel, MySecureModel):
103
+ username: str
104
+ address: Annotated[str, Encrypt]
105
+
106
+ model = MyModel(username="john_doe", address="123456")
107
+ print(model.address) # encrypted
108
+ ```
109
+
110
+ ## Encryption
111
+
112
+ You can encrypt any field by using the `Encrypt` annotation with `Annotated` and inheriting from `BaseModel`.
113
+
114
+ ```python
115
+ from typing import Annotated
116
+ from pydantic_encryption import Encrypt, BaseModel
117
+
118
+ class User(BaseModel):
119
+ name: str
120
+ address: Annotated[str, Encrypt] # This field will be encrypted
121
+
122
+ user = User(name="John Doe", address="123456")
123
+ print(user.address) # encrypted
124
+ print(user.name) # plaintext (untouched)
125
+ ```
126
+
127
+ The fields marked with `Encrypt` are automatically encrypted during model initialization.
128
+
129
+ ## Decryption
130
+
131
+ Similar to encryption, you can decrypt any field by using the `Decrypt` annotation with `Annotated` and inheriting from `BaseModel`.
132
+
133
+ ```python
134
+ from typing import Annotated
135
+ from pydantic_encryption import Decrypt, BaseModel
136
+
137
+ class UserResponse(BaseModel):
138
+ name: str
139
+ address: Annotated[str, Decrypt] # This field will be decrypted
140
+
141
+ user = UserResponse(**user_data) # encrypted value
142
+ print(user.address) # decrypted
143
+ print(user.name) # plaintext (untouched)
144
+ ```
145
+
146
+ Fields marked with `Decrypt` are automatically decrypted during model initialization.
147
+
148
+
149
+ ## Hashing
150
+
151
+ You can hash sensitive data like passwords by using the `Hash` annotation.
152
+
153
+ ```python
154
+ from typing import Annotated
155
+ from pydantic_encryption import Hash, BaseModel
156
+
157
+ class User(BaseModel):
158
+ username: str
159
+ password: Annotated[str, Hash] # This field will be hashed
160
+
161
+ user = User(username="john_doe", password="secret123")
162
+ print(user.password) # hashed value
163
+ ```
164
+
165
+ Fields marked with `Hash` are automatically hashed using bcrypt during model initialization.
166
+
167
+ ## Disable Auto Processing
168
+
169
+ You can disable automatic encryption/decryption/hashing by setting `disable` to `True` in the class definition.
170
+
171
+ ```python
172
+ from typing import Annotated
173
+ from pydantic_encryption import Encrypt, BaseModel
174
+
175
+ class UserResponse(BaseModel, disable=True):
176
+ name: str
177
+ address: Annotated[str, Encrypt]
178
+
179
+ # To encrypt/decrypt/hash, call the respective methods manually:
180
+ user = UserResponse(name="John Doe", address="123 Main St")
181
+
182
+ # Manual encryption
183
+ user.encrypt_data()
184
+ print(user.address) # encrypted
185
+
186
+ # Or user.decrypt_data() to decrypt and user.hash_data() to hash
187
+ ```
188
+
189
+ ## Generics
190
+
191
+ Each BaseModel has an additional helpful method that will tell you its generic type.
192
+
193
+ To use generics, you must install this package with the `generics` extra: `pip install pydantic_encryption[generics]`.
194
+
195
+ ```py
196
+ from pydantic_encryption import BaseModel
197
+
198
+ class MyModel[T](BaseModel):
199
+ value: T
200
+
201
+ model = MyModel[str](value="Hello")
202
+ print(model.get_type()) # <class 'str'>
203
+ ```
204
+
205
+ ## Run Tests
206
+
207
+ Install [Poetry](https://python-poetry.org/docs/) and run:
208
+
209
+ ```bash
210
+ poetry install --with test
211
+ poetry run coverage run -m pytest -v -s
212
+ ```
213
+
214
+ ## Roadmap
215
+
216
+ This is an early development version. I am considering the following features:
217
+
218
+ - [ ] Add optional support for other encryption providers beyond Evervault
219
+
220
+ ## Feature Requests
221
+
222
+ If you have any feature requests, please open an issue.
@@ -1,4 +1,4 @@
1
- from typing import override, Any
1
+ from typing import Any, Optional
2
2
  from pydantic_settings import BaseSettings
3
3
 
4
4
 
@@ -6,16 +6,15 @@ class Settings(BaseSettings):
6
6
  """Settings for the package."""
7
7
 
8
8
  # Evervault settings
9
- EVERVAULT_API_KEY: str | None = None
10
- EVERVAULT_APP_ID: str | None = None
11
- EVERVAULT_ENCRYPTION_ROLE: str | None = None
9
+ EVERVAULT_API_KEY: Optional[str] = None
10
+ EVERVAULT_APP_ID: Optional[str] = None
11
+ EVERVAULT_ENCRYPTION_ROLE: Optional[str] = None
12
12
 
13
13
  class Config:
14
14
  env_file = [".env.local", ".env"]
15
15
  case_sensitive = True
16
16
  extra = "ignore"
17
17
 
18
- @override
19
18
  def model_post_init(self, context: Any, /) -> None:
20
19
  try:
21
20
  import evervault # pylint: disable=unused-import
@@ -0,0 +1,2 @@
1
+ from .secure_model import *
2
+ from .base import *
@@ -1,6 +1,6 @@
1
- from typing import Any, override
1
+ from typing import Any, Optional
2
2
  from pydantic import BaseModel as PydanticBaseModel
3
- from .encryption import EncryptableObject
3
+ from . import SecureModel
4
4
 
5
5
  try:
6
6
  from generics import get_filled_type
@@ -10,12 +10,11 @@ except ImportError:
10
10
  __all__ = ["BaseModel"]
11
11
 
12
12
 
13
- class BaseModel(PydanticBaseModel, EncryptableObject):
13
+ class BaseModel(PydanticBaseModel, SecureModel):
14
14
  """Base model for encryptable models."""
15
15
 
16
16
  _generic_type_value: Any = None
17
17
 
18
- @override
19
18
  def model_post_init(self, context: Any, /) -> None:
20
19
  if not self._disable:
21
20
  if self.pending_decryption_fields:
@@ -24,9 +23,12 @@ class BaseModel(PydanticBaseModel, EncryptableObject):
24
23
  if self.pending_encryption_fields:
25
24
  self.encrypt_data()
26
25
 
26
+ if self.pending_hash_fields:
27
+ self.hash_data()
28
+
27
29
  super().model_post_init(context)
28
30
 
29
- def get_type(self) -> type | None:
31
+ def get_type(self) -> Optional[type]:
30
32
  """Get the type of the model."""
31
33
 
32
34
  if not get_filled_type:
@@ -1,12 +1,7 @@
1
- from typing import (
2
- Any,
3
- Annotated,
4
- get_type_hints,
5
- Union,
6
- get_args,
7
- get_origin,
8
- )
1
+ from typing import Any, Annotated, get_type_hints, Union, get_args, get_origin, Optional
2
+ import bcrypt
9
3
  from pydantic_encryption.config import settings
4
+ from pydantic import BaseModel
10
5
 
11
6
  try:
12
7
  import evervault
@@ -18,34 +13,31 @@ else:
18
13
  )
19
14
 
20
15
  __all__ = [
21
- "EncryptField",
22
- "DecryptField",
23
- "EncryptableObject",
16
+ "Encrypt",
17
+ "Decrypt",
18
+ "Hash",
19
+ "SecureModel",
24
20
  ]
25
21
 
26
22
 
27
- class _EncryptedFieldValue:
28
- pass
23
+ class Encrypt:
24
+ """Annotation to mark fields for encryption."""
29
25
 
30
26
 
31
- class _DecryptedFieldValue:
32
- pass
27
+ class Decrypt:
28
+ """Annotation to mark fields for decryption."""
33
29
 
34
30
 
35
- EncryptField = Annotated[
36
- str, _EncryptedFieldValue
37
- ] # Fields with this annotation will be encrypted
38
- DecryptField = Annotated[
39
- str, _DecryptedFieldValue
40
- ] # Fields with this annotation will be decrypted
31
+ class Hash:
32
+ """Annotation to mark fields for hashing."""
41
33
 
42
34
 
43
- class EncryptableObject:
44
- """Base class for encryptable models."""
35
+ class SecureModel:
36
+ """Base class for encryptable and hashable models."""
45
37
 
46
- _disable: bool | None = None
38
+ _disable: Optional[bool] = None
47
39
 
48
- def __init_subclass__(cls, disable: bool | None = None, **kwargs):
40
+ def __init_subclass__(cls, disable: Optional[bool] = None, **kwargs) -> None:
49
41
  super().__init_subclass__(**kwargs)
50
42
 
51
43
  cls._disable = disable
@@ -92,9 +84,24 @@ class EncryptableObject:
92
84
  for field_name, value in decrypted_data.items():
93
85
  setattr(self, field_name, value)
94
86
 
87
+ def hash_data(self) -> None:
88
+ """Hash fields marked with `Hash` annotation."""
89
+
90
+ if self._disable:
91
+ return
92
+
93
+ if not self.pending_hash_fields:
94
+ return
95
+
96
+ for field_name, value in self.pending_hash_fields.items():
97
+ salt = bcrypt.gensalt()
98
+ hashed = bcrypt.hashpw(value.encode("utf-8"), salt)
99
+
100
+ setattr(self, field_name, hashed)
101
+
95
102
  @staticmethod
96
103
  def get_annotated_fields(
97
- instance: "BaseModel", obj: dict[str, Any] | None = None, *annotations: type
104
+ instance: "BaseModel", obj: Optional[dict[str, Any]] = None, *annotations: type
98
105
  ) -> dict[str, str]:
99
106
  """Get fields that have the specified annotations, handling union types.
100
107
 
@@ -163,10 +170,16 @@ class EncryptableObject:
163
170
  def pending_encryption_fields(self) -> dict[str, str]:
164
171
  """Get all encrypted fields from the model."""
165
172
 
166
- return self.get_annotated_fields(self, None, _EncryptedFieldValue)
173
+ return self.get_annotated_fields(self, None, Encrypt)
167
174
 
168
175
  @property
169
176
  def pending_decryption_fields(self) -> dict[str, str]:
170
177
  """Get all decrypted fields from the model."""
171
178
 
172
- return self.get_annotated_fields(self, None, _DecryptedFieldValue)
179
+ return self.get_annotated_fields(self, None, Decrypt)
180
+
181
+ @property
182
+ def pending_hash_fields(self) -> dict[str, str]:
183
+ """Get all hashable fields from the model."""
184
+
185
+ return self.get_annotated_fields(self, None, Hash)
@@ -0,0 +1,25 @@
1
+ [project]
2
+ name = "pydantic_encryption"
3
+ version = "0.0.2a2"
4
+ description = "Encryption and hashing models for Pydantic"
5
+ authors = [
6
+ { name = "Julien Kmec", email = "me@julien.dev" },
7
+ ]
8
+ readme = "README.md"
9
+ packages = [{include = "pydantic_encryption"}]
10
+ requires-python = ">=3.9,<4.0.0"
11
+ dependencies = [
12
+ "pydantic>=2.10.6",
13
+ "pydantic-settings>=2.9.1",
14
+ "bcrypt (>=4.3.0,<5.0.0)",
15
+ ]
16
+
17
+ [project.optional-dependencies]
18
+ evervault = ["evervault>=4.4.1"]
19
+ generics = ["python-generics>=0.2.3"]
20
+ test = ["pytest>=8.3.5", "coverage>=7.8.0"]
21
+ all = ["evervault>=4.4.1", "python-generics>=0.2.3", "pytest>=8.3.5", "coverage>=7.8.0"]
22
+
23
+ [build-system]
24
+ requires = ["poetry-core", "setuptools"]
25
+ build-backend = "poetry.core.masonry.api"
@@ -1,199 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: pydantic_encryption
3
- Version: 0.0.2a0
4
- Summary: Encryption models for Pydantic
5
- Author: Julien Kmec
6
- Author-email: me@julien.dev
7
- Requires-Python: >=3.12,<3.13
8
- Classifier: Programming Language :: Python :: 3
9
- Classifier: Programming Language :: Python :: 3.12
10
- Requires-Dist: pydantic (>=2.10.6,<3.0.0)
11
- Requires-Dist: pydantic-settings (>=2.9.1,<3.0.0)
12
- Description-Content-Type: text/markdown
13
-
14
- # Encryption models for Pydantic
15
-
16
- This package provides Pydantic field types that encrypt and decrypt the field values.
17
-
18
- ## Installation
19
-
20
- Install [Poetry](https://python-poetry.org/) if you haven't already.
21
-
22
- Install this package with the `evervault` and `generics` extras:
23
- ```bash
24
- poetry add pydantic_encryption --with evervault,generics
25
- ```
26
-
27
- Install this package without extras:
28
- ```bash
29
- poetry add pydantic_encryption
30
- ```
31
-
32
- ## Features
33
-
34
- - Encrypt and decrypt fields
35
- - Support for BaseModel inheritance
36
- - Support for generics
37
-
38
- ## Example
39
-
40
- ```py
41
- from pydantic_encryption import BaseModel, EncryptField
42
-
43
- class User(BaseModel):
44
- name: str
45
- password: EncryptField # This field will be encrypted
46
-
47
- user = User(name="John Doe", password="123456")
48
- print(user.password) # encrypted
49
- ```
50
-
51
-
52
-
53
- ## Choose an Encryption Method
54
-
55
- ### Evervault
56
-
57
- If you install this package with the `evervault` extra, you can use [Evervault](https://evervault.com/) to encrypt and decrypt fields.
58
-
59
- You need to set the following environment variables or add them to your `.env` file:
60
-
61
- ```bash
62
- EVERVAULT_APP_ID=your_app_id
63
- EVERVAULT_API_KEY=your_api_key
64
- EVERVAULT_ENCRYPTION_ROLE=your_encryption_role
65
- ```
66
-
67
- ### Custom Encryption
68
-
69
- You can define your own encryption and decryption methods by subclassing `EncryptableObject`.
70
-
71
- `EncryptableObject` provides you with the utilities to handle encryption and decryption.
72
-
73
- `self.pending_encryption_fields` and `self.pending_decryption_fields` are dictionaries of field names to field values that need to be encrypted or decrypted, i.e., fields annotated with `EncryptField` or `DecryptField`.
74
-
75
- You can override the `encrypt_data` and `decrypt_data` methods to implement your own encryption and decryption logic.
76
-
77
- You then need to override `model_post_init` to call `self.encrypt_data()` and/or `self.decrypt_data()`.
78
-
79
- First, define a custom encryptable object:
80
-
81
- ```py
82
- from typing import override
83
- from pydantic_encryption import EncryptableObject
84
-
85
- class MyEncryptableObject(EncryptableObject):
86
- @override
87
- def encrypt_data(self) -> None:
88
- # Your encryption logic here
89
- pass
90
-
91
- @override
92
- def decrypt_data(self) -> None:
93
- # Your decryption logic here
94
- pass
95
-
96
- @override
97
- def model_post_init(self, context: Any, /) -> None:
98
- if not self._disable:
99
- if self.pending_decryption_fields:
100
- self.decrypt_data()
101
-
102
- if self.pending_encryption_fields:
103
- self.encrypt_data()
104
-
105
- super().model_post_init(context)
106
- ```
107
-
108
- Then use it:
109
-
110
- ```py
111
- from pydantic import BaseModel # Here, we don't use the BaseModel provided by the library, but the native one from Pydantic
112
- from pydantic_encryption import EncryptField
113
-
114
- class MyModel(BaseModel, MyEncryptableObject):
115
- username: str
116
- password: EncryptField
117
-
118
- model = MyModel(username="john_doe", password="123456")
119
- print(model.password) # encrypted
120
- ```
121
-
122
- ## Encryption
123
-
124
- You can encrypt any field by annotating with `EncryptField` and inheriting from `EncryptableObject`.
125
-
126
- ```py
127
- from pydantic_encryption import EncryptableObject, EncryptField, BaseModel
128
-
129
- class User(BaseModel):
130
- name: str
131
- password: EncryptField # This field will be encrypted
132
-
133
- user = User(name="John Doe", password="123456")
134
- print(user.password) # encrypted
135
- print(user.name) # plaintext (untouched)
136
- ```
137
-
138
- ## Decryption
139
-
140
- Similar to encryption, you can decrypt any field by annotating with `DecryptField` and inheriting from `EncryptableObject`.
141
-
142
- ```py
143
- from pydantic_encryption import EncryptableObject, DecryptField, BaseModel
144
-
145
- class User(BaseModel, EncryptableObject):
146
- name: str
147
- password: DecryptField # This field will be decrypted
148
-
149
- user = User(name="John Doe", password="123456")
150
- print(user.password) # encrypted
151
- print(user.name) # plaintext (untouched)
152
-
153
- ```
154
-
155
-
156
- ## Disable Auto-Encryption/Decryption
157
-
158
- You can disable auto-encryption/decryption by setting `disable` to `True` in the class definition.
159
-
160
- ```py
161
- from pydantic_encryption import EncryptableObject, EncryptField, BaseModel
162
-
163
- class UserResponse(BaseModel, EncryptableObject, disable=True):
164
- name: str
165
- password: EncryptField
166
-
167
- # To encrypt/decrypt, call `encrypt_data()` or `decrypt_data()`:
168
- user = UserResponse(name="John Doe", password="ENCRYPTED_PASSWORD")
169
-
170
- user.decrypt_data()
171
- print(user.password) # decrypted
172
-
173
- user.encrypt_data()
174
- print(user.password) # encrypted
175
- ```
176
-
177
- ## Generics
178
-
179
- Each BaseModel has an additional helpful method that will tell you its generic type.
180
-
181
- To use generics, you must install this package with the `generics` extra: `poetry add pydantic_encryption --with generics`.
182
-
183
- ```py
184
- from pydantic_encryption import BaseModel
185
-
186
- class MyModel[T](BaseModel):
187
- value: T
188
-
189
- model = MyModel[str](value="Hello")
190
- print(model.get_type()) # <class 'str'>
191
- ```
192
-
193
- ## Run Tests
194
-
195
- ```bash
196
- poetry install --with test
197
- poetry run coverage run -m pytest -v -s
198
- ```
199
-
@@ -1,185 +0,0 @@
1
- # Encryption models for Pydantic
2
-
3
- This package provides Pydantic field types that encrypt and decrypt the field values.
4
-
5
- ## Installation
6
-
7
- Install [Poetry](https://python-poetry.org/) if you haven't already.
8
-
9
- Install this package with the `evervault` and `generics` extras:
10
- ```bash
11
- poetry add pydantic_encryption --with evervault,generics
12
- ```
13
-
14
- Install this package without extras:
15
- ```bash
16
- poetry add pydantic_encryption
17
- ```
18
-
19
- ## Features
20
-
21
- - Encrypt and decrypt fields
22
- - Support for BaseModel inheritance
23
- - Support for generics
24
-
25
- ## Example
26
-
27
- ```py
28
- from pydantic_encryption import BaseModel, EncryptField
29
-
30
- class User(BaseModel):
31
- name: str
32
- password: EncryptField # This field will be encrypted
33
-
34
- user = User(name="John Doe", password="123456")
35
- print(user.password) # encrypted
36
- ```
37
-
38
-
39
-
40
- ## Choose an Encryption Method
41
-
42
- ### Evervault
43
-
44
- If you install this package with the `evervault` extra, you can use [Evervault](https://evervault.com/) to encrypt and decrypt fields.
45
-
46
- You need to set the following environment variables or add them to your `.env` file:
47
-
48
- ```bash
49
- EVERVAULT_APP_ID=your_app_id
50
- EVERVAULT_API_KEY=your_api_key
51
- EVERVAULT_ENCRYPTION_ROLE=your_encryption_role
52
- ```
53
-
54
- ### Custom Encryption
55
-
56
- You can define your own encryption and decryption methods by subclassing `EncryptableObject`.
57
-
58
- `EncryptableObject` provides you with the utilities to handle encryption and decryption.
59
-
60
- `self.pending_encryption_fields` and `self.pending_decryption_fields` are dictionaries of field names to field values that need to be encrypted or decrypted, i.e., fields annotated with `EncryptField` or `DecryptField`.
61
-
62
- You can override the `encrypt_data` and `decrypt_data` methods to implement your own encryption and decryption logic.
63
-
64
- You then need to override `model_post_init` to call `self.encrypt_data()` and/or `self.decrypt_data()`.
65
-
66
- First, define a custom encryptable object:
67
-
68
- ```py
69
- from typing import override
70
- from pydantic_encryption import EncryptableObject
71
-
72
- class MyEncryptableObject(EncryptableObject):
73
- @override
74
- def encrypt_data(self) -> None:
75
- # Your encryption logic here
76
- pass
77
-
78
- @override
79
- def decrypt_data(self) -> None:
80
- # Your decryption logic here
81
- pass
82
-
83
- @override
84
- def model_post_init(self, context: Any, /) -> None:
85
- if not self._disable:
86
- if self.pending_decryption_fields:
87
- self.decrypt_data()
88
-
89
- if self.pending_encryption_fields:
90
- self.encrypt_data()
91
-
92
- super().model_post_init(context)
93
- ```
94
-
95
- Then use it:
96
-
97
- ```py
98
- from pydantic import BaseModel # Here, we don't use the BaseModel provided by the library, but the native one from Pydantic
99
- from pydantic_encryption import EncryptField
100
-
101
- class MyModel(BaseModel, MyEncryptableObject):
102
- username: str
103
- password: EncryptField
104
-
105
- model = MyModel(username="john_doe", password="123456")
106
- print(model.password) # encrypted
107
- ```
108
-
109
- ## Encryption
110
-
111
- You can encrypt any field by annotating with `EncryptField` and inheriting from `EncryptableObject`.
112
-
113
- ```py
114
- from pydantic_encryption import EncryptableObject, EncryptField, BaseModel
115
-
116
- class User(BaseModel):
117
- name: str
118
- password: EncryptField # This field will be encrypted
119
-
120
- user = User(name="John Doe", password="123456")
121
- print(user.password) # encrypted
122
- print(user.name) # plaintext (untouched)
123
- ```
124
-
125
- ## Decryption
126
-
127
- Similar to encryption, you can decrypt any field by annotating with `DecryptField` and inheriting from `EncryptableObject`.
128
-
129
- ```py
130
- from pydantic_encryption import EncryptableObject, DecryptField, BaseModel
131
-
132
- class User(BaseModel, EncryptableObject):
133
- name: str
134
- password: DecryptField # This field will be decrypted
135
-
136
- user = User(name="John Doe", password="123456")
137
- print(user.password) # encrypted
138
- print(user.name) # plaintext (untouched)
139
-
140
- ```
141
-
142
-
143
- ## Disable Auto-Encryption/Decryption
144
-
145
- You can disable auto-encryption/decryption by setting `disable` to `True` in the class definition.
146
-
147
- ```py
148
- from pydantic_encryption import EncryptableObject, EncryptField, BaseModel
149
-
150
- class UserResponse(BaseModel, EncryptableObject, disable=True):
151
- name: str
152
- password: EncryptField
153
-
154
- # To encrypt/decrypt, call `encrypt_data()` or `decrypt_data()`:
155
- user = UserResponse(name="John Doe", password="ENCRYPTED_PASSWORD")
156
-
157
- user.decrypt_data()
158
- print(user.password) # decrypted
159
-
160
- user.encrypt_data()
161
- print(user.password) # encrypted
162
- ```
163
-
164
- ## Generics
165
-
166
- Each BaseModel has an additional helpful method that will tell you its generic type.
167
-
168
- To use generics, you must install this package with the `generics` extra: `poetry add pydantic_encryption --with generics`.
169
-
170
- ```py
171
- from pydantic_encryption import BaseModel
172
-
173
- class MyModel[T](BaseModel):
174
- value: T
175
-
176
- model = MyModel[str](value="Hello")
177
- print(model.get_type()) # <class 'str'>
178
- ```
179
-
180
- ## Run Tests
181
-
182
- ```bash
183
- poetry install --with test
184
- poetry run coverage run -m pytest -v -s
185
- ```
@@ -1,2 +0,0 @@
1
- from .encryption import *
2
- from .base import *
@@ -1,32 +0,0 @@
1
- [tool.poetry]
2
- name = "pydantic_encryption"
3
- version = "0.0.2a0"
4
- description = "Encryption models for Pydantic"
5
- authors = ["Julien Kmec <me@julien.dev>"]
6
- readme = "README.md"
7
- packages = [{include = "pydantic_encryption"}]
8
-
9
- [tool.poetry.dependencies]
10
- python = "<3.13, >=3.12"
11
- pydantic = "^2.10.6"
12
- pydantic-settings = "^2.9.1"
13
-
14
- [tool.poetry.group.evervault]
15
- optional = true
16
-
17
- [tool.poetry.group.evervault.dependencies]
18
- evervault = "^4.4.1"
19
-
20
- [tool.poetry.group.generics]
21
- optional = true
22
-
23
- [tool.poetry.group.generics.dependencies]
24
- python-generics = "^0.2.3"
25
-
26
- [tool.poetry.group.test.dependencies]
27
- pytest = "^8.3.5"
28
- coverage = "^7.8.0"
29
-
30
- [build-system]
31
- requires = ["poetry-core", "setuptools"]
32
- build-backend = "poetry.core.masonry.api"