libdev 0.83__tar.gz → 0.85__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.
- {libdev-0.83 → libdev-0.85}/PKG-INFO +2 -1
- {libdev-0.83 → libdev-0.85}/README.md +1 -0
- {libdev-0.83 → libdev-0.85}/libdev/__init__.py +1 -1
- libdev-0.85/libdev/crypt.py +112 -0
- {libdev-0.83 → libdev-0.85}/libdev/time.py +10 -0
- {libdev-0.83 → libdev-0.85}/libdev.egg-info/PKG-INFO +2 -1
- {libdev-0.83 → libdev-0.85}/libdev.egg-info/SOURCES.txt +2 -0
- libdev-0.85/tests/test_crypt.py +12 -0
- {libdev-0.83 → libdev-0.85}/tests/test_time.py +16 -1
- {libdev-0.83 → libdev-0.85}/LICENSE +0 -0
- {libdev-0.83 → libdev-0.85}/libdev/cfg.py +0 -0
- {libdev-0.83 → libdev-0.85}/libdev/check.py +0 -0
- {libdev-0.83 → libdev-0.85}/libdev/codes.py +0 -0
- {libdev-0.83 → libdev-0.85}/libdev/dev.py +0 -0
- {libdev-0.83 → libdev-0.85}/libdev/doc.py +0 -0
- {libdev-0.83 → libdev-0.85}/libdev/fin.py +0 -0
- {libdev-0.83 → libdev-0.85}/libdev/gen.py +0 -0
- {libdev-0.83 → libdev-0.85}/libdev/img.py +0 -0
- {libdev-0.83 → libdev-0.85}/libdev/lang.py +0 -0
- {libdev-0.83 → libdev-0.85}/libdev/log.py +0 -0
- {libdev-0.83 → libdev-0.85}/libdev/num.py +0 -0
- {libdev-0.83 → libdev-0.85}/libdev/req.py +0 -0
- {libdev-0.83 → libdev-0.85}/libdev/s3.py +0 -0
- {libdev-0.83 → libdev-0.85}/libdev.egg-info/dependency_links.txt +0 -0
- {libdev-0.83 → libdev-0.85}/libdev.egg-info/requires.txt +0 -0
- {libdev-0.83 → libdev-0.85}/libdev.egg-info/top_level.txt +0 -0
- {libdev-0.83 → libdev-0.85}/setup.cfg +0 -0
- {libdev-0.83 → libdev-0.85}/setup.py +0 -0
- {libdev-0.83 → libdev-0.85}/tests/test_cfg.py +0 -0
- {libdev-0.83 → libdev-0.85}/tests/test_check.py +0 -0
- {libdev-0.83 → libdev-0.85}/tests/test_codes.py +0 -0
- {libdev-0.83 → libdev-0.85}/tests/test_dev.py +0 -0
- {libdev-0.83 → libdev-0.85}/tests/test_doc.py +0 -0
- {libdev-0.83 → libdev-0.85}/tests/test_gen.py +0 -0
- {libdev-0.83 → libdev-0.85}/tests/test_img.py +0 -0
- {libdev-0.83 → libdev-0.85}/tests/test_lang.py +0 -0
- {libdev-0.83 → libdev-0.85}/tests/test_log.py +0 -0
- {libdev-0.83 → libdev-0.85}/tests/test_num.py +0 -0
- {libdev-0.83 → libdev-0.85}/tests/test_req.py +0 -0
- {libdev-0.83 → libdev-0.85}/tests/test_s3.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: libdev
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.85
|
|
4
4
|
Summary: Set of standard functions for development
|
|
5
5
|
Home-page: https://github.com/chilleco/lib
|
|
6
6
|
Author: Alex Poloz
|
|
@@ -41,6 +41,7 @@ Data Format | ` libdev.num ` | Numeric conversions & handlers
|
|
|
41
41
|
Transforms | ` libdev.gen ` | Code & token generators
|
|
42
42
|
| ` libdev.codes ` | Ciphers: langs & flags / networks / user statuses
|
|
43
43
|
| ` libdev.check ` | Validation functions
|
|
44
|
+
| ` libdev.crypt ` | Encryption and decryption functions
|
|
44
45
|
Fields | ` libdev.dev ` | Development tools
|
|
45
46
|
| ` libdev.fin ` | Financial codes and tools
|
|
46
47
|
| ` libdev.lang ` | Natural language formatters
|
|
@@ -15,6 +15,7 @@ Data Format | ` libdev.num ` | Numeric conversions & handlers
|
|
|
15
15
|
Transforms | ` libdev.gen ` | Code & token generators
|
|
16
16
|
| ` libdev.codes ` | Ciphers: langs & flags / networks / user statuses
|
|
17
17
|
| ` libdev.check ` | Validation functions
|
|
18
|
+
| ` libdev.crypt ` | Encryption and decryption functions
|
|
18
19
|
Fields | ` libdev.dev ` | Development tools
|
|
19
20
|
| ` libdev.fin ` | Financial codes and tools
|
|
20
21
|
| ` libdev.lang ` | Natural language formatters
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Provides functions for encrypting and decrypting integers
|
|
3
|
+
using a custom base encoding scheme
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import random
|
|
7
|
+
import string
|
|
8
|
+
|
|
9
|
+
# '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
10
|
+
ALPHABET = string.digits + string.ascii_letters
|
|
11
|
+
BASE = len(ALPHABET)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _int_to_custom_base(number):
|
|
15
|
+
"""
|
|
16
|
+
Convert an integer to a custom base string.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
number (int): The integer to convert.
|
|
20
|
+
|
|
21
|
+
Returns:
|
|
22
|
+
str: The custom base string representation of the integer.
|
|
23
|
+
"""
|
|
24
|
+
if number == 0:
|
|
25
|
+
return ALPHABET[0]
|
|
26
|
+
result = ""
|
|
27
|
+
while number > 0:
|
|
28
|
+
result = ALPHABET[number % BASE] + result
|
|
29
|
+
number //= BASE
|
|
30
|
+
return result
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _custom_base_to_int(encoded_str):
|
|
34
|
+
"""
|
|
35
|
+
Convert a custom base string to an integer.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
encoded_str (str): The custom base string to convert.
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
int: The integer representation of the custom base string.
|
|
42
|
+
"""
|
|
43
|
+
number = 0
|
|
44
|
+
for char in encoded_str:
|
|
45
|
+
number = number * BASE + ALPHABET.index(char)
|
|
46
|
+
return number
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _get_checksum(encoded_str):
|
|
50
|
+
"""
|
|
51
|
+
Calculate the checksum for a custom base string.
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
encoded_str (str): The custom base string.
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
str: The checksum character.
|
|
58
|
+
"""
|
|
59
|
+
checksum_value = sum(ALPHABET.index(char) for char in encoded_str) % BASE
|
|
60
|
+
return ALPHABET[checksum_value]
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def encrypt(number, length=5):
|
|
64
|
+
"""
|
|
65
|
+
Encrypt an integer by converting it to a custom base string,
|
|
66
|
+
adding a checksum, and padding with random characters.
|
|
67
|
+
|
|
68
|
+
Args:
|
|
69
|
+
number (int): The integer to encrypt.
|
|
70
|
+
length (int): The desired length of the encrypted string.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
str: The encrypted string.
|
|
74
|
+
"""
|
|
75
|
+
if number is None:
|
|
76
|
+
return None
|
|
77
|
+
# Convert the number to custom base
|
|
78
|
+
encoded_str = _int_to_custom_base(number)
|
|
79
|
+
# Calculate checksum and append to the end
|
|
80
|
+
checksum = _get_checksum(encoded_str)
|
|
81
|
+
if encoded_str != checksum:
|
|
82
|
+
encoded_str += checksum
|
|
83
|
+
# Add random characters to the beginning to meet the desired length
|
|
84
|
+
while len(encoded_str) < length:
|
|
85
|
+
random_char = random.choice(ALPHABET)
|
|
86
|
+
if _get_checksum(random_char + encoded_str[:-1]) != checksum:
|
|
87
|
+
encoded_str = random_char + encoded_str
|
|
88
|
+
return encoded_str
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def decrypt(encoded_str):
|
|
92
|
+
"""
|
|
93
|
+
Decrypt an encoded string back to its original integer form.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
encoded_str (str): The encoded string to decrypt.
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
int: The original integer.
|
|
100
|
+
|
|
101
|
+
Raises:
|
|
102
|
+
ValueError: If the encoded string is invalid.
|
|
103
|
+
"""
|
|
104
|
+
# Extract the checksum
|
|
105
|
+
checksum = encoded_str[-1]
|
|
106
|
+
encoded_str = encoded_str[:-1]
|
|
107
|
+
# Find the actual encoded string by removing the random characters
|
|
108
|
+
for i in range(len(encoded_str)):
|
|
109
|
+
candidate = encoded_str[i:]
|
|
110
|
+
if _get_checksum(candidate) == checksum:
|
|
111
|
+
return _custom_base_to_int(candidate)
|
|
112
|
+
return _custom_base_to_int(checksum)
|
|
@@ -54,6 +54,11 @@ def get_time(data=None, template="%d.%m.%Y %H:%M:%S", tz=0):
|
|
|
54
54
|
return time.strftime(template, time.gmtime(data + tz * 3600))
|
|
55
55
|
|
|
56
56
|
|
|
57
|
+
def get_date(data=None, template="%d.%m.%Y", tz=0):
|
|
58
|
+
"""Get date from timestamp"""
|
|
59
|
+
return get_time(data, template, tz)
|
|
60
|
+
|
|
61
|
+
|
|
57
62
|
def decode_time(data=None, template="%d.%m.%Y %H:%M:%S", tz=0):
|
|
58
63
|
"""Get timestamp from time"""
|
|
59
64
|
|
|
@@ -72,6 +77,11 @@ def decode_time(data=None, template="%d.%m.%Y %H:%M:%S", tz=0):
|
|
|
72
77
|
return int(data.timestamp())
|
|
73
78
|
|
|
74
79
|
|
|
80
|
+
def decode_date(data=None, template="%d.%m.%Y", tz=0):
|
|
81
|
+
"""Get timestamp from date"""
|
|
82
|
+
return decode_time(data, template, tz)
|
|
83
|
+
|
|
84
|
+
|
|
75
85
|
# pylint: disable=too-many-branches,too-many-statements
|
|
76
86
|
def parse_time(data: str, tz=0):
|
|
77
87
|
"""Parse time"""
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: libdev
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.85
|
|
4
4
|
Summary: Set of standard functions for development
|
|
5
5
|
Home-page: https://github.com/chilleco/lib
|
|
6
6
|
Author: Alex Poloz
|
|
@@ -41,6 +41,7 @@ Data Format | ` libdev.num ` | Numeric conversions & handlers
|
|
|
41
41
|
Transforms | ` libdev.gen ` | Code & token generators
|
|
42
42
|
| ` libdev.codes ` | Ciphers: langs & flags / networks / user statuses
|
|
43
43
|
| ` libdev.check ` | Validation functions
|
|
44
|
+
| ` libdev.crypt ` | Encryption and decryption functions
|
|
44
45
|
Fields | ` libdev.dev ` | Development tools
|
|
45
46
|
| ` libdev.fin ` | Financial codes and tools
|
|
46
47
|
| ` libdev.lang ` | Natural language formatters
|
|
@@ -5,6 +5,7 @@ libdev/__init__.py
|
|
|
5
5
|
libdev/cfg.py
|
|
6
6
|
libdev/check.py
|
|
7
7
|
libdev/codes.py
|
|
8
|
+
libdev/crypt.py
|
|
8
9
|
libdev/dev.py
|
|
9
10
|
libdev/doc.py
|
|
10
11
|
libdev/fin.py
|
|
@@ -24,6 +25,7 @@ libdev.egg-info/top_level.txt
|
|
|
24
25
|
tests/test_cfg.py
|
|
25
26
|
tests/test_check.py
|
|
26
27
|
tests/test_codes.py
|
|
28
|
+
tests/test_crypt.py
|
|
27
29
|
tests/test_dev.py
|
|
28
30
|
tests/test_doc.py
|
|
29
31
|
tests/test_gen.py
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from libdev.crypt import encrypt, decrypt
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def test_cfg_json():
|
|
5
|
+
assert encrypt(None) == None
|
|
6
|
+
assert encrypt(0)[-1:] == "0"
|
|
7
|
+
assert encrypt(0, length=0) == "0"
|
|
8
|
+
assert encrypt(0, length=10)[-1:] == "0"
|
|
9
|
+
assert decrypt(encrypt(0, length=15)) == 0
|
|
10
|
+
assert decrypt(encrypt(123, length=15)) == 123
|
|
11
|
+
for i in range(0, 1000):
|
|
12
|
+
assert decrypt(encrypt(i)) == i
|
|
@@ -4,7 +4,14 @@ https://www.epochconverter.com/
|
|
|
4
4
|
|
|
5
5
|
import datetime
|
|
6
6
|
|
|
7
|
-
from libdev.time import
|
|
7
|
+
from libdev.time import (
|
|
8
|
+
get_time,
|
|
9
|
+
get_date,
|
|
10
|
+
decode_time,
|
|
11
|
+
decode_date,
|
|
12
|
+
parse_time,
|
|
13
|
+
format_delta,
|
|
14
|
+
)
|
|
8
15
|
|
|
9
16
|
|
|
10
17
|
def test_get_time():
|
|
@@ -22,6 +29,10 @@ def test_get_time():
|
|
|
22
29
|
)
|
|
23
30
|
|
|
24
31
|
|
|
32
|
+
def test_get_date():
|
|
33
|
+
assert get_date(1641061152.4) == "01.01.2022"
|
|
34
|
+
|
|
35
|
+
|
|
25
36
|
def test_decode_time():
|
|
26
37
|
assert decode_time("") == None
|
|
27
38
|
assert decode_time("01.01.2022 21:19:12", tz=3) == 1641061152
|
|
@@ -29,6 +40,10 @@ def test_decode_time():
|
|
|
29
40
|
assert decode_time("2024-10-07", "%Y-%m-%d", 4) == 1728244800
|
|
30
41
|
|
|
31
42
|
|
|
43
|
+
def test_decode_date():
|
|
44
|
+
assert decode_date("01.01.2022", tz=3) == 1640984400
|
|
45
|
+
|
|
46
|
+
|
|
32
47
|
def test_parse_time():
|
|
33
48
|
assert parse_time("07.10.1998", tz=3) == 907707600
|
|
34
49
|
assert parse_time("7.10.1998 7:00:00") == 907743600
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|