🛠️ToolsShed

Base58エンコーダー / デコーダー

テキストをBase58にエンコードしたり、Base58文字列をデコードします。ビットコインアドレスやIPFS CIDに使われます。

よくある質問

コード実装

import base58  # pip install base58

# Encode text to Base58
text = "Hello, World!"
encoded = base58.b58encode(text.encode()).decode()
print(encoded)  # JxF12TrwUP45BMd

# Decode Base58 back to text
decoded = base58.b58decode(encoded).decode()
print(decoded)  # Hello, World!

# Manual implementation without library
ALPHABET = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

def encode(data: bytes) -> str:
    num = int.from_bytes(data, "big")
    result = b""
    while num > 0:
        num, rem = divmod(num, 58)
        result = bytes([ALPHABET[rem]]) + result
    pad = len(data) - len(data.lstrip(b"\x00"))
    return (b"1" * pad + result).decode()

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.