{"id":13524613,"url":"https://github.com/leprosus/kotlin-hashids","last_synced_at":"2025-04-01T03:32:12.785Z","repository":{"id":25992585,"uuid":"29434901","full_name":"leprosus/kotlin-hashids","owner":"leprosus","description":"Kotlin hashids hash function","archived":false,"fork":false,"pushed_at":"2022-03-01T12:29:21.000Z","size":73,"stargazers_count":119,"open_issues_count":2,"forks_count":26,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-11-02T09:31:40.228Z","etag":null,"topics":["decode","encode","hash","hashids","kotlin"],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leprosus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-18T17:54:48.000Z","updated_at":"2024-06-11T17:04:12.000Z","dependencies_parsed_at":"2022-08-24T03:00:19.803Z","dependency_job_id":null,"html_url":"https://github.com/leprosus/kotlin-hashids","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leprosus%2Fkotlin-hashids","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leprosus%2Fkotlin-hashids/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leprosus%2Fkotlin-hashids/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leprosus%2Fkotlin-hashids/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leprosus","download_url":"https://codeload.github.com/leprosus/kotlin-hashids/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246578409,"owners_count":20799813,"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":["decode","encode","hash","hashids","kotlin"],"created_at":"2024-08-01T06:01:11.732Z","updated_at":"2025-04-01T03:32:12.509Z","avatar_url":"https://github.com/leprosus.png","language":"Kotlin","funding_links":[],"categories":["开源库和框架"],"sub_categories":["其他"],"readme":"# Hashids.kt\n\nA Kotlin class to generate YouTube-like hashes from one or many numbers.\n\nPorted from Java [Hashids.java](https://github.com/jiecao-fm/hashids-java/blob/master/src/main/java/org/hashids/Hashids.java) by [fanweixiao](https://github.com/fanweixiao) (is port of javascript [hashids.js](https://github.com/ivanakimov/hashids.js/blob/master/lib/hashids.js) by [Ivan Akimov](https://github.com/ivanakimov))\n\n## What is it?\n\nHashids (Hash ID's) creates short, unique, decryptable hashes from unsigned (long) integers.\n\nThis algorithm tries to satisfy the following requirements:\n\n1. Hashes must be unique and decryptable.\n2. They should be able to contain more than one integer (so you can use them in complex or clustered systems).\n3. You should be able to specify minimum hash length.\n4. Hashes should not contain basic English curse words (since they are meant to appear in public places - like the URL).\n\nInstead of showing items as `1`, `2`, or `3`, you could show them as `U6dc`, `u87U`, and `HMou`.\nYou don't have to store these hashes in the database, but can encrypt + decrypt on the fly.\n\nAll (long) integers need to be greater than or equal to zero.\n\n## Usage\n\n#### Import the package\n\n```java\nimport org.hashids;\n```\n\n#### Encrypting one number\n\nYou must pass a unique salt string so your hashes differ from everyone. I use \"this is my salt\" as an example.\n\n```kotlin\n\nval hashids = Hashids(\"this is my salt\")\nval hash: String = hashids.encode(12345)\n```\n\n`hash` is now going to be: NkK9\n\n#### Decrypting\n\nNotice: during decryption, the same salt value has to be used:\n\n```kotlin\n\nval hashids = Hashids(\"this is my salt\")\nval numbers: LongArray = hashids.decode(\"NkK9\")\nval numver: Int = numbers[0]\n```\n\n`numbers` is now going to be: [12345]\n`number` is: 12345\n\n#### Decrypting with different salt\n\nDecryption will not work if salt is changed:\n\n```kotlin\n\nval hashids = Hashids(\"this is my pepper\")\nval numbers: LongArray = hashids.decode(\"NkK9\")\n```\n\n`numbers` is now going to be: []\n\n#### Encrypting several numbers\n\n```kotlin\n\nval hashids = Hashids(\"this is my salt\")\nval hash: String = hashids.encode(683L, 94108L, 123L, 5L)\n```\n\n`hash` is now going to be: aBMswoO2UB3Sj\n\n#### Decrypting is done the same way\n\n```kotlin\n\nval hashids = Hashids(\"this is my salt\")\nval numbers: String = hashids.decode(\"aBMswoO2UB3Sj\")\n```\n\n`numbers` is now going to be: [683, 94108, 123, 5]\n\n#### Encrypting and specifying minimum hash length\n\nHere we encode integer 1, and set the minimum hash length to **8** (by default it's **0** -- meaning hashes will be the shortest possible length).\n\n```kotlin\n\nval hashids = Hashids(\"this is my salt\", 8)\nval hash: String = hashids.encode(1)\n```\n\n`hash` is now going to be: gB0NV05e\n\n#### Decrypting\n\n```kotlin\n\nval hashids = Hashids(\"this is my salt\", 8)\nval numbers: String = hashids.decode(\"gB0NV05e\")\n```\n\n`numbers` is now going to be: [1]\n\n#### Specifying custom hash alphabet\n\nLet's set the alphabet that consist of only four letters: \"0123456789abcdef\"\n\n```kotlin\n\nval hashids = Hashids(\"this is my salt\", 0, \"0123456789abcdef\")\nval hash: String = hashids.encode(1234567)\n```\n\n`hash` is now going to be: b332db5\n\n#### Repeating numbers\n\n```kotlin\n\nval hashids = Hashids(\"this is my salt\")\nval hash: String = hashids.encode(5, 5, 5, 5);\n```\n\nYou don't see any repeating patterns that might show there's 4 identical numbers in the hash: 1Wc8cwcE\n\nSame with incremented numbers:\n\n```kotlin\n\nval hashids = Hashids(\"this is my salt\")\nval hash: String = hashids.encode(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);\n```\n\n`hash` will be: kRHnurhptKcjIDTWC3sx\n\n### Incrementing number hashes:\n\n```kotlin\n\nval hashids = Hashids(\"this is my salt\")\nval hash1: String = hashids.encode(1) /* NV */\nval hash2: String = hashids.encode(2) /* 6m */\nval hash3: String = hashids.encode(3) /* yD */\nval hash4: String = hashids.encode(4) /* 2l */\nval hash5: String = hashids.encode(5) /* rD */\n```\n\n## Contact\n\nFollow me [@leprosus](https://twitter.com/leprosus_ru), [@IvanAkimov](http://twitter.com/ivanakimov), [@fanweixiao](https://twitter.com/fanweixiao), [@spuklo](https://twitter.com/spuklo)\n\n## License\n\nMIT License. See the `LICENSE` file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleprosus%2Fkotlin-hashids","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleprosus%2Fkotlin-hashids","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleprosus%2Fkotlin-hashids/lists"}