ph-utils 0.2.1__tar.gz → 0.2.2__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.
- {ph_utils-0.2.1 → ph_utils-0.2.2}/PKG-INFO +7 -1
- {ph_utils-0.2.1 → ph_utils-0.2.2}/README.md +6 -0
- {ph_utils-0.2.1 → ph_utils-0.2.2}/pyproject.toml +2 -1
- {ph_utils-0.2.1 → ph_utils-0.2.2}/src/ph_utils/crypto.py +73 -72
- {ph_utils-0.2.1 → ph_utils-0.2.2}/src/ph_utils/__init__.py +0 -0
- {ph_utils-0.2.1 → ph_utils-0.2.2}/src/ph_utils/common.py +0 -0
- {ph_utils-0.2.1 → ph_utils-0.2.2}/src/ph_utils/config.py +0 -0
- {ph_utils-0.2.1 → ph_utils-0.2.2}/src/ph_utils/date.py +0 -0
- {ph_utils-0.2.1 → ph_utils-0.2.2}/src/ph_utils/id.py +0 -0
- {ph_utils-0.2.1 → ph_utils-0.2.2}/src/ph_utils/logger.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: ph-utils
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: The python3 tool classes
|
|
5
5
|
Author: Tenny
|
|
6
6
|
Author-email: Tenny <tenny.shu@foxmail.com>
|
|
@@ -36,3 +36,9 @@ pip install ph-utils
|
|
|
36
36
|
### 5. [config](https://gitee.com/towardly/ph-utils_py/wikis/config_en): Configure relevant tool classes
|
|
37
37
|
|
|
38
38
|
### 6. [id](https://gitee.com/towardly/ph-utils_py/wikis/id): 生成唯一 ID 工具类
|
|
39
|
+
|
|
40
|
+
## 运行测试
|
|
41
|
+
|
|
42
|
+
```shell
|
|
43
|
+
uv run python tests/crypto_utils.py
|
|
44
|
+
```
|
|
@@ -21,3 +21,9 @@ pip install ph-utils
|
|
|
21
21
|
### 5. [config](https://gitee.com/towardly/ph-utils_py/wikis/config_en): Configure relevant tool classes
|
|
22
22
|
|
|
23
23
|
### 6. [id](https://gitee.com/towardly/ph-utils_py/wikis/id): 生成唯一 ID 工具类
|
|
24
|
+
|
|
25
|
+
## 运行测试
|
|
26
|
+
|
|
27
|
+
```shell
|
|
28
|
+
uv run python tests/crypto_utils.py
|
|
29
|
+
```
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "ph-utils"
|
|
3
|
-
version = "0.2.
|
|
3
|
+
version = "0.2.2"
|
|
4
4
|
description = "The python3 tool classes"
|
|
5
5
|
authors = [
|
|
6
6
|
{ name = "Tenny", email = "tenny.shu@foxmail.com" },
|
|
@@ -30,4 +30,5 @@ build-backend = "uv_build"
|
|
|
30
30
|
[dependency-groups]
|
|
31
31
|
dev = [
|
|
32
32
|
"ruff>=0.14.10",
|
|
33
|
+
"pytest>=8.0",
|
|
33
34
|
]
|
|
@@ -1,72 +1,73 @@
|
|
|
1
|
-
# Copyright (c) [2023] [Tenny]
|
|
2
|
-
# [ph-utils] is licensed under Mulan PSL v2.
|
|
3
|
-
# You can use this software according to the terms and conditions of the Mulan PSL v2.
|
|
4
|
-
# You may obtain a copy of Mulan PSL v2 at:
|
|
5
|
-
# http://license.coscl.org.cn/MulanPSL2
|
|
6
|
-
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
7
|
-
# See the Mulan PSL v2 for more details.
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
def hash(d: str, method="sha1", upper=False, result="hex"):
|
|
13
|
-
"""
|
|
14
|
-
|
|
15
|
-
Args:
|
|
16
|
-
d (str): 待加密的数据
|
|
17
|
-
method (str, optional): 签名算法, md5、sha1、sha256
|
|
18
|
-
upper (bool, optional): 返回的结果是否需要大写. Defaults to False.
|
|
19
|
-
result (str, optional): 返回数据, hex 转换为 HEX 返回
|
|
20
|
-
|
|
21
|
-
Raises:
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
Returns:
|
|
25
|
-
str: 加密后的结果
|
|
26
|
-
"""
|
|
27
|
-
if
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
1
|
+
# Copyright (c) [2023] [Tenny]
|
|
2
|
+
# [ph-utils] is licensed under Mulan PSL v2.
|
|
3
|
+
# You can use this software according to the terms and conditions of the Mulan PSL v2.
|
|
4
|
+
# You may obtain a copy of Mulan PSL v2 at:
|
|
5
|
+
# http://license.coscl.org.cn/MulanPSL2
|
|
6
|
+
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
7
|
+
# See the Mulan PSL v2 for more details.
|
|
8
|
+
import base64
|
|
9
|
+
import hashlib
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def hash(d: str, method="sha1", upper=False, result="hex"):
|
|
13
|
+
"""使用 hashlib 支持的算法进行数据摘要签名
|
|
14
|
+
|
|
15
|
+
Args:
|
|
16
|
+
d (str): 待加密的数据
|
|
17
|
+
method (str, optional): 签名算法, 如 md5、sha1、sha256、sha512 等 hashlib 支持的算法. Defaults to "sha1".
|
|
18
|
+
upper (bool, optional): 返回的结果是否需要大写. Defaults to False.
|
|
19
|
+
result (str, optional): 返回数据, hex 转换为 HEX 返回
|
|
20
|
+
|
|
21
|
+
Raises:
|
|
22
|
+
ValueError: 不支持的签名算法
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
str: 加密后的结果
|
|
26
|
+
"""
|
|
27
|
+
if not hasattr(hashlib, method):
|
|
28
|
+
raise ValueError(
|
|
29
|
+
f"不支持的签名算法: {method}, 可用算法: {', '.join(hashlib.algorithms_guaranteed)}"
|
|
30
|
+
)
|
|
31
|
+
func = getattr(hashlib, method)
|
|
32
|
+
althorithm = func()
|
|
33
|
+
althorithm.update(d.encode("utf-8"))
|
|
34
|
+
if result == "hex":
|
|
35
|
+
rd = althorithm.hexdigest()
|
|
36
|
+
return rd if upper is False else rd.upper()
|
|
37
|
+
else:
|
|
38
|
+
return althorithm.digest()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def b64encode(data, result="str"):
|
|
42
|
+
"""base64加密
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
data (str): 待base64加密数据
|
|
46
|
+
result (str, optional): 加密返回结果, str、bytes. Defaults to "str".
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
str|bytes: base64加密后的数据
|
|
50
|
+
"""
|
|
51
|
+
if not data:
|
|
52
|
+
return None
|
|
53
|
+
encode_data = data
|
|
54
|
+
if isinstance(encode_data, str):
|
|
55
|
+
encode_data = encode_data.encode()
|
|
56
|
+
encoded = base64.b64encode(encode_data)
|
|
57
|
+
return encoded.decode() if result == "str" else encoded
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def b64decode(data, result="str"):
|
|
61
|
+
"""base64解密
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
data (str|bytes): base64加密后的数据
|
|
65
|
+
result (str, optional): str、bytes. Defaults to "str".
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
str|bytes: base64解密后的数据
|
|
69
|
+
"""
|
|
70
|
+
if not data:
|
|
71
|
+
return None
|
|
72
|
+
decoded = base64.b64decode(data)
|
|
73
|
+
return decoded.decode() if result == "str" else decoded
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|