base-62

Crates.iobase-62
lib.rsbase-62
version0.1.1
sourcesrc
created_at2019-01-12 23:37:41.419637
updated_at2019-01-12 23:40:48.065067
descriptionencode to/from bytes to base62 string
homepagehttps://github.com/kryptco/base62.rs
repositoryhttps://github.com/kryptco/base62.rs
max_upload_size
id108236
size8,586
Alex Grinman (agrinman)

documentation

README

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();
}
Commit count: 7

cargo fmt