Skip to content

Commit a497ee0

Browse files
authored
update fns and docs (#30)
* feat: new hash func * update docs
1 parent 3f8115f commit a497ee0

4 files changed

Lines changed: 24 additions & 8 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
[![License](https://img.shields.io/github/license/LabNow-ai/aloha-python)](https://github.com/LabNow-ai/aloha-python/blob/main/LICENSE)
88
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/LabNow-ai/aloha-python/build.yml?branch=main)](https://github.com/LabNow-ai/aloha-python/actions)
9-
[![Join the Gitter Chat](https://img.shields.io/gitter/room/nwjs/nw.js.svg)](https://gitter.im/LabNow-ai/)
109
[![PyPI version](https://img.shields.io/pypi/v/aloha)](https://pypi.python.org/pypi/aloha/)
1110
[![PyPI Downloads](https://img.shields.io/pypi/dm/aloha)](https://pepy.tech/badge/aloha/)
1211
[![Code Activity](https://img.shields.io/github/commit-activity/m/LabNow-ai/aloha-python)](https://github.com/LabNow-ai/aloha-python/pulse)
@@ -15,7 +14,10 @@
1514
Please generously STAR★ our project or donate to us!
1615
[![GitHub Starts](https://img.shields.io/github/stars/LabNow-ai/aloha-python.svg?label=Stars&style=social)](https://github.com/LabNow-ai/aloha-python/stargazers)
1716

18-
- To contribute or talk to a human: [![Open an Issue on GitHub](https://img.shields.io/github/issues/LabNow-ai/aloha-python)](https://github.com/LabNow-ai/aloha-python/issues) [![Join the Discord Chat](https://img.shields.io/badge/Discuss_on-Discord-green)](https://discord.gg/kHUzgQxgbJ)
17+
- To understand the package, read the [📚docs](https://aloha-python.readthedocs.io/) or [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/LabNow-ai/lab-foundation)
18+
19+
- To contribute or talk to a human: [![Open an Issue on GitHub](https://img.shields.io/github/issues/LabNow-ai/aloha-python)](https://github.com/LabNow-ai/aloha-python/issues) [![Join the Discord Chat](https://img.shields.io/badge/Discuss_on-Discord-green)](https://discord.gg/kHUzgQxgbJ) [![Join the Gitter Chat](https://img.shields.io/gitter/room/nwjs/nw.js.svg)](https://gitter.im/LabNow-ai/)
20+
1921

2022
## Getting started
2123

src/aloha/encrypt/hash.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,18 @@ def hash_obj(obj):
2424
"""Hash an arbitrary JSON-serializable object."""
2525
s = json.dumps(obj, sort_keys=True, ensure_ascii=False, default=str)
2626
return hashlib.md5(s.encode()).hexdigest()
27+
28+
29+
def hash_base62(s: str, length: int = 6) -> str:
30+
"""Return a Base62-encoded hash of a string with specified length."""
31+
assert length > 0 and length <= 11, "Length must be between 1 and 11 for Base62 encoding."
32+
33+
CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
34+
digest = hashlib.sha256(s.encode()).digest() # 32 bytes = 256 bits
35+
# Convert the digest to an integer and then to Base62
36+
num = int.from_bytes(digest, "big")
37+
result = []
38+
for _ in range(length):
39+
result.append(CHARS[num % 62])
40+
num //= 62
41+
return "".join(reversed(result))

src/aloha/encrypt/jwt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
from ..logger import LOG
66

7-
LOG.debug("Using pyjwt == %s" % jwt.__version__.__str__())
7+
LOG.debug("Using pyjwt == %s" % str(jwt.__version__))
88

99

10-
def encode(secret_key: str, payload: dict, headers: dict = None, **kwargs):
10+
def encode(secret_key: str, payload: dict, headers: dict | None = None, **kwargs):
1111
"""Encode a payload into a JWT token."""
1212
token = jwt.encode(payload=payload, key=secret_key, headers=headers, **kwargs)
1313
return token

src/aloha/encrypt/rsa.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ def main():
176176
y_dec = rsa_dec.decrypt_with_private_key(y_bin, key_private=key_pri)
177177
y_txt = y_dec.decode("UTF-8")
178178

179-
print(
180-
"[test {i_case} success = {status}] {src} -> {enc}".format(
181-
i_case=i, status=(y_txt == str_src), src=y_txt, enc=x_txt
182-
)
179+
msg = "[test {i_case} success = {status}] {src} -> {enc}".format(
180+
i_case=i, status=(y_txt == str_src), src=y_txt, enc=x_txt
183181
)
182+
print(msg)

0 commit comments

Comments
 (0)