#link-shortener #encode-decode #decode #encode #shortener #url #data-encoding

bs62

Base62 encoder / decoder with support for leading 0x00 bytes

5 releases

0.1.4 May 17, 2021
0.1.3 Mar 11, 2021
0.1.2 Mar 11, 2021
0.1.1 Mar 11, 2021
0.1.0 Mar 11, 2021

#2108 in Encoding

Download history 43/week @ 2024-03-13 51/week @ 2024-03-20 23/week @ 2024-03-27 26/week @ 2024-04-03 13/week @ 2024-04-10 19/week @ 2024-04-17 29/week @ 2024-04-24 9/week @ 2024-05-01 13/week @ 2024-05-08 20/week @ 2024-05-15 22/week @ 2024-05-22 16/week @ 2024-05-29 18/week @ 2024-06-05 47/week @ 2024-06-12 147/week @ 2024-06-19 43/week @ 2024-06-26

257 downloads per month
Used in basecracker

MIT license

21KB
203 lines

bs62

Crates.io License

A Base62 encoder / decoder with support for leading zero bytes.

Normally, during the conversion of base10 (decimal) to base62, the input data is interpreted as one large number:

[0x00, 0x13, 0x37] => 0x001337 => 4919 (decimal)

As leading zeroes do not count to the value of a number (001337 = 1337), they are ignored while converting the number to base62.

This is achieved by prepending a 0x01 byte to the data before encoding, thus creating a number that starts with a 1: 001337 => 1001337 (No zeroes are removed)

The leading 0x01 is removed after the data has been decoded from bse62 back to base10: 1001337 => 001337

Alphabet

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

This is the same alphabet that CyberChef uses by default: [0-9][A-Z][a-z].

Wikipedia/Base62 suggests another alphabet ([A-Z][a-z][0-9]) but I found that many online converters use either [0-9][A-Z][a-z] or [0-9][a-z][A-Z]. And as I love CyberChef, I decided to use their default alphabet aswell. I also think that starting with numbers is more natural as base62 is actually a number system like decimal (which is actually base10) or hexa-decimal (base16).

Examples

Convert Data to Base62

This method will prepend 0x01 to the data before encoding it.

let data = vec![0x13, 0x37];
let encoded = bs62::encode_data(&data);

assert_eq!(encoded, "IKN")

Parse Base62 to Data

This method expects a leading 0x01 in the byte array after decoding. It removes the first byte before returning the byte array.

let encoded = "IKN";
let data = bs62::decode_data(&encoded)?;

assert_eq!(data, vec![0x13_u8, 0x37]);

Convert a Number to Base62

let num = 1337;
let encoded = bs62::encode_num(&num);

assert_eq!(encoded, "LZ")

Parse Base62 to Number

let num = 1337;
let encoded = bs62::encode_num(&num);

assert_eq!(encoded, "LZ")

License

bs62 is licensed under MIT.

See the LICENSE.txt file in this repository for more information.

Dependencies

~495KB
~11K SLoC