Crate harsh[][src]

Expand description

A Rust-native implementation of Hashids.

Hashids provides a few benefits. For a start, identifiers are more difficult to confuse, provided that your selected alphabet does not include easily-confused symbols such as o, 0, O, 1, l, and I. Additionally, it becomes much more difficult for malicious or curious users to increment an identifier to “see what happens” when they throw it at your API. Lastly, Hashids values may combine several identifiers into a single value.

NOTE: Hashids values are not cryptographically secure. Regardless of the quality of your salt, this algorithm is fairly easy to crack.

Hashids should not be used for security purposes, but for your own convenience.

Creating and encoding

Harsh lacks a constructor (other than the default constructor, which should not be used), and should be created by the use of HarshBuilder, which allows for configuration with salts, alphabets, separators, and so forth.

Initialization ensures that appropriate values have been provided for the salt, alphabet, separators, and so forth, and in the event a struct cannot be created in a usable state, an error will be returned.

Encoding assumes zero or more input values and will return None in the even that zero inputs have been provided.

let harsh = Harsh::builder().salt("salt goes here!").build()?;
let encoded = harsh.encode(&[1, 2, 3, 4, 5]);

assert_eq!("xrUQTnhgu7", encoded);


Decoding likewise will return zero or more values in the form of a vector, but may also return None in the event that the decoded value is not a valid Hashid.

let harsh = Harsh::builder().salt("salt goes here!").build()?;
let decoded = harsh.decode(&encoded)?;

assert_eq!(&decoded, &[1, 2, 3, 4, 5]);


A hashids-compatible hasher.

A builder used to configure and create a Harsh instance.


Represents potential errors encountered during Harsh initialization.