{"id":13683508,"url":"https://github.com/archer884/harsh","last_synced_at":"2025-04-22T10:36:32.546Z","repository":{"id":45575380,"uuid":"66126050","full_name":"archer884/harsh","owner":"archer884","description":"Hashids implementation in Rust","archived":false,"fork":false,"pushed_at":"2021-12-07T18:21:58.000Z","size":96,"stargazers_count":63,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-22T10:36:16.908Z","etag":null,"topics":["database-ids","encoding","hash","hashids","ids","javascript","javascript-library","rust","rust-library"],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/archer884.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-20T03:26:42.000Z","updated_at":"2024-05-08T20:18:29.000Z","dependencies_parsed_at":"2022-09-04T13:23:02.450Z","dependency_job_id":null,"html_url":"https://github.com/archer884/harsh","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/archer884%2Fharsh","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/archer884%2Fharsh/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/archer884%2Fharsh/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/archer884%2Fharsh/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/archer884","download_url":"https://codeload.github.com/archer884/harsh/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250221680,"owners_count":21394746,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database-ids","encoding","hash","hashids","ids","javascript","javascript-library","rust","rust-library"],"created_at":"2024-08-02T13:02:13.713Z","updated_at":"2025-04-22T10:36:32.380Z","avatar_url":"https://github.com/archer884.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"\n[![hashids](http://hashids.org/public/img/hashids.gif \"Hashids\")](http://hashids.org/)\n\n**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](http://hashids.org/javascript)\n\nQuick example\n-------\n\n```rust\nlet harsh = Harsh::default();\nlet id = harsh.encode(\u0026[1, 2, 3]); // \"o2fXhV\"\nlet numbers = harsh.decode(id).unwrap(); // [1, 2, 3]\n```\n\n**Make your ids unique:**\n\nPass a project name to make your ids unique:\n\n```rust\nlet harsh = Harsh::builder().salt(\"My Project\").build().unwrap();\nlet id = harsh.encode(\u0026[1, 2, 3]); // \"Z4UrtW\"\n\nlet harsh = Harsh::builder().salt(\"My Other Project\").build().unwrap();\nlet id = harsh.encode(\u0026[1, 2, 3]); // \"gPUasb\"\n```\n\n**Use padding to make your ids longer:**\n\nNote that ids are only padded to fit **at least** a certain length. It doesn't mean that your ids will be *exactly* that length.\n\n```rust\nlet harsh = Harsh::default(); // no padding\nlet id = harsh.encode(\u0026[1]); // \"jR\"\n\nlet harsh = Harsh::builder().length(10).build().unwrap(); // pad to length 10\nlet id = harsh.encode(\u0026[1]); // \"VolejRejNm\"\n```\n\n**Pass a custom alphabet:**\n\n```rust\nlet harsh = Harsh::builder().alphabet(\"abcdefghijklmnopqrstuvwxyz\").build().unwrap(); // all lowercase\nlet id = harsh.encode(\u0026[1, 2, 3]); // \"mdfphx\"\n```\n\n**Encode hex instead of numbers:**\n\nUseful if you want to encode [Mongo](https://www.mongodb.com/)'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).\n\n```rust\nlet harsh = Harsh::default();\n\nlet id = harsh.encode_hex(\"507f1f77bcf86cd799439011\").unwrap(); // \"y42LW46J9luq3Xq9XMly\"\nlet hex = harsh.decode_hex(\"y42LW46J9luq3Xq9XMly\").unwrap(); // \"507f1f77bcf86cd799439011\" \n```\n\nPitfalls\n-------\n\n1. When decoding, output is always an array of numbers (even if you encode only one number):\n\n\t```rust\n\tlet harsh = Harsh::default();\n\n    let id = harsh.encode(\u0026[1]);\n    println!(\"{:?}\", harsh.decode(\u0026id).unwrap()); // [1]\n\t```\n\n2. Encoding negative numbers is not supported.\n3. If you pass bogus input to `encode()`, an empty string will be returned:\n\n\t```rust\n\tlet harsh = Harsh::default();\n\n\tlet id = harsh.decode(\"a123\"); // note lack of unwrap call; would panic here\n\tprintln!(\"{:?}\", id); // \"\"\n\t```\n\n4. Do not use this library as a security tool and do not encode sensitive data. This is **not** an encryption library.\n\nRandomness\n-------\n\nThe 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:\n\nNo repeating patterns showing there are 3 identical numbers in the id:\n\n```rust\nlet harsh = Harsh::default();\nprintln!(\"{}\", harsh.encode(\u0026[5, 5, 5])); // A6t1tQ\n```\n\nSame with incremented numbers:\n\n```rust\nlet harsh = Harsh::default();\n\nprintln!(\"{}\", harsh.encode(\u0026[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])); // wpfLh9iwsqt0uyCEFjHM\n\nprintln!(\"{}\", harsh.encode(\u0026[1])); // jR\nprintln!(\"{}\", harsh.encode(\u0026[2])); // k5\nprintln!(\"{}\", harsh.encode(\u0026[3])); // l5\nprintln!(\"{}\", harsh.encode(\u0026[4])); // mO\nprintln!(\"{}\", harsh.encode(\u0026[5])); // nR\n```\n\nCurses! #$%@\n-------\n\nThis 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:\n\n\tc, f, h, i, s, t, u\n\nSupport\n-------\n\nHave a question? Open an issue here, or find the author of the original JavaScript library: \n\n\u003e [@IvanAkimov](http://twitter.com/ivanakimov) or [ivanakimov.com](http://ivanakimov.com)\n\nMaybe one of these days I'll get around to fixing my website up. :)\n\nChangelog\n---------\n\n### 0.2.0\n\n- Convert to result-based API and add quickcheck tests courtesy of Dr-Emann.\n\n### 0.1.5\n\n- Fix panic when decoding values containing characters not found in alphabet\n\n### 0.1.3\n\n- Remove dependency on clippy. (Still using clippy, but just as `cargo clippy` now.)\n\n### 0.1.2\n\n- Changed `HarshFactory` to `HarshBuilder` in order to stop rubbing my OCD the wrong way\u003csup\u003e1\u003c/sup\u003e\n- Updated dependencies\n\n\u003e 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!\n\nLicense\n-------\n\nLicensed under either of\n\n * Apache License, Version 2.0, ([LICENSE-APACHE][license-url-ap2] or http://www.apache.org/licenses/LICENSE-2.0)\n * MIT license ([LICENSE-MIT][license-url-mit] or http://opensource.org/licenses/MIT)\n\nat your option.\n\n### Contribution\n\nUnless 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.\n\n[travis-url]: https://travis-ci.org/archer884/harsh\n[travis-image]: https://travis-ci.org/archer884/harsh.svg?branch=master\n\n[appveyor-url]: (https://ci.appveyor.com/project/archer884/harsh)\n[appveyor-image]: https://ci.appveyor.com/api/projects/status/github/archer884/harsh?branch=master\u0026svg=true\n\n[license-url-mit]: https://github.com/archer884/harsh/blob/master/LICENSE-MIT\n[license-url-ap2]: https://github.com/archer884/harsh/blob/master/LICENSE-APACHE\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farcher884%2Fharsh","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farcher884%2Fharsh","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farcher884%2Fharsh/lists"}