Copyright | (c) 2012 Magnus Therning |
---|---|
License | BSD3 |
Safe Haskell | None |
Language | Haskell2010 |
Implemented as specified in RFC 4648 (http://tools.ietf.org/html/rfc4648).
Base32 encoding works by expanding blocks of 5 bytes of data into blocks of 8 bytes of data. Finally it also includes a well defined ending of the encoded data to make sure the size of the final block of encoded data is 8 bytes too.
Synopsis
- b32EncodePart :: ByteString -> (ByteString, ByteString)
- b32EncodeFinal :: ByteString -> Maybe ByteString
- b32DecodePart :: ByteString -> Either (ByteString, ByteString) (ByteString, ByteString)
- b32DecodeFinal :: ByteString -> Maybe ByteString
- encode :: ByteString -> ByteString
- decode :: ByteString -> Either (ByteString, ByteString) ByteString
Documentation
b32EncodePart :: ByteString -> (ByteString, ByteString) Source #
Encoding function.
This function encodes as large a portion of the input as possible and
returns the encoded part together with the remaining part. Enough space is
allocated for the encoding to make sure that the remaining part is less than
5 bytes long, which means it can be passed to b32_encode_final
as is.
>>>
b32EncodePart $ Data.ByteString.Char8.pack "fooba"
("MZXW6YTB","")>>>
b32EncodePart $ Data.ByteString.Char8.pack "foobar"
("MZXW6YTB","r")
b32EncodeFinal :: ByteString -> Maybe ByteString Source #
Encoding function for the final block.
The final block has to have a size less than 5.
>>>
b32EncodeFinal $ Data.ByteString.Char8.pack "r"
Just "OI======"
Trying to pass in too large a block result in failure:
>>>
b32EncodeFinal $ Data.ByteString.Char8.pack "fooba"
Nothing
b32DecodePart :: ByteString -> Either (ByteString, ByteString) (ByteString, ByteString) Source #
Decoding function.
Decode as large a portion of the input as possible. Enough data is
allocated for the output to ensure that the remainder is less than 8 bytes
in size. Success result in a Right
value:
>>>
b32DecodePart $ Data.ByteString.Char8.pack "MZXW6YTB"
Right ("fooba","")>>>
b32DecodePart $ Data.ByteString.Char8.pack "MZXW6YTBOI======"
Right ("fooba","OI======")
Failures occur on bad input and result in a Left
value:
>>>
b32DecodePart $ Data.ByteString.Char8.pack "M=XW6YTB"
Left ("","M=XW6YTB")
b32DecodeFinal :: ByteString -> Maybe ByteString Source #
Decoding function for the final block.
The final block has to have a size of 0 or 8:
>>>
b32DecodeFinal $ Data.ByteString.Char8.pack "MZXW6YQ="
Just "foob">>>
b32DecodeFinal $ Data.ByteString.Char8.pack ""
Just "">>>
b32DecodeFinal $ Data.ByteString.Char8.pack "MZXW6Y="
Nothing
But it must be the encoding of a block that is less than 5 bytes:
>>>
b32DecodeFinal $ encode $ Data.ByteString.Char8.pack "fooba"
Nothing
encode :: ByteString -> ByteString Source #
Convenience function that combines b32_encode_part
and
b32_encode_final
to encode a complete string.
>>>
encode $ Data.ByteString.Char8.pack "fooba"
"MZXW6YTB">>>
encode $ Data.ByteString.Char8.pack "foobar"
"MZXW6YTBOI======"
decode :: ByteString -> Either (ByteString, ByteString) ByteString Source #
Convenience function that combines b32_decode_part
and
b32_decode_final
to decode a complete string.
>>>
decode $ Data.ByteString.Char8.pack "MZXW6YTB"
Right "fooba">>>
decode $ Data.ByteString.Char8.pack "MZXW6YTBOI======"
Right "foobar"
Failures when decoding returns the decoded part and the remainder:
>>>
decode $ Data.ByteString.Char8.pack "MZXW6YTBOI=0===="
Left ("fooba","OI=0====")