base_62/
lib.rs

1// Copyright 2019 KryptCo, Inc
2//
3// Licensed under the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>.
4// This file may not be copied, modified, or distributed except according to those terms.
5
6/*!
7A library for encoding/decoding byte arrays to/from a base62 strings.
8
9# Alphabet
10This library defines the Base62 alphabet as the following characters:
11
12```0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz```
13
14# How it works
15A byte array (leading zeros allowed) is prepended with `0x01` and is treated
16as a big-endian unsigned integer (`num_bigint::BigUint`).
17
18This number is repeatedly divided by our base, `62`, and each remainder is used as an index into our alphabet above,
19producing the base62 encoded string.
20
21To decode, we run the algorithm above in reverse.
22
23# Example
24
25```rust
26
27fn main() {
28    let input = vec![0xDE,0xAD,0xBE,0xEF];
29    let encoded = base_62::encode(&input);
30    println!("0xDEADBEEF = {}", encoded);
31    let deadbeef = base_62::decode("JsoUl8").unwrap();
32
33    let input = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt";
34    let encoded = base_62::encode(input.as_bytes());
35    println!("lorem... = {}", encoded);
36    let loremipsum = base_62::decode("Inj62xrWzFT5RgFoP72ZkfbrMabXdyZeYGijtTt8zuBN4XvHvEw6x2pk2BtdepGle57axcSeY2ixeXqOvwpE2VaEE3pHeeumHvIbZf0qUUxRBg99NrIALFCE").unwrap();
37}
38```
39*/
40
41pub mod base62;
42pub use self::base62::*;