elm-cardano/base64/1.0.1

Base64

Fast Base64 encoding and decoding for Bytes.

  • fromBytes encodes Bytes into a Base64 String.
  • toBytes decodes a Base64 String into Bytes, with validation.
  • toBytesUnsafe decodes a Base64 String into Bytes, without validation.

Encoding

fromBytes : Bytes -> String

Encode Bytes to a Base64 string.

import Base64
import Bytes.Encode as Encode


Encode.encode (Encode.sequence [ Encode.unsignedInt8 102, Encode.unsignedInt8 111, Encode.unsignedInt8 111 ])
    |> Base64.fromBytes
--> "Zm9v"


Encode.encode (Encode.sequence [])
    |> Base64.fromBytes
--> ""

Decoding

toBytes : String -> Maybe Bytes

Decode a Base64 string to Bytes, returning Nothing if the input contains invalid characters or has an impossible length (e.g. 1 mod 4).

import Base64
import Bytes


Base64.toBytes "Zm9v"
    |> Maybe.map Bytes.width
--> Just 3


Base64.toBytes "not valid!!"
--> Nothing
toBytesUnsafe : String -> Bytes

Decode a Base64 string to Bytes without validating the input.

This is faster than toBytes but will produce garbage output if the input is not a valid Base64 string. Use this when you trust the input (e.g. it was produced by fromBytes).

import Base64
import Bytes


Base64.toBytesUnsafe "Zm9v"
    |> Bytes.width
--> 3