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
- Be human readable and machine readable.
- Be compact. Humans have difficulty in manipulating long strings of arbitrary symbols.
- Be error resistant. Entering the symbols must not require keyboarding gymnastics.
- Be pronounceable. Humans should be able to accurately transmit the symbols to other humans using a telephone.
This package provides functions for encoding and decoding base32 data.
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
Encoding or decoding can fail in a couple of ways:
NegativeNumber
means you tried to encode a negative integer, which isn't supported.InvalidChecksum
means you tried to decode a base32 string with a checksum, but the checksum didn't match.InvalidCharacter
means you tried to decode a base32 string, but an invalid character was encountered.EmptyString
means you tried to decode an empty string.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.