Crate data_encoding [−] [src]
This crate provides generic data encoding functions.
Encoding and decoding functions are provided for common bases.
Those functions are instantiated from generic functions using a
base interface described in module base. The
generic encoding and decoding functions are defined in the
encode and decode
modules respectively.
Examples
use data_encoding::hex; use data_encoding::base64; assert_eq!(hex::encode(b"some raw data").as_bytes(), b"736F6D65207261772064617461"); assert_eq!(base64::decode(b"c29tZSByYXcgZGF0YQ==").unwrap(), b"some raw data");
A more involved example is available in the examples directory.
Properties
The encoding and decoding functions satisfy the following properties:
- They are deterministic: their output only depends on their input.
- They have no side-effects: they do not modify a hidden mutable state.
- They never panic, although the decoding function may return a decoding error on invalid input.
- They are inverses of each other:
- For all
data: Vec<u8>, we havedecode(encode(&data).as_bytes()) == Ok(data). - For all
repr: String, if there isdata: Vec<u8>such thatdecode(repr.as_bytes()) == Ok(data), thenencode(&data) == repr.
- For all
Conformance
The base16, hex,
base32,
base32hex,
base64, and
base64url modules are conform to RFC
4648.
Performance
This crate has comparable performance to the rustc-serialize
crate and the base64 GNU program.
Reexports
pub use base16 as hex; |
Modules
| base |
Generic base module. |
| base16 |
Base 16 Encoding. |
| base2 |
Base 2 Encoding. |
| base32 |
Base 32 Encoding. |
| base32hex |
Base 32 Encoding with Extended Hex Alphabet. |
| base4 |
Base 4 Encoding. |
| base64 |
Base 64 Encoding. |
| base64url |
Base 64 Encoding with URL and Filename Safe Alphabet. |
| base8 |
Base 8 Encoding. |
| decode |
Generic decoding module. |
| encode |
Generic encoding module. |