Skip to content

Self-describing base encodings — Multibase encoder/decoder

License

filip26/copper-multibase

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Copper Multibase

Multibase is a self-describing format for representing binary data as text. Each multibase-encoded string begins with a single character that uniquely identifies the base encoding (e.g., z for Base58BTC, m for Base64).

This design allows encoded data to carry the information needed for correct decoding, ensuring clarity, interoperability, and extensibility across systems.

Java 8 CI javadoc Maven Central License

✨ Features

  • static registry
    • no lookups when encoding
    • direct static access to an encoder
    • configurable set of encodings to support when decoding
  • built-in bases
    • Base2
    • Base16 (lower|upper)
    • Base32 (lower|upper, [no-]padding)
    • Base32Hex (lower|upper, [no-]padding)
    • Base58BTC
    • Base64 ([no-]padding)
    • Base64URL ([no-]padding)
  • no 3rd party dependencies
  • easily extendable

💡 Examples

/* encode an input with Base58BTC */
String encoded = Multibase.BASE_58_BTC.encode(byte[]);

/* get decoder instance initialized with all supported bases */
var decoder = MultibaseDecoder.getInstance();

/* get decoder initialized with custom base(s) */
var decoder = MultibaseDecoder.getInstance(mybase, ...);

/* decode */
byte[] decoded = decoder.decode(encoded);

/* or check if base is supported  */
byte[] decoded = decoder.getBase(encoded)
                        .map(base -> base.decode(encoded))
                        .orElseThrow(() -> new IllegalArgumentException("Unsupported base."));

/* or directy when only one base is supported */
byte[] decoded = Multibase.BASE_58_BTC.decode(encoded);

/* check if encoded with a base */
if (Multibase.BASE_58_BTC.isEncoded(encoded)) {
  ...
}

/* a cutom base implementation */
var mybase = new Multibase(
                     name,     // the unique base name (e.g., "base64urlpad")
                     prefix,   // the unique prefix character indicating the base
                     length,   // the base alphabet length
                     string -> byte[], // the decoding function
                     byte[] -> string  // the encoding function
                     );

/* encode with a custom base */
String encoded = mybase.encode(byte[]);

/* directly decode with a custom base */
byte[] decoded = mybase.decode(encoded);

📦 Installation

Maven

To include Copper Multibase in your project, add the following dependency to your pom.xml:

<dependency>
    <groupId>com.apicatalog</groupId>
    <artifactId>copper-multibase</artifactId>
    <version>4.1.0</version>
</dependency>

🛠️ LD-CLI

LD-CLI is a command-line utility for working with multiformats including multibase, multicodec, and multihash, as well as JSON-LD and related specifications.

It provides encoding, decoding, detection, analysis, and format conversion features, making it useful for inspecting identifiers, testing content addressing, and integrating multiformats into development workflows.

Example

Detect and analyze a multibase + multicodec value

> ld-cli multicodec --analyze --multibase <<< 'z6MkmM42vxfqZQsv4ehtTjFFxQ4sQKS2w6WR7emozFAn5cxu'

Multibase:  name=base58btc, prefix=z, length=58 chars
Multicodec: name=ed25519-pub, code=237, varint=[0xED,0x01], tag=Key, status=Draft
Length:     32 bytes

🤝 Contributing

Contributions are welcome! Please submit a pull request.

Building

Fork and clone the repository, then build with Maven:

> cd copper-multibase
> mvn package

📚 Resources

💼 Commercial Support

Commercial support and consulting are available.
For inquiries, please contact: [email protected]