dasch / crockford / Crockford

Encode integers as Crockford-style base32 strings.

From the specification:

Base 32 is a textual 32-symbol notation for expressing numbers in a form that can be conveniently and accurately transmitted between humans and computer systems. It can be used for out of band communication of public keys.

The encoding scheme is required to

This package provides functions for encoding and decoding base32 data.

Encoding & decoding

encode : Basics.Int -> Result Error String

Encode an integer as a base32 string.

Crockford.encode 1337 --> Ok "19S" : Result Crockford.Error String

decode : String -> Result Error Basics.Int

Decode a base32 string to an integer.

Crockford.decode "19S" --> Ok 1337 : Result Crockford.Error Int


type Error
    = NegativeNumber
    | InvalidChecksum
    | InvalidCharacter Char
    | EmptyString

Encoding or decoding can fail in a couple of ways:

Checksums

You can optionally insert a checksum at the end of the encoded data.

This allows validating the correctness of the string at a later point. For example, you may want to allow people to communicate the string over the phone, which obviously introduces a source of error. Crockford's base32 is optimized for communication, avoiding characters that look the same, but it's still possible for characters to be swapped or omitted during communication. By encoding with a checksum, and later decoding with a checksum again, you can validate that the string has not been modified and therefore corrupted.

encodeWithChecksum : Basics.Int -> Result Error String

Like encode, but appends a checksum character to the end of the string.

Crockford.encodeWithChecksum 32 --> Ok "10*" : Result Crockford.Error String

Crockford.decodeWithChecksum "10*" --> Ok 32 : Result Crockford.Error Int

Crockford.decodeWithChecksum "10~" --> Err InvalidChecksum : Result Crockford.Error Int

decodeWithChecksum : String -> Result Error Basics.Int

Like decode, but expects a checksum character at the end of the string.

See encodeWithChecksum for more information.