Giter VIP home page Giter VIP logo

harsh's Introduction

hashids

Harsh is a Rust implementation of the Hashids JavaScript library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database ids to the user: http://hashids.org/javascript

Quick example

let harsh = Harsh::default();
let id = harsh.encode(&[1, 2, 3]); // "o2fXhV"
let numbers = harsh.decode(id).unwrap(); // [1, 2, 3]

Make your ids unique:

Pass a project name to make your ids unique:

let harsh = Harsh::builder().salt("My Project").build().unwrap();
let id = harsh.encode(&[1, 2, 3]); // "Z4UrtW"

let harsh = Harsh::builder().salt("My Other Project").build().unwrap();
let id = harsh.encode(&[1, 2, 3]); // "gPUasb"

Use padding to make your ids longer:

Note that ids are only padded to fit at least a certain length. It doesn't mean that your ids will be exactly that length.

let harsh = Harsh::default(); // no padding
let id = harsh.encode(&[1]); // "jR"

let harsh = Harsh::builder().length(10).build().unwrap(); // pad to length 10
let id = harsh.encode(&[1]); // "VolejRejNm"

Pass a custom alphabet:

let harsh = Harsh::builder().alphabet("abcdefghijklmnopqrstuvwxyz").build().unwrap(); // all lowercase
let id = harsh.encode(&[1, 2, 3]); // "mdfphx"

Encode hex instead of numbers:

Useful if you want to encode Mongo's ObjectIds. Note that there is no limit on how large of a hex number you can pass (it does not have to be Mongo's ObjectId).

let harsh = Harsh::default();

let id = harsh.encode_hex("507f1f77bcf86cd799439011").unwrap(); // "y42LW46J9luq3Xq9XMly"
let hex = harsh.decode_hex("y42LW46J9luq3Xq9XMly").unwrap(); // "507f1f77bcf86cd799439011" 

Pitfalls

  1. When decoding, output is always an array of numbers (even if you encode only one number):

    let harsh = Harsh::default();
    
    let id = harsh.encode(&[1]);
    println!("{:?}", harsh.decode(&id).unwrap()); // [1]
  2. Encoding negative numbers is not supported.

  3. If you pass bogus input to encode(), an empty string will be returned:

    let harsh = Harsh::default();
    
    let id = harsh.decode("a123"); // note lack of unwrap call; would panic here
    println!("{:?}", id); // ""
  4. Do not use this library as a security tool and do not encode sensitive data. This is not an encryption library.

Randomness

The primary purpose of Hashids is to obfuscate ids. It's not meant or tested to be used as a security or compression tool. Having said that, this algorithm does try to make these ids random and unpredictable:

No repeating patterns showing there are 3 identical numbers in the id:

let harsh = Harsh::default();
println!("{}", harsh.encode(&[5, 5, 5])); // A6t1tQ

Same with incremented numbers:

let harsh = Harsh::default();

println!("{}", harsh.encode(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])); // wpfLh9iwsqt0uyCEFjHM

println!("{}", harsh.encode(&[1])); // jR
println!("{}", harsh.encode(&[2])); // k5
println!("{}", harsh.encode(&[3])); // l5
println!("{}", harsh.encode(&[4])); // mO
println!("{}", harsh.encode(&[5])); // nR

Curses! #$%@

This code was written with the intent of placing created ids in visible places, like the URL. Therefore, the algorithm tries to avoid generating most common English curse words by generating ids that never have the following letters next to each other:

c, f, h, i, s, t, u

Support

Have a question? Open an issue here, or find the author of the original JavaScript library:

@IvanAkimov or ivanakimov.com

Maybe one of these days I'll get around to fixing my website up. :)

Changelog

0.2.0

  • Convert to result-based API and add quickcheck tests courtesy of Dr-Emann.

0.1.5

  • Fix panic when decoding values containing characters not found in alphabet

0.1.3

  • Remove dependency on clippy. (Still using clippy, but just as cargo clippy now.)

0.1.2

  • Changed HarshFactory to HarshBuilder in order to stop rubbing my OCD the wrong way1
  • Updated dependencies
  1. I apologize for the inconvenience this causes, but we all know this is better in the long run; if I stay sane, I can continue to keep this lib up to date!

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

harsh's People

Contributors

archer884 avatar ewouth avatar knnut avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

harsh's Issues

Harsh::decode panics when given input with invalid alphabet

Here's the test case:

extern crate harsh;

fn main() {
    let h = harsh::HarshBuilder::new()
        .init().unwrap();
    h.decode("non-alphabet.characters=will|cause$panic");
    println!("No hello worlds :(");
}

And the output:

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/harsh-test`
thread 'main' panicked at 'what a world, what a world!', libcore/option.rs:914:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.

Expected the decode to return Option::None, not panic.

Harsh version 0.1.4

Would you consider implementing encoding and decoding for u128?

I'm thinking about using this crate to provide hashids for the webserver I'm building, but my database is storing identifiers as UUIDs, which can only be reliably represented by u128.

This being said, I would like to ask if you would consider accepting u128 as input for encoding and decoding. If acceptable, I'm willing to try and propose an implementation.

Support generating numeric hashIDs

I wanted to configure harsh to encode IDs using only 0-9 so it still looks like a number, but got BuildError::AlphabetLength from HarshBuilder::build() when I tried to pass an alphabet of "0123456789".

Is there any particular reason why it requires that the alphabet be at least 16 characters? There's no comments about it in the source. If there is one, it should probably be documented on HarshBuilder::alphabet().

Encode via `Display` adapter as an alternative to going through `String`

In our projects at work, wherever we're using hash IDs, I like to create adaptor structs to make them easier to work with:

static CODEC: std::sync::Lazy<harsh::Harsh> = Lazy::new(|| /* init `Harsh` */);

pub struct SomeHashedId(pub u64);

impl FromStr for SomeHashedId {
    // implement using `Harsh::decode()`
}

impl Display for SomeHashedId {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.pad(&CODEC.encode(&[self.0]))
    }
}

However, implementing Display using Harsh::encode() is inefficient since it requires going through String. I would really like to see something like:

impl Harsh {
    // it'd then be trivial to implement `encode` in terms of this method
    pub fn encode_fmt<'a>(&'a self, ids: &'a [u64]) -> impl Display + 'a {
        // ...
    }
}

so the above Display impl could be implemented as:

impl Display for SomeHashedId {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        CODEC.encode_fmt(&[self.0]).fmt(f)
    }
}

Decodes invalid hashes without errors

Hi,

first, thank you for the great crate!

I have just observed that when I add to a hash some random characters and try to decode it, it still decodes without errors.

    let harsh = HarshBuilder::new().length(4).init().unwrap();

    let id = harsh.encode(&[1]).unwrap();
    println!("encoded: {}", &id);
    let numbers = harsh.decode("ejRe12345").unwrap();
    println!("{:?}", numbers);

Output:

encoded: ejRe
[1]

As a reference I took the JS library https://codepen.io/anon/pen/vqGmBX But it reported an error.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.