A Scala port of hashids.js library to generate short hashes from one or many numbers. Ported from hashids.java by fanweixiao
- Hashid is initialized with an
alphabet
,salt
and aminimum hash length
- It's possible to hash single and multiple long numbers
- Hashes are unique across the salt value
- Hashes are decryptable to a single or multiple numbers respectively
- Hashes don't contain English curse words
- Supports positive long numbers
- The primary purpose of hashids is to obfuscate ids
- Do not use hashids for security purposes or compression
Besides the goals of the original library, this scala port is written without mutable state. Also you get clear exceptions in following cases:
IllegalArgumentException
when alphabet, you provided, contains duplicatesIllegalArgumentException
if alphabet contains spacesIllegalArgumentException
if alphabet is less then 16 chars longIllegalArgumentException
when callingencodeHex
with non-HEX stringIllegalStateException
when callingdecode
with hash, produced with different salt
Cross-Built for Scala 2.11, 2.12 and 2.13
libraryDependencies += "com.github.ancane" %% "hashids-scala" % "1.4"
import org.hashids.Hashids, Hashids._
You should provide your own unique salt to get hashes, different from other hashids. Do not use salf from the examples.
val hashids = Hashids("this is my salt")
val hash = hashids.encode(12345L)
> "NkK9"
During decryption, same salt must be used to get original numbers back:
val hashids = Hashids("this is my salt")
val numbers = hashids.decode("NkK9")
> List(12345L): Seq[Long]
MIT License. See the LICENSE
file.