pdamoc / elm-hashids / Hashids

This is an Elm port of the Hashids library by Ivan Akimov. This is not a cryptographic hashing algorithm. Hashids is typically used to encode numbers to a format suitable for appearance in places like urls.

See the official Hashids home page: http://hashids.org

Hashids is a small open-source library that generates short, unique, non-sequential ids from numbers. It converts numbers like 347 into strings like

Types


type alias Context =
{ guards : String
, seps : String
, salt : String
, minHashLength : Basics.Int
, alphabet : String 
}

A record with various internals required for encoding and decoding.

Context object constructors

createHashidsContext : String -> Basics.Int -> String -> Context

Create a context object using the given salt, a minimum hash length, and a custom alphabet. If you only need to supply the salt, or the first two arguments, use 'hashidsSimple' or 'hashidsMinimum' instead.

Changing the alphabet is useful if you want to make your hashes unique, i.e., create hashes different from those generated by other applications relying on the same algorithm.

hashidsSimple : String -> Context

Create a context object using the default alphabet and the provided salt, without any minimum required length.

hashidsMinimum : String -> Basics.Int -> Context

Create a context object using the default alphabet and the provided salt. The generated hashes will have a minimum length as specified by the second argument.

Encoding and decoding

encodeHex : Context -> String -> String

Encode a hexadecimal number.

Example use:

encodeHex context "ff83"

decodeHex : Context -> String -> String

Decode a hash generated with 'encodeHex'.

Example use:

decodeHex context "yzgwD"

encode : Context -> Basics.Int -> String

Encode a single number.

Example use:

hash =
    let
        context =
            hashidsSimple "this is my salt"
    in
    encode context 5


-- == "rD"

encodeList : Context -> List Basics.Int -> String

Encode a list of numbers.

Example use:

hash =
    let
        context =
            hashidsSimple "this is my salt"
    in
    encodeList context [ 2, 3, 5, 7, 11 ]


-- == "EOurh6cbTD"

decode : Context -> String -> List Basics.Int

Decode a hash.

Example use:

hash =
    let
        context =
            hashidsSimple "this is my salt"
    in
    decode context "rD"


-- == [5]

Convenience wrappers

encodeUsingSalt : String -> Basics.Int -> String

Encode a number using the provided salt.

This convenience function creates a context with the default alphabet. If the same context is used repeatedly, use 'encode' with one of the constructors instead.

encodeListUsingSalt : String -> List Basics.Int -> String

Encode a list of numbers using the provided salt.

This function wrapper creates a context with the default alphabet. If the same context is used repeatedly, use 'encodeList' with one of the constructors instead.

decodeUsingSalt : String -> String -> List Basics.Int

Decode a hash using the provided salt.

This convenience function creates a context with the default alphabet. If the same context is used repeatedly, use 'decode' with one of the constructors instead.

encodeHexUsingSalt : String -> String -> String

Shortcut for 'encodeHex'.

decodeHexUsingSalt : String -> String -> String

Shortcut for 'decodeHex'.