Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ base-x = { version = "0.2.7", default-features = false }
base256emoji = "1.0.2"
data-encoding = { version = "2.3.1", default-features = false, features = ["alloc"] }
data-encoding-macro = "0.1.9"
base45 = "3.1.0"

[dev-dependencies]
criterion = "0.7"
Expand Down
2 changes: 2 additions & 0 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl fmt::Display for StrBase {
Base::Base32Z => "base32z",
Base::Base36Lower => "base36lower",
Base::Base36Upper => "base36upper",
Base::Base45 => "base45",
Base::Base58Flickr => "base58flickr",
Base::Base58Btc => "base58btc",
Base::Base64 => "base64",
Expand Down Expand Up @@ -118,6 +119,7 @@ impl FromStr for StrBase {
"base32z" => Ok(Base::Base32Z),
"base36lower" => Ok(Base::Base36Lower),
"base36upper" => Ok(Base::Base36Upper),
"base45" => Ok(Base::Base45),
"base58flickr" => Ok(Base::Base58Flickr),
"base58btc" => Ok(Base::Base58Btc),
"base64" => Ok(Base::Base64),
Expand Down
2 changes: 2 additions & 0 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ build_base_enum! {
'k' => Base36Lower,
/// Base36, [0-9A-Z] no padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ).
'K' => Base36Upper,
/// Base45, rfc9285 (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:).
'R' => Base45,
/// Base58 flicker (alphabet: 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ).
'Z' => Base58Flickr,
/// Base58 bitcoin (alphabet: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz).
Expand Down
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ impl From<data_encoding::DecodeError> for Error {
Self::InvalidBaseString
}
}

impl From<base45::DecodeError> for Error {
fn from(_: base45::DecodeError) -> Self {
Self::InvalidBaseString
}
}
14 changes: 14 additions & 0 deletions src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,17 @@ impl BaseCodec for Base36Upper {
Ok(base_x::decode(encoding::BASE36_UPPER, &uppercased)?)
}
}

/// Base45, rfc9285 (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:).
pub(crate) struct Base45;

impl BaseCodec for Base45 {
fn encode<I: AsRef<[u8]>>(input: I) -> String {
base45::encode(input.as_ref())
}

fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
let uppercased = input.as_ref().to_ascii_uppercase();
Ok(base45::decode(&uppercased)?)
}
}
4 changes: 4 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ fn test_basic() {
(Base32Z, "hxf1zgedpcfzg1ebb"),
(Base36Lower, "k2lcpzo5yikidynfl"),
(Base36Upper, "K2LCPZO5YIKIDYNFL"),
(Base45, "RRFF.OEB$D5/DZ24"),
(Base58Flickr, "Z7Pznk19XTTzBtx"),
(Base58Btc, "z7paNL19xttacUY"),
(Base64, "meWVzIG1hbmkgIQ"),
Expand Down Expand Up @@ -90,6 +91,7 @@ fn preserves_leading_zero() {
(Base32Z, "hybhskh3ypiosh4jyrr"),
(Base36Lower, "k02lcpzo5yikidynfl"),
(Base36Upper, "K02LCPZO5YIKIDYNFL"),
(Base45, "RV206$CL44CEC2DDX0"),
(Base58Flickr, "Z17Pznk19XTTzBtx"),
(Base58Btc, "z17paNL19xttacUY"),
(Base64, "mAHllcyBtYW5pICE"),
Expand Down Expand Up @@ -122,6 +124,7 @@ fn preserves_two_leading_zeroes() {
(Base32Z, "hyyy813murbssn5ujryoo"),
(Base36Lower, "k002lcpzo5yikidynfl"),
(Base36Upper, "K002LCPZO5YIKIDYNFL"),
(Base45, "R000RFF.OEB$D5/DZ24"),
(Base58Flickr, "Z117Pznk19XTTzBtx"),
(Base58Btc, "z117paNL19xttacUY"),
(Base64, "mAAB5ZXMgbWFuaSAh"),
Expand Down Expand Up @@ -149,6 +152,7 @@ fn case_insensitivity() {
(Base32HexPadUpper, "Td1imor3f41RMUSJCCG======"),
(Base36Lower, "kfUvrsIvVnfRbjWaJo"),
(Base36Upper, "KfUVrSIVVnFRbJWAJo"),
(Base45, "R+8d vd82ek4f.kea2"),
];
for (base, output) in test_cases {
assert_eq!(decode(output).unwrap(), (base, input.to_vec()));
Expand Down
Loading