#base62 #encode #decode

base-62

encode to/from bytes to base62 string

2 releases

0.1.1 Jan 12, 2019
0.1.0 Jan 12, 2019

#5 in #base62

Download history 264/week @ 2021-05-28 281/week @ 2021-06-04 266/week @ 2021-06-11 241/week @ 2021-06-18 287/week @ 2021-06-25 232/week @ 2021-07-02 221/week @ 2021-07-09 258/week @ 2021-07-16 337/week @ 2021-07-23 165/week @ 2021-07-30 228/week @ 2021-08-06 217/week @ 2021-08-13 202/week @ 2021-08-20 124/week @ 2021-08-27 104/week @ 2021-09-03 194/week @ 2021-09-10

1,077 downloads per month

MIT license

7KB
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

~520KB
~11K SLoC