• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

leprosus/kotlin-hashids: Kotlin hashids hash function

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

leprosus/kotlin-hashids

开源软件地址(OpenSource Url):

https://github.com/leprosus/kotlin-hashids

开源编程语言(OpenSource Language):

Kotlin 100.0%

开源软件介绍(OpenSource Introduction):

Hashids.kt

A Kotlin class to generate YouTube-like hashes from one or many numbers.

Ported from Java Hashids.java by fanweixiao (is port of javascript hashids.js by Ivan Akimov)

What is it?

Hashids (Hash ID's) creates short, unique, decryptable hashes from unsigned (long) integers.

This algorithm tries to satisfy the following requirements:

  1. Hashes must be unique and decryptable.
  2. They should be able to contain more than one integer (so you can use them in complex or clustered systems).
  3. You should be able to specify minimum hash length.
  4. Hashes should not contain basic English curse words (since they are meant to appear in public places - like the URL).

Instead of showing items as 1, 2, or 3, you could show them as U6dc, u87U, and HMou. You don't have to store these hashes in the database, but can encrypt + decrypt on the fly.

All (long) integers need to be greater than or equal to zero.

Usage

Import the package

import org.hashids;

Encrypting one number

You must pass a unique salt string so your hashes differ from everyone. I use "this is my salt" as an example.

val hashids = Hashids("this is my salt")
val hash: String = hashids.encode(12345)

hash is now going to be: NkK9

Decrypting

Notice: during decryption, the same salt value has to be used:

val hashids = Hashids("this is my salt")
val numbers: LongArray = hashids.decode("NkK9")
val numver: Int = numbers[0]

numbers is now going to be: [12345] number is: 12345

Decrypting with different salt

Decryption will not work if salt is changed:

val hashids = Hashids("this is my pepper")
val numbers: LongArray = hashids.decode("NkK9")

numbers is now going to be: []

Encrypting several numbers

val hashids = Hashids("this is my salt")
val hash: String = hashids.encode(683L, 94108L, 123L, 5L)

hash is now going to be: aBMswoO2UB3Sj

Decrypting is done the same way

val hashids = Hashids("this is my salt")
val numbers: String = hashids.decode("aBMswoO2UB3Sj")

numbers is now going to be: [683, 94108, 123, 5]

Encrypting and specifying minimum hash length

Here 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).

val hashids = Hashids("this is my salt", 8)
val hash: String = hashids.encode(1)

hash is now going to be: gB0NV05e

Decrypting

val hashids = Hashids("this is my salt", 8)
val numbers: String = hashids.decode("gB0NV05e")

numbers is now going to be: [1]

Specifying custom hash alphabet

Let's set the alphabet that consist of only four letters: "0123456789abcdef"

val hashids = Hashids("this is my salt", 0, "0123456789abcdef")
val hash: String = hashids.encode(1234567)

hash is now going to be: b332db5

Repeating numbers

val hashids = Hashids("this is my salt")
val hash: String = hashids.encode(5, 5, 5, 5);

You don't see any repeating patterns that might show there's 4 identical numbers in the hash: 1Wc8cwcE

Same with incremented numbers:

val hashids = Hashids("this is my salt")
val hash: String = hashids.encode(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

hash will be: kRHnurhptKcjIDTWC3sx

Incrementing number hashes:

val hashids = Hashids("this is my salt")
val hash1: String = hashids.encode(1) /* NV */
val hash2: String = hashids.encode(2) /* 6m */
val hash3: String = hashids.encode(3) /* yD */
val hash4: String = hashids.encode(4) /* 2l */
val hash5: String = hashids.encode(5) /* rD */

Contact

Follow me @leprosus, @IvanAkimov, @fanweixiao, @spuklo

License

MIT License. See the LICENSE file.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
LinXueyuanStdio/Data-for-LaTeX_OCR: LaTeX OCR 的数据仓库发布时间:2022-07-09
下一篇:
saiwal/latexthesis发布时间:2022-07-09
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap