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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: ph-utils
3
- Version: 0.2.1
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.1"
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 hashlib
9
- import base64
10
-
11
-
12
- def hash(d: str, method="sha1", upper=False, result="hex"):
13
- """进行 md5、sha1、sha256 数据摘要签名
14
-
15
- Args:
16
- d (str): 待加密的数据
17
- method (str, optional): 签名算法, md5、sha1、sha256. Defaults to "md5".
18
- upper (bool, optional): 返回的结果是否需要大写. Defaults to False.
19
- result (str, optional): 返回数据, hex 转换为 HEX 返回
20
-
21
- Raises:
22
- Exception: method只允许为 md5、sha1中的一个
23
-
24
- Returns:
25
- str: 加密后的结果
26
- """
27
- if method in ("md5", "sha1", "sha256"):
28
- func = getattr(hashlib, method)
29
- althorithm = func()
30
- althorithm.update(d.encode("utf-8"))
31
- if result == "hex":
32
- rd = althorithm.hexdigest()
33
- return rd if upper is False else rd.upper()
34
- else:
35
- return althorithm.digest()
36
- else:
37
- raise Exception("unsupport_althorithm_method")
38
-
39
-
40
- def b64encode(data, result="str"):
41
- """base64加密
42
-
43
- Args:
44
- data (str): 待base64加密数据
45
- result (str, optional): 加密返回结果, str、bytes. Defaults to "str".
46
-
47
- Returns:
48
- str|bytes: base64加密后的数据
49
- """
50
- if not data:
51
- return None
52
- encode_data = data
53
- if isinstance(encode_data, str):
54
- encode_data = encode_data.encode()
55
- encoded = base64.b64encode(encode_data)
56
- return encoded.decode() if result == "str" else encoded
57
-
58
-
59
- def b64decode(data, result="str"):
60
- """base64解密
61
-
62
- Args:
63
- data (str|bytes): base64加密后的数据
64
- result (str, optional): str、bytes. Defaults to "str".
65
-
66
- Returns:
67
- str|bytes: base64解密后的数据
68
- """
69
- if not data:
70
- return None
71
- decoded = base64.b64decode(data)
72
- return decoded.decode() if result == "str" else decoded
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