License | BSD-style |
---|---|
Maintainer | Vincent Hanquez <vincent@snarc.org> |
Stability | experimental |
Portability | unknown |
Safe Haskell | None |
Language | Haskell2010 |
Base conversions for ByteArray
.
Synopsis
- convertToBase :: (ByteArrayAccess bin, ByteArray bout) => Base -> bin -> bout
- convertFromBase :: (ByteArrayAccess bin, ByteArray bout) => Base -> bin -> Either String bout
- data Base
Documentation
convertToBase :: (ByteArrayAccess bin, ByteArray bout) => Base -> bin -> bout Source #
Encode some bytes to the equivalent representation in a specific Base
.
Examples
Convert a ByteString
to base-64:
>>>
convertToBase Base64 ("foobar" :: ByteString) :: ByteString
"Zm9vYmFy"
convertFromBase :: (ByteArrayAccess bin, ByteArray bout) => Base -> bin -> Either String bout Source #
Try to decode some bytes from the equivalent representation in a specific Base
.
Examples
Successfully convert from base-64 to a ByteString
:
>>>
convertFromBase Base64 ("Zm9vYmFy" :: ByteString) :: Either String ByteString
Right "foobar"
Trying to decode invalid data will return an error string:
>>>
convertFromBase Base64 ("!!!" :: ByteString) :: Either String ByteString
Left "base64: input: invalid length"
The different bases that can be used.
See RFC4648 for details. In particular, Base64 can be standard or URL-safe. URL-safe encoding is often used in other specifications without padding characters.
Examples
A quick example to show the differences:
>>>
let input = "Is 3 > 2?" :: ByteString
>>>
let convertedTo base = convertToBase base input :: ByteString
>>>
convertedTo Base16
"49732033203e20323f">>>
convertedTo Base32
"JFZSAMZAHYQDEPY=">>>
convertedTo Base64
"SXMgMyA+IDI/">>>
convertedTo Base64URLUnpadded
"SXMgMyA-IDI_">>>
convertedTo Base64OpenBSD
"QVKeKw.8GBG9"
Base16 | similar to hexadecimal |
Base32 | |
Base64 | standard Base64 |
Base64URLUnpadded | unpadded URL-safe Base64 |
Base64OpenBSD | Base64 as used in OpenBSD password encoding (such as bcrypt) |