Function bs62::decode_data_forgiving [−][src]
pub fn decode_data_forgiving(inp: &str) -> Result<Vec<u8>, Box<dyn Error>>
Expand description
Decode data just like decode_data
but allow data that was not prepended
with a 0x01
byte.
ATTENTION: Using this method might corrupt the data! If the real data
started with an actual 0x01
byte and is encoded without using the
encode_data
method, this first byte will be removed!
If the decoded data does not begin with a 0x01
byte, nothing is remove and
the data vector is returned just as is.
This method is motly used to allow human generated base62 strings to be used as seeds for RNGs.
Errors
An error variant is returned when the input string contains invlid chars.
Example
use num_bigint::BigUint; use num_traits::FromPrimitive; let orig = 1337; // Convert to byte array let orig_data = BigUint::from_i32(orig) .ok_or("Failed to convert `i32` to `BigUint`")? .to_bytes_be(); // Encode using both methods let data_encoded = bs62::encode_data(&orig_data); let num_encoded = bs62::encode_num(&orig); // The `encode_data` function produces a different encoded string because of // the prepended `0x01` byte: assert_eq!(data_encoded, "HOb"); assert_eq!(num_encoded, "LZ"); // Convert both encoded strings back using the same funtion let data = bs62::decode_data_forgiving(&data_encoded)?; let num = bs62::decode_data_forgiving(&num_encoded)?; // They should both produce the same (original) byte array assert_eq!(data, orig_data); assert_eq!(num, orig_data);