All Projects → archer884 → harsh

archer884 / harsh

Licence: Apache-2.0, MIT licenses found Licenses found Apache-2.0 LICENSE-APACHE MIT LICENSE-MIT
Hashids implementation in Rust

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to harsh

Hashids.js
A small JavaScript library to generate YouTube-like ids from numbers.
Stars: ✭ 3,525 (+7243.75%)
Mutual labels:  encoding, hashids, hash, ids, database-ids
hashids.pm
Hashids, ported for Perl
Stars: ✭ 15 (-68.75%)
Mutual labels:  encoding, hashids, hash, ids, database-ids
Hashids
A small PHP library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database ids to the user.
Stars: ✭ 4,596 (+9475%)
Mutual labels:  encoding, hashids, hash, ids, database-ids
laravel-hashid
HashId Implementation on Laravel Eloquent ORM
Stars: ✭ 23 (-52.08%)
Mutual labels:  hashids, hash
murmur3
A rust implementation of murmur3
Stars: ✭ 48 (+0%)
Mutual labels:  hash, rust-library
idy
👓 An ID obfuscator for ActiveRecord
Stars: ✭ 15 (-68.75%)
Mutual labels:  hashids, hash
Hashids.net
A small .NET package to generate YouTube-like hashes from one or many numbers. Use hashids when you do not want to expose your database ids to the user.
Stars: ✭ 470 (+879.17%)
Mutual labels:  encoding, hashids
Optimus
🤖 Id obfuscation based on Knuth's multiplicative hashing method for PHP.
Stars: ✭ 1,084 (+2158.33%)
Mutual labels:  hashids, ids
Bitmatch
A Rust crate that allows you to match, bind, and pack the individual bits of integers.
Stars: ✭ 82 (+70.83%)
Mutual labels:  encoding, rust-library
Codetective
a tool to determine the crypto/encoding algorithm used according to traces from its representation
Stars: ✭ 121 (+152.08%)
Mutual labels:  encoding, hash
Hashlib4pascal
Hashing for Modern Object Pascal
Stars: ✭ 132 (+175%)
Mutual labels:  encoding, hash
bytes-java
Bytes is a utility library that makes it easy to create, parse, transform, validate and convert byte arrays in Java. It supports endianness as well as immutability and mutability, so the caller may decide to favor performance.
Stars: ✭ 120 (+150%)
Mutual labels:  encoding, hash
id-mask
IDMask is a Java library for masking internal ids (e.g. from your DB) when they need to be published to hide their actual value and to prevent forging. It has support optional randomisation has a wide support for various Java types including long, UUID and BigInteger. This library bases its security on strong cryptographic primitives.
Stars: ✭ 39 (-18.75%)
Mutual labels:  hashids, database-ids
Amino.JS
A powerful JavaScript library for interacting with the Amino API 🌟
Stars: ✭ 25 (-47.92%)
Mutual labels:  javascript-library
ds
🔗 Common Data Structures and Algorithms
Stars: ✭ 40 (-16.67%)
Mutual labels:  hash
IPRadar2
Real-time detection and defense against malicious network activity and policy violations (exploits, port-scanners, advertising, telemetry, state surveillance, etc.)
Stars: ✭ 20 (-58.33%)
Mutual labels:  ids
wazuh-cloudformation
Wazuh - Amazon AWS Cloudformation
Stars: ✭ 32 (-33.33%)
Mutual labels:  ids
placekey-js
placekey.io
Stars: ✭ 19 (-60.42%)
Mutual labels:  javascript-library
blinkt
A Rust library for the Pimoroni Blinkt!, and any similar APA102 or SK9822 LED strips or boards, on a Raspberry Pi.
Stars: ✭ 18 (-62.5%)
Mutual labels:  rust-library
expand-hash
Recursively expands property keys with dot-notation into objects.
Stars: ✭ 25 (-47.92%)
Mutual labels:  hash

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].