| Crates.io | b58 |
| lib.rs | b58 |
| version | 0.1.2 |
| created_at | 2025-07-07 04:11:08.04331+00 |
| updated_at | 2025-07-07 04:53:24.917213+00 |
| description | A Base58 encoding/decoding library with no external dependencies |
| homepage | |
| repository | https://github.com/cmackenzie1/base58-rs |
| max_upload_size | |
| id | 1740704 |
| size | 32,060 |
A fast, zero-dependency Base58 encoding and decoding library for Rust.
Add this to your Cargo.toml:
[dependencies]
b58 = "0.1"
Install the binary using cargo:
cargo install b58
use b58::{encode, decode};
// Encode bytes to Base58 (uses Bitcoin alphabet by default)
let data = b"Hello, World!";
let encoded = encode(data);
println!("Encoded: {}", encoded); // "72k1xXWG59fYdzSNoA"
// Decode Base58 string back to bytes
let decoded = decode(&encoded).unwrap();
assert_eq!(data, decoded.as_slice());
use b58::{encode_with_alphabet, decode_with_alphabet, Alphabet};
// Encode using Ripple alphabet
let data = b"Hello, World!";
let encoded = encode_with_alphabet(data, Alphabet::Ripple);
println!("Ripple encoded: {}", encoded);
// Decode using Ripple alphabet
let decoded = decode_with_alphabet(&encoded, Alphabet::Ripple).unwrap();
assert_eq!(data, decoded.as_slice());
// Encode using Flickr alphabet
let encoded_flickr = encode_with_alphabet(data, Alphabet::Flickr);
println!("Flickr encoded: {}", encoded_flickr);
use b58::{decode, DecodeError};
match decode("invalid0characters") {
Ok(data) => println!("Decoded: {:?}", data),
Err(DecodeError::InvalidCharacter(c)) => println!("Invalid character: {}", c),
Err(e) => println!("Error: {}", e),
}
The base58 binary provides a convenient command-line interface for encoding and decoding Base58 data, similar to the base64 command:
# Encode text to Base58 (default behavior)
printf "Hello, World!" | base58
# Output: 72k1xXWG59fYdzSNoA
# Decode Base58 back to original data
printf "72k1xXWG59fYdzSNoA" | base58 -d
# Output: Hello, World!
# Use different alphabets
printf "Hello, World!" | base58 --alphabet ripple
# Output: fpkrxXWGn9CYdzS4ow
printf "Hello, World!" | base58 --alphabet flickr
# Output: 72K1Xwvg59ExCZrnNa
# Decode with specific alphabet
printf "fpkrxXWGn9CYdzS4ow" | base58 -d --alphabet ripple
# Output: Hello, World!
# Encode/decode files
base58 < input.txt > encoded.txt
base58 -d < encoded.txt > output.txt
# Show help
base58 --help
-d, --decode - Decode Base58 input (default: encode)-a, --alphabet <ALPHABET> - Specify alphabet (bitcoin, ripple, flickr) [default: bitcoin]-h, --help - Show help informationLike the standard base64 command, base58 defaults to encoding mode when no flags are specified. This provides a clean, intuitive interface that follows Unix conventions.
encode(input: &[u8]) -> String - Encodes a byte slice to a Base58 string using Bitcoin alphabetdecode(input: &str) -> Result<Vec<u8>, DecodeError> - Decodes a Base58 string to bytes using Bitcoin alphabetencode_with_alphabet(input: &[u8], alphabet: Alphabet) -> String - Encodes using specified alphabetdecode_with_alphabet(input: &str, alphabet: Alphabet) -> Result<Vec<u8>, DecodeError> - Decodes using specified alphabetAlphabet::Bitcoin (default) - 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyzAlphabet::Ripple - rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyzAlphabet::Flickr - 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZDecodeError::InvalidCharacter(char) - Invalid character in Base58 stringDecodeError::EmptyInput - Empty input string (currently unused)DecodeError::Overflow - Numeric overflow during decodingThis library uses big integer arithmetic to handle arbitrarily large inputs without overflow. The implementation:
The library is optimized for correctness and clarity rather than raw speed. For most use cases, performance is more than adequate. The big integer arithmetic ensures no data loss for large inputs.
Run the test suite:
cargo test
The tests include:
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
no_std environments (std features used only for error trait implementations)Base58 was designed specifically for use in Bitcoin addresses to provide human-friendly encoding with several key benefits:
Yes! Base58 is inherently URL-safe because it only uses alphanumeric characters (no special characters like +, /, or =). This makes it ideal for:
Here's how Base58 compares to other common encodings:
| Encoding | Characters Used | Size Increase | URL-Safe |
|---|---|---|---|
| Hex | 16 | 100% | Yes |
| Base32 | 32 | 60% | Yes* |
| Base58 | 58 | ~38% | Yes |
| Base64 | 64 | 33% | No** |
*Base32 typically uses padding (=) which requires URL encoding **Base64 uses +, /, and = which require URL encoding
For example, encoding 32 random bytes:
While Base58 produces slightly larger output than Base64, it's completely URL-safe without any encoding, making it perfect for web applications, APIs, and anywhere human readability matters.