Crate base32_fs

Crate base32_fs 

Source
Expand description

§base32-fs

Crates.io Version Docs dependency status

This crate implements a variant of BASE32 encoding for hashes that are used as file names: no two encoded hashes can be decoded to the same original hash unless there is a hash collision.

To achieve that the crate

  • uses the same characters when encoding and decoding the data as opposed to the original Crockford’s alphabet that permits e.g. both “a” and “A” to be decoded to 10;
  • doesn’t zero-extend invalid input lengths when decoding.

Besides that the crate

  • uses only lowercase letters instead of uppercase as the most common representation of hashes;
  • doesn’t use padding characters;
  • doesn’t change the sorting order of the encoded data;
  • doesn’t use lookup table when decoding.

§Usage

§Encode into PathBuf

use std::path::PathBuf;
use base32_fs::{encode, encoded_len, PathBufOutput};

let input = *b"hello";
let mut output = PathBufOutput::with_capacity(encoded_len(input.len()));
encode(&input, &mut output);
assert_eq!(PathBuf::from("d1jprv3f"), output.into_path_buf());

§Decode from PathBuf

use std::path::Path;
use base32_fs::{decode, decoded_len, PathBufInput};

let input = PathBufInput::new(Path::new("d1jprv3f"));
let mut output: Vec<u8> = Vec::new();
decode(input, &mut output);
assert_eq!(b"hello", output.as_slice());

§Encode into Vec<u8>

use base32_fs::{encode, encoded_len};

let input = *b"hello";
let mut output: Vec<u8> = Vec::with_capacity(encoded_len(input.len()));
encode(&input, &mut output);
let string = std::str::from_utf8(output.as_slice()).expect("Always a valid UTF-8 byte sequence");
assert_eq!("d1jprv3f", string);

§Decode from &[u8]

use std::path::Path;
use base32_fs::{decode, decoded_len, PathBufInput};

let input = b"d1jprv3f";
let mut output: Vec<u8> = Vec::with_capacity(decoded_len(input.len()).unwrap());
decode(input.as_slice(), &mut output);
assert_eq!(b"hello", output.as_slice());

Structs§

DecodeError
BASE32 decode error.
PathBufInputstd
An implementation of Input for file system paths.
PathBufOutputstd
An implementation of Output for file system paths.

Constants§

MAX_INPUT_LEN
Maximum number of bytes that can be encoded as BASE32.

Traits§

Input
An input for decode.
Output
An output of encode or decode.

Functions§

decode
Decode input byte sequence using BASE32 encoding and write the resulting byte sequence to output.
decoded_len
Returns the length of the original byte sequence for the given BASE32-encoded string length.
encode
Encode input byte sequence using BASE32 encoding and write the resulting byte sequence to output.
encoded_len
Returns the length of the BASE32-encoded string for the given input length.
is_valid
Returns true if the input is a valid BASE32-encoded string.