codenerix-lib 1.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.
@@ -0,0 +1,9 @@
1
+ __version__ = "1.1.0"
2
+
3
+ __authors__ = [
4
+ "Juanmi Taboada",
5
+ ]
6
+
7
+ __authors_email__ = [
8
+ "juanmi@juanmitaboada.com",
9
+ ]
@@ -0,0 +1,121 @@
1
+ #!/usr/bin/env python3
2
+ #
3
+ # django-codenerix
4
+ #
5
+ # Codenerix GNU
6
+ #
7
+ # Project URL : http://www.codenerix.com
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ """
21
+ Colors definition
22
+ """
23
+
24
+ from colorama import init
25
+
26
+ init()
27
+
28
+ __version__ = "201109111106"
29
+
30
+
31
+ __all__ = ["colors"]
32
+
33
+
34
+ # Colors
35
+ colors = {
36
+ # Simple
37
+ "simplegrey": (0, 30),
38
+ "simplered": (0, 31),
39
+ "simplegreen": (0, 32),
40
+ "simpleyellow": (0, 33),
41
+ "simpleblue": (0, 34),
42
+ "simplepurple": (0, 35),
43
+ "simplecyan": (0, 36),
44
+ "simplewhite": (0, 37),
45
+ # Bold
46
+ "grey": (1, 30),
47
+ "red": (1, 31),
48
+ "green": (1, 32),
49
+ "yellow": (1, 33),
50
+ "blue": (1, 34),
51
+ "purple": (1, 35),
52
+ "cyan": (1, 36),
53
+ "white": (1, 37),
54
+ # Close
55
+ "close": (1, 0),
56
+ }
57
+
58
+ # HTML Colors
59
+ html_colors = {
60
+ # Simple
61
+ "simplegrey": (0, (128, 128, 128)),
62
+ "simplered": (0, (255, 0, 0)),
63
+ "simplegreen": (0, (0, 255, 0)),
64
+ "simpleyellow": (0, (255, 255, 0)),
65
+ "simpleblue": (0, (0, 0, 255)),
66
+ "simplepurple": (0, (128, 0, 128)),
67
+ "simplecyan": (0, (0, 255, 255)),
68
+ "simplewhite": (0, (255, 255, 255)),
69
+ # Bold
70
+ "grey": (1, (169, 169, 169)),
71
+ "red": (1, (255, 69, 0)),
72
+ "green": (1, (0, 128, 0)),
73
+ "yellow": (1, (255, 255, 0)),
74
+ "blue": (1, (0, 0, 255)),
75
+ "purple": (1, (128, 0, 128)),
76
+ "cyan": (1, (0, 255, 255)),
77
+ "white": (1, (255, 255, 255)),
78
+ }
79
+
80
+
81
+ def colorize(msg, color=None):
82
+ # Colors
83
+ if color in colors:
84
+ (darkbit, subcolor) = colors[color]
85
+ else:
86
+ (darkbit, subcolor) = (1, 0)
87
+
88
+ # Prepare the message
89
+ result = f"\033[{darkbit:1d};{subcolor:02d}m"
90
+ result += msg
91
+ result += "\033[1;00m"
92
+
93
+ # Return the result
94
+ return result
95
+
96
+
97
+ def get_colors():
98
+ # Reorder colors
99
+ keys = []
100
+ for key in colors.keys():
101
+ keys.append((colors[key][0], colors[key][1], key))
102
+ keys.sort()
103
+
104
+ # Show up all colors
105
+ buffer = ""
106
+ for color in keys:
107
+ # Get the color information
108
+ (simplebit, subcolor) = colors[color[2]]
109
+ if len(buffer) > 0:
110
+ buffer += "\n"
111
+ # Show it
112
+ buffer += (
113
+ f"{simplebit:1d}:{subcolor:02d} - "
114
+ f"\033[{simplebit:1d};{subcolor:02d}m{color[2]:<14s}"
115
+ f"\033[1;00m{color[2]:<s}"
116
+ )
117
+ return buffer
118
+
119
+
120
+ if __name__ == "__main__": # pragma: no cover
121
+ print(get_colors())
@@ -0,0 +1,81 @@
1
+ #
2
+ # django-codenerix-extensions
3
+ #
4
+ # Codenerix GNU
5
+ #
6
+ # Project URL : http://www.codenerix.com
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+ #
20
+ # This library is intended for encrypting as CODENERIX's Cryptography
21
+ # Libraries work, the basis for the library to work are, the KEY is always
22
+ # a SHA256 from your real KEY, the IV vector should be generated automatically
23
+ # on encryption time by the library and must be inserted at the beginning
24
+ # of the encrypted string (which shoul be padded with PKCS7). The full
25
+ # string IV+ENCRYPTED is encoded to Base64. For decrypting the process is
26
+ # the opossite, the raw string is decoded from Base64, the first 16 bytes
27
+ # are taken as the IV and the rest as the encrypted string, the key is hashed
28
+ # with SHA256 and then the encrypted string is decrypted using the IV and
29
+ # the hashed key.
30
+ import base64
31
+ import hashlib
32
+
33
+ from Cryptodome import Random
34
+ from Cryptodome.Cipher import AES
35
+
36
+
37
+ class AESCipher:
38
+ """
39
+ Adapted solution from: https://stackoverflow.com/a/21928790/1481040
40
+ """
41
+
42
+ def __init__(self):
43
+ self.bs = 32
44
+
45
+ def encrypt(self, raw, key, iv=None, b64encoded=True):
46
+ raw = self._pad(raw)
47
+ if iv is None:
48
+ iv = Random.new().read(AES.block_size)
49
+ hashkey = hashlib.sha256(key.encode()).digest()
50
+ cipher = AES.new(hashkey, AES.MODE_CBC, iv)
51
+
52
+ if not isinstance(raw, bytes):
53
+ raw = raw.encode()
54
+
55
+ if b64encoded:
56
+ return base64.b64encode(iv + cipher.encrypt(raw))
57
+ else:
58
+ return iv + cipher.encrypt(raw)
59
+
60
+ def decrypt(self, enc, key, b64encoded=True):
61
+ if b64encoded:
62
+ enc = base64.b64decode(enc)
63
+ iv = enc[: AES.block_size]
64
+ if not isinstance(key, bytes):
65
+ key = key.encode()
66
+ hashkey = hashlib.sha256(key).digest()
67
+ cipher = AES.new(hashkey, AES.MODE_CBC, iv)
68
+ unpad = self._unpad(cipher.decrypt(enc[AES.block_size :]))
69
+ if not isinstance(unpad, bytes):
70
+ unpad = unpad.decode("utf-8")
71
+ return unpad
72
+
73
+ def _pad(self, s):
74
+ pad = (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)
75
+ if isinstance(s, bytes):
76
+ pad = pad.encode()
77
+ return s + pad
78
+
79
+ @staticmethod
80
+ def _unpad(s):
81
+ return s[: -ord(s[len(s) - 1 :])]