sync improve
This commit is contained in:
1 parent
01bb82fa5a
commit
2916e49973
52 files changed
+2171
-271
No files matched your search
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,51 @@
|
||||
from __future__ import annotations
|
||||
|
||||
try:
|
||||
# Prefer external package if available
|
||||
from base58 import b58encode, b58decode # type: ignore
|
||||
except Exception:
|
||||
# Minimal fallback (compatible subset)
|
||||
ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
||||
ALPHABET_INDEX = {c: i for i, c in enumerate(ALPHABET)}
|
||||
|
||||
def _to_bytes(value: bytes | bytearray | str) -> bytes:
|
||||
if isinstance(value, (bytes, bytearray)):
|
||||
return bytes(value)
|
||||
if isinstance(value, str):
|
||||
return value.encode()
|
||||
raise TypeError("value must be bytes or str")
|
||||
|
||||
def b58encode(data: bytes | bytearray | str) -> bytes:
|
||||
data = _to_bytes(data)
|
||||
if not data:
|
||||
return b""
|
||||
n = int.from_bytes(data, "big")
|
||||
out = []
|
||||
while n > 0:
|
||||
n, rem = divmod(n, 58)
|
||||
out.append(ALPHABET[rem])
|
||||
enc = "".join(reversed(out))
|
||||
leading = 0
|
||||
for b in data:
|
||||
if b == 0:
|
||||
leading += 1
|
||||
else:
|
||||
break
|
||||
return ("1" * leading + enc).encode()
|
||||
|
||||
def b58decode(data: bytes | bytearray | str) -> bytes:
|
||||
data_b = _to_bytes(data)
|
||||
if not data_b:
|
||||
return b""
|
||||
num = 0
|
||||
for ch in data_b.decode():
|
||||
num = num * 58 + ALPHABET_INDEX[ch]
|
||||
full = num.to_bytes((num.bit_length() + 7) // 8, "big")
|
||||
leading = 0
|
||||
for ch in data_b:
|
||||
if ch == ord('1'):
|
||||
leading += 1
|
||||
else:
|
||||
break
|
||||
return b"\x00" * leading + full
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
from typing import Iterable
|
||||
|
||||
|
||||
def _to_bytes(data: Iterable[int] | bytes | bytearray | str) -> bytes:
|
||||
if isinstance(data, (bytes, bytearray)):
|
||||
return bytes(data)
|
||||
if isinstance(data, str):
|
||||
return data.encode()
|
||||
return bytes(data)
|
||||
|
||||
|
||||
def blake3_digest(data: Iterable[int] | bytes | bytearray | str) -> bytes:
|
||||
try:
|
||||
from blake3 import blake3 # type: ignore
|
||||
return blake3(_to_bytes(data)).digest()
|
||||
except Exception:
|
||||
return hashlib.blake2s(_to_bytes(data)).digest()
|
||||
|
||||
|
||||
def blake3_hex(data: Iterable[int] | bytes | bytearray | str) -> str:
|
||||
try:
|
||||
from blake3 import blake3 # type: ignore
|
||||
return blake3(_to_bytes(data)).hexdigest()
|
||||
except Exception:
|
||||
return hashlib.blake2s(_to_bytes(data)).hexdigest()
|
||||
|
||||
Reference in new issue
Block a user