add the ulibs
This commit is contained in:
1 parent
150708564c
commit
f3982221a1
2 files changed
+85
No files matched your search
@@ -0,0 +1,55 @@
|
|||||||
|
class Base64:
|
||||||
|
def __init__(self):
|
||||||
|
# Base64 alphabet
|
||||||
|
self.alphabet = (
|
||||||
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||||
|
)
|
||||||
|
|
||||||
|
def encode(self, data: str) -> str:
|
||||||
|
# Convert string to bytes and get binary representation
|
||||||
|
binary = "".join([format(b, "08b") for b in data.encode()])
|
||||||
|
|
||||||
|
# Pad with zeros if necessary
|
||||||
|
padding = len(binary) % 6
|
||||||
|
if padding:
|
||||||
|
binary += "0" * (6 - padding)
|
||||||
|
|
||||||
|
# Split into 6-bit chunks and convert to decimal
|
||||||
|
chunks = [binary[i : i + 6] for i in range(0, len(binary), 6)]
|
||||||
|
result = "".join(self.alphabet[int(chunk, 2)] for chunk in chunks)
|
||||||
|
|
||||||
|
# Add padding characters
|
||||||
|
padding = len(data) % 3
|
||||||
|
if padding:
|
||||||
|
result += "=" * (3 - padding)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
def decode(self, data: str) -> str:
|
||||||
|
# Remove padding characters
|
||||||
|
data = data.rstrip("=")
|
||||||
|
|
||||||
|
# Convert to binary
|
||||||
|
binary = "".join([format(self.alphabet.index(c), "06b") for c in data])
|
||||||
|
|
||||||
|
# Remove padding zeros
|
||||||
|
padding = len(binary) % 8
|
||||||
|
if padding:
|
||||||
|
binary = binary[:-padding]
|
||||||
|
|
||||||
|
# Convert binary to bytes and then to string
|
||||||
|
bytes_data = [int(binary[i : i + 8], 2) for i in range(0, len(binary), 8)]
|
||||||
|
return bytes(bytes_data).decode()
|
||||||
|
|
||||||
|
|
||||||
|
# Example usage
|
||||||
|
def example():
|
||||||
|
b64 = Base64()
|
||||||
|
original = "Hello, World!"
|
||||||
|
encoded = b64.encode(original)
|
||||||
|
print(f"Original: {original}")
|
||||||
|
print(f"Encoded: {encoded}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
example()
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import secrets
|
||||||
|
|
||||||
|
|
||||||
|
class MicroUUID:
|
||||||
|
def __init__(self):
|
||||||
|
self.uuid = self.generate_uuid()
|
||||||
|
|
||||||
|
def generate_uuid(self):
|
||||||
|
# Generate a random UUID (version 4)
|
||||||
|
random_bytes = secrets.token_bytes(16)
|
||||||
|
# Set the version to 4 (randomly generated UUID)
|
||||||
|
random_bytes = bytearray(random_bytes)
|
||||||
|
random_bytes[6] = (random_bytes[6] & 0x0F) | 0x40
|
||||||
|
# Set the variant to RFC 4122
|
||||||
|
random_bytes[8] = (random_bytes[8] & 0x3F) | 0x80
|
||||||
|
uid = "".join(f"{byte:02x}" for byte in random_bytes)
|
||||||
|
return f"{uid[0:8]}-{uid[8:12]}-{uid[12:16]}-{uid[16:20]}-{uid[20:32]}"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.uuid
|
||||||
|
|
||||||
|
|
||||||
|
# Example usage:
|
||||||
|
def example():
|
||||||
|
uuid_instance = MicroUUID()
|
||||||
|
print(f"Generated UUID: {uuid_instance}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
example()
|
||||||
Reference in new issue
Block a user