Crate pricklybirdlib

Crate pricklybirdlib 

Source
Expand description

GitHub License Crate


§Overview

pricklybird is a method for conversion of arbitrary binary data into more human-friendly words, where each word represents a single byte.
A CRC-8 checksum is attached to allow the detection of errors during decoding.
0xDEADBEEF becomes turf-port-rust-warn-void, for example.

pricklybirdlib is a rust implementation pricklybird version v1.

§Documentation

Documentation is hosted on on docs.rs.

§Usage

Basic conversion functions that fully comply with the specification and include the CRC can be used as follows.

use pricklybirdlib::{convert_to_pricklybird, convert_from_pricklybird};
let data = [0x42_u8, 0x43];
let words = convert_to_pricklybird(&data);
// Notice the third word "full" used to encode the CRC.
assert_eq!("flea-flux-full", words);
let recovered_data = convert_from_pricklybird(&words).unwrap();
assert_eq!(vec![0x42, 0x43], recovered_data);

Is is also possible to map word to bytes and bytes to words without the full standard implementation and CRC. The words are encoded as four bytes of ASCII compatible UTF-8, since the wordlist contains no non ASCII characters and all words are four letters long.

use pricklybirdlib::{words_to_bytes, bytes_to_words};
let data = [0x42_u8, 0x43];
let words = bytes_to_words(&data);
// Notice that no CRC is attached, the bytes represent the words: "flea", "flux"
assert_eq!(vec![[102, 108, 101, 97], [102, 108, 117, 120]], words);
let word_str = vec!["flea", "flux"];
let recovered_data = words_to_bytes(&word_str).unwrap();
assert_eq!(vec![0x42, 0x43], recovered_data); 

The constants module allows direct access to the WORDLIST used for mapping bytes to words, and the HASH_TABLE use to map words to bytes.

use pricklybirdlib::constants::{word_hash, HASH_TABLE, WORDLIST};
// Confirm that the word flux maps to the byte 0x43 in both directions.
let word = "flux".as_bytes();
let table_index = word_hash(word[0], word[3]);
let byte_value = HASH_TABLE[table_index];
assert_eq!(0x43, byte_value);
assert_eq!("flux", WORDLIST[0x43])

§License

pricklybirdlib is distributed under the terms of the MIT license.

Modules§

constants
Contains pricklybird wordlist, reverse wordlist hashmap and CRC-8 lookup table.

Enums§

DecodeError
An error occured while trying to decode pricklybird words.

Constants§

PRICKLYBIRD_VERSION
Version of the pricklybird specification that this implementation complies with.

Functions§

bytes_to_words
Convert bytearray to list of pricklybird words.
calculate_crc8
Calculate the CRC-8 used by pricklybird based on a precomputed table.
convert_from_pricklybird
Convert a pricklybird string to bytes and check CRC.
convert_to_pricklybird
Convert arbitrary data to a pricklybird string and attach CRC.
words_to_bytes
Return a vector of bytes coresponding to the pricklybird words supplied as input.