really-good-things 0.1.0__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.
- really_good_things-0.1.0/LICENSE +21 -0
- really_good_things-0.1.0/PKG-INFO +26 -0
- really_good_things-0.1.0/README.md +2 -0
- really_good_things-0.1.0/pyproject.toml +37 -0
- really_good_things-0.1.0/really_good_things.egg-info/PKG-INFO +26 -0
- really_good_things-0.1.0/really_good_things.egg-info/SOURCES.txt +14 -0
- really_good_things-0.1.0/really_good_things.egg-info/dependency_links.txt +1 -0
- really_good_things-0.1.0/really_good_things.egg-info/requires.txt +8 -0
- really_good_things-0.1.0/really_good_things.egg-info/top_level.txt +2 -0
- really_good_things-0.1.0/really_good_things.egg-info/zip-safe +1 -0
- really_good_things-0.1.0/rgt/__init__.py +0 -0
- really_good_things-0.1.0/rgt/security/__init__.py +5 -0
- really_good_things-0.1.0/rgt/security/cipher_utils.py +83 -0
- really_good_things-0.1.0/rgt/security/hash_utils.py +11 -0
- really_good_things-0.1.0/rgt/security/jwt_utils.py +67 -0
- really_good_things-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 J.Hu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: really-good-things
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Relly Good Things.
|
|
5
|
+
Author-email: "J.Hu" <joestarhu@163.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: homepage, https://github.com/joestarhu/rgt
|
|
8
|
+
Classifier: Environment :: Web Environment
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: MacOS
|
|
11
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
13
|
+
Requires-Python: >=3.14
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Requires-Dist: bcrypt>=5.0.0
|
|
17
|
+
Requires-Dist: pycryptodomex>=3.23.0
|
|
18
|
+
Requires-Dist: python-jose>=3.5.0
|
|
19
|
+
Provides-Extra: security
|
|
20
|
+
Requires-Dist: python-jose; extra == "security"
|
|
21
|
+
Requires-Dist: bcrypt; extra == "security"
|
|
22
|
+
Requires-Dist: pycryptodomex; extra == "security"
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# rgt
|
|
26
|
+
Really Good Things(RGT)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools >= 80.9.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "really-good-things"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Relly Good Things."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.14"
|
|
11
|
+
license = {text = "MIT"}
|
|
12
|
+
authors = [
|
|
13
|
+
{name = "J.Hu", email = "joestarhu@163.com"}
|
|
14
|
+
]
|
|
15
|
+
urls = {homepage = "https://github.com/joestarhu/rgt"} # GitHub 链接作为主页
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Environment :: Web Environment",
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Operating System :: MacOS",
|
|
20
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
21
|
+
"Programming Language :: Python :: 3.14"
|
|
22
|
+
]
|
|
23
|
+
dependencies = [
|
|
24
|
+
"bcrypt>=5.0.0",
|
|
25
|
+
"pycryptodomex>=3.23.0",
|
|
26
|
+
"python-jose>=3.5.0",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
[project.optional-dependencies]
|
|
31
|
+
security = ["python-jose", "bcrypt", "pycryptodomex"]
|
|
32
|
+
|
|
33
|
+
[tool.setuptools.packages.find]
|
|
34
|
+
exclude = ["test", "examples", "script", "tutorials"]
|
|
35
|
+
|
|
36
|
+
[tool.setuptools]
|
|
37
|
+
zip-safe = true
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: really-good-things
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Relly Good Things.
|
|
5
|
+
Author-email: "J.Hu" <joestarhu@163.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: homepage, https://github.com/joestarhu/rgt
|
|
8
|
+
Classifier: Environment :: Web Environment
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: MacOS
|
|
11
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
13
|
+
Requires-Python: >=3.14
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Requires-Dist: bcrypt>=5.0.0
|
|
17
|
+
Requires-Dist: pycryptodomex>=3.23.0
|
|
18
|
+
Requires-Dist: python-jose>=3.5.0
|
|
19
|
+
Provides-Extra: security
|
|
20
|
+
Requires-Dist: python-jose; extra == "security"
|
|
21
|
+
Requires-Dist: bcrypt; extra == "security"
|
|
22
|
+
Requires-Dist: pycryptodomex; extra == "security"
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# rgt
|
|
26
|
+
Really Good Things(RGT)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
really_good_things.egg-info/PKG-INFO
|
|
5
|
+
really_good_things.egg-info/SOURCES.txt
|
|
6
|
+
really_good_things.egg-info/dependency_links.txt
|
|
7
|
+
really_good_things.egg-info/requires.txt
|
|
8
|
+
really_good_things.egg-info/top_level.txt
|
|
9
|
+
really_good_things.egg-info/zip-safe
|
|
10
|
+
rgt/__init__.py
|
|
11
|
+
rgt/security/__init__.py
|
|
12
|
+
rgt/security/cipher_utils.py
|
|
13
|
+
rgt/security/hash_utils.py
|
|
14
|
+
rgt/security/jwt_utils.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
File without changes
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
from base64 import b64decode, b64encode
|
|
2
|
+
from Cryptodome.Cipher import AES
|
|
3
|
+
from Cryptodome.Util.Padding import pad, unpad
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class AESECBManager:
|
|
7
|
+
def __init__(self, key: str) -> None:
|
|
8
|
+
"""
|
|
9
|
+
Args:
|
|
10
|
+
key(str):秘钥,长度必须为 16,24,32
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
# ECB模式,加密结果固定
|
|
14
|
+
self._cipher = AES.new(key.encode(), AES.MODE_ECB)
|
|
15
|
+
|
|
16
|
+
@property
|
|
17
|
+
def cipher(self):
|
|
18
|
+
return self._cipher
|
|
19
|
+
|
|
20
|
+
def encrypt(self, plain_text: str) -> str:
|
|
21
|
+
"""加密:pad->encrypt->b64encode
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
# 对明文进行补全至块大小的填充
|
|
25
|
+
padded_text = pad(plain_text.encode(), AES.block_size)
|
|
26
|
+
|
|
27
|
+
# 加密
|
|
28
|
+
ciphertext = self.cipher.encrypt(padded_text)
|
|
29
|
+
|
|
30
|
+
# 返回Base64编码的密文
|
|
31
|
+
return b64encode(ciphertext).decode()
|
|
32
|
+
|
|
33
|
+
def decrypt(self, encrypted_value: str) -> str:
|
|
34
|
+
"""解密:b64decode->decrypt->unpad
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
# 将Base64编码的密文解码为字节
|
|
38
|
+
ciphertext = b64decode(encrypted_value)
|
|
39
|
+
|
|
40
|
+
# 解密
|
|
41
|
+
decrypted_text = unpad(self.cipher.decrypt(ciphertext), AES.block_size)
|
|
42
|
+
|
|
43
|
+
# 将解密后的字节串转换回字符串
|
|
44
|
+
return decrypted_text.decode()
|
|
45
|
+
|
|
46
|
+
def phone_encrypt(self, plain_text: str) -> str:
|
|
47
|
+
"""加密手机号;手机号按照每3位组成一段密文,然后拼接而成;
|
|
48
|
+
如:18012345678,分为 180,801,012,123,......678;
|
|
49
|
+
使用AES ECB模式加密,保障同样的明文输出同样的密文,用于手机号的模糊匹配
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
plain_text:手机号明文,例如:18012345678
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
str:加密后的手机号
|
|
56
|
+
"""
|
|
57
|
+
if (length := len(plain_text)) == 0:
|
|
58
|
+
return ""
|
|
59
|
+
|
|
60
|
+
end_pos = max(length-3, 0)
|
|
61
|
+
return ",".join([self.encrypt(plain_text[i:i+3]) for i in range(end_pos+1)])
|
|
62
|
+
|
|
63
|
+
def phone_decrypt(self, encrypted_text: str, mask: bool = True) -> str:
|
|
64
|
+
"""解密手机号
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
encrypted_text:加密的手机号
|
|
68
|
+
mask: 是否脱敏显示,比如180****5678
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
str:手机号明文
|
|
72
|
+
"""
|
|
73
|
+
if not encrypted_text:
|
|
74
|
+
return ""
|
|
75
|
+
|
|
76
|
+
phone_array = [self.decrypt(v) for v in encrypted_text.split(",")]
|
|
77
|
+
phone = "".join([phone_array[i][0]
|
|
78
|
+
for i in range(8)]) + phone_array[-1]
|
|
79
|
+
|
|
80
|
+
if mask:
|
|
81
|
+
phone = f"{phone[:3]}****{phone[7:]}"
|
|
82
|
+
|
|
83
|
+
return phone
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from bcrypt import hashpw, checkpw, gensalt
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class HashUtils:
|
|
5
|
+
@staticmethod
|
|
6
|
+
def hash(plain: str, rounds: int = 12) -> str:
|
|
7
|
+
return hashpw(plain.encode(), gensalt(rounds=rounds)).decode()
|
|
8
|
+
|
|
9
|
+
@staticmethod
|
|
10
|
+
def verify(plain: str, hashed: str) -> bool:
|
|
11
|
+
return checkpw(plain.encode(), hashed.encode())
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
from datetime import datetime, timedelta, timezone
|
|
2
|
+
from typing import Dict, Any
|
|
3
|
+
from uuid import uuid4
|
|
4
|
+
from jose import jwt
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class JWTManager:
|
|
8
|
+
def __init__(self, key: str, exp_min: float, algorithm: str = "HS256") -> None:
|
|
9
|
+
"""
|
|
10
|
+
实例化JWT的管理类
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
key(str):jwt加密用的秘钥,可通过openssl rand -base 32 生成
|
|
14
|
+
exp_min(float): jwt的过期时间(分钟)
|
|
15
|
+
algorithm(str): jwt加密算法,默认"HS256"
|
|
16
|
+
"""
|
|
17
|
+
self._key = key
|
|
18
|
+
self._exp_min = exp_min
|
|
19
|
+
self._algorithm = algorithm
|
|
20
|
+
|
|
21
|
+
@property
|
|
22
|
+
def key(self) -> str:
|
|
23
|
+
return self._key
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def exp_min(self) -> float:
|
|
27
|
+
return self._exp_min
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def algorithm(self) -> str:
|
|
31
|
+
return self._algorithm
|
|
32
|
+
|
|
33
|
+
def encode(self, **kw) -> str:
|
|
34
|
+
"""
|
|
35
|
+
JWT编码, 自动设置jti,iat,exp参数
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
**kw:payload参数
|
|
39
|
+
"""
|
|
40
|
+
iat = datetime.now(tz=timezone.utc)
|
|
41
|
+
exp = iat + timedelta(minutes=self.exp_min)
|
|
42
|
+
|
|
43
|
+
payload = kw | {
|
|
44
|
+
"jti": uuid4().hex,
|
|
45
|
+
"iat": iat,
|
|
46
|
+
"exp": exp,
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return jwt.encode(
|
|
50
|
+
claims=payload,
|
|
51
|
+
key=self.key,
|
|
52
|
+
algorithm=self.algorithm
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
def decode(self, token: str) -> Dict[str, Any]:
|
|
56
|
+
"""
|
|
57
|
+
JWT解码
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
token(str):json web token字符串
|
|
61
|
+
|
|
62
|
+
"""
|
|
63
|
+
return jwt.decode(
|
|
64
|
+
token=token,
|
|
65
|
+
key=self.key,
|
|
66
|
+
algorithms=self.algorithm
|
|
67
|
+
)
|