Author: [silene, 0x20CB, DionysianMYST]
License: APACHE 2.0 or MIT
SlugAPI is a module that provides a unified interface for encoding and decoding various formats such as Hex, Base32, Base58, and Base64 in a constant-time manner. It is designed to be efficient and secure, making it suitable for cryptographic applications.
SlugEncode is an open-source, swiss-army knife for encoding in constant-time for base32, base64, and hexadecimal, as well as encoding for base58. It uses traits to allow the conversion between encodings, as well as provides a builder struct that can be used to encode/decode data in a simple manner. Due to them being constant-time, excluding Base58, it may be more suitable for certain applications.
The traits can be implemented or the struct can be used to encode different data easily and more efficiently in constant-time with zeroize support. It includes support for:
-
Hexadecimal
-
Base32
-
Base32 (Padded)
-
Base64
-
Base64 (URL SAFE)
-
Base58
-
Base85
- Add Zeroize (Feature)
- Add Large Unsigned Intergers (Feature)
- Try Get Encoding (Feature)
- Integrity-Check (Feature)
- Expansion on Traits
In this example, we import the slugencode prelude to easily take care of encoding/decoding.
//! In this example, we import the slugencode prelude, take a hex string, and then decode using the "SlugEncodingUsage" struct as opposed to using traits like "TRAIT::SlugEncoder" or "TRAIT::SlugDecoder".
//!
//! It returns a vector of bytes (that are decoded hexadecimal).
//!
//! This can be applied to any encodings listed.
use slugencode::prelude::*;
fn main() -> Result<Vec<u8>,SlugEncodingErrors> {
let hex_str: &str = "e061930f8e7e02e1bef959646be6b18bc5991ff07a60771bb0b8f8f5";
// Using the SlugAPI
let slug_encoding = SlugEncodingUsage::new(SlugEncoding::Hex);
let output_bytes = slug_encoding.decode(hex_str)?;
return Ok(output_bytes)
}In this example, we use the traits slugencoder and slugdecoder to encode/decode data. It encodes data from bytes while it decodes from strings.
Once the trait is imported, it can be used right away.
// Import Traits
use slugencode::SlugEncoder;
use slugencode::SlugDecoder;
fn main() {
// Bytes
let x: Vec<u8> = [0x4C, 0x41, 0x53, 0x54];
// Hexadecimal (CT)
let hex_str: String = x.to_hex();
// Base32 (CT)
let base32_str: String = x.to_bs32();
let base32unpadded_str: String = x.to_bs32_unpadded();
// Base58 (Not-CT)
let base58: String = x.to_base58();
// Base64 (CT)
let base64: String = x.to_base64();
let base64_url_safe: String = x.to_base64_url();
}
fn decode() {
// Message (224)
let message_224: &str = "4b046cf780d891526710c8fdcab78e1f35c188fc278221f330bcaec2";
// Convert string from hex using trait
let hex_bytes = message_224.from_hex().expect("Decoding failed");
// BTC Address (Base58)
let btc_address: &str = "1FfmbHfnpaZjKFvyi1okTjJJusN455paPH";
// Convert string From Base58 using trait
let bytes = btc_address.from_base58().expect("Decoding failed");
}Contributions are welcome :)
APACHE-2.0 OR MIT
