2 releases

0.1.1 Jan 12, 2019
0.1.0 Jan 12, 2019

#2477 in Encoding

Download history 550/week @ 2024-07-23 661/week @ 2024-07-30 460/week @ 2024-08-06 620/week @ 2024-08-13 450/week @ 2024-08-20 511/week @ 2024-08-27 623/week @ 2024-09-03 636/week @ 2024-09-10 296/week @ 2024-09-17 472/week @ 2024-09-24 681/week @ 2024-10-01 690/week @ 2024-10-08 1065/week @ 2024-10-15 1434/week @ 2024-10-22 1263/week @ 2024-10-29 1042/week @ 2024-11-05

4,886 downloads per month
Used in 2 crates

MIT license

8KB
85 lines

base62.rs

A library for encoding/decoding byte arrays to/from a base62 strings.

Alphabet

This library defines the Base62 alphabet as the following characters:

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

How it works

A byte array (leading zeros allowed) is prepended with 0x01 and is treated as a big-endian unsigned integer (num_bigint::BigUint).

This number is repeatedly divided by our base, 62, and each remainder is used as an index into our alphabet above, producing the base62 encoded string.

To decode, we run the algorithm above in reverse.

Example

fn main() {
    let input = vec![0xDE,0xAD,0xBE,0xEF];
    let encoded = base62::encode(&input);
    println!("0xDEADBEEF = {}", encoded);
    let deadbeef = base62::decode("JsoUl8").unwrap();

    let input = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt";
    let encoded = base62::encode(input.as_bytes());
    println!("lorem... = {}", encoded);
    let loremipsum = base62::decode("Inj62xrWzFT5RgFoP72ZkfbrMabXdyZeYGijtTt8zuBN4XvHvEw6x2pk2BtdepGle57axcSeY2ixeXqOvwpE2VaEE3pHeeumHvIbZf0qUUxRBg99NrIALFCE").unwrap();
}

Dependencies

~565KB
~12K SLoC