File tree Expand file tree Collapse file tree 3 files changed +38
-1
lines changed Expand file tree Collapse file tree 3 files changed +38
-1
lines changed Original file line number Diff line number Diff line change 1
1
[tool .poetry ]
2
2
name = " sqlorm-py"
3
- version = " 0.2.1 "
3
+ version = " 0.2.2 "
4
4
description = " A new kind or ORM that do not abstract away your database or SQL queries."
5
5
authors = [
" Maxime Bouroumeau-Fuseau <[email protected] >" ]
6
6
readme = " README.md"
@@ -13,6 +13,7 @@ python = "^3.10"
13
13
blinker = " ^1.8.2"
14
14
psycopg = { extras = [" binary" ], version = " ^3.1.18" , optional = true }
15
15
mysql-connector-python = { version = " ^8.3.0" , optional = true }
16
+ pycryptodome = { version = " ^3.20.0" , optional = true }
16
17
17
18
[tool .poetry .group .dev .dependencies ]
18
19
pytest = " ^8.0.0"
Original file line number Diff line number Diff line change @@ -57,6 +57,8 @@ def __str__(self):
57
57
}
58
58
)
59
59
60
+ from .encrypted import Encrypted
61
+
60
62
__all__ = (
61
63
"SQLType" ,
62
64
"Integer" ,
@@ -68,4 +70,5 @@ def __str__(self):
68
70
"DateTime" ,
69
71
"JSON" ,
70
72
"Pickle" ,
73
+ "Encrypted"
71
74
)
Original file line number Diff line number Diff line change
1
+ import base64
2
+ from Crypto .Cipher import AES
3
+ from Crypto import Random
4
+ import os
5
+ from . import SQLType
6
+
7
+
8
+ class Encrypted (SQLType ):
9
+ block_size = 16
10
+
11
+ def __init__ (self , key ):
12
+ super ().__init__ ("text" , self .decrypt , self .encrypt )
13
+ self .key = key
14
+
15
+ def encrypt (self , raw ):
16
+ raw = self .pad (raw ).encode ()
17
+ iv = Random .new ().read (AES .block_size )
18
+ key = self .key () if callable (self .key ) else self .key
19
+ cipher = AES .new (key , AES .MODE_CBC , iv )
20
+ return base64 .b64encode (iv + cipher .encrypt (raw ))
21
+
22
+ def decrypt (self , enc ):
23
+ enc = base64 .b64decode (enc )
24
+ iv = enc [:16 ]
25
+ key = self .key () if callable (self .key ) else self .key
26
+ cipher = AES .new (key , AES .MODE_CBC , iv )
27
+ return self .unpad (cipher .decrypt (enc [16 :]).decode ())
28
+
29
+ def pad (self , s ):
30
+ return s + (self .block_size - len (s ) % self .block_size ) * chr (self .block_size - len (s ) % self .block_size )
31
+
32
+ def unpad (self , s ):
33
+ return s [:- ord (s [len (s )- 1 :])]
You can’t perform that action at this time.
0 commit comments