Title: | Fast and Vectorized Base 64 Engine |
---|---|
Description: | Provides a fast, lightweight, and vectorized base 64 engine to encode and decode character and raw vectors as well as files stored on disk. Common base 64 alphabets are supported out of the box including the standard, URL-safe, bcrypt, crypt, 'BinHex', and IMAP-modified UTF-7 alphabets. Custom engines can be created to support unique base 64 encoding and decoding needs. |
Authors: | Josiah Parry [aut, cre] , Etienne Bacher [ctb] |
Maintainer: | Josiah Parry <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.3 |
Built: | 2024-10-26 06:12:30 UTC |
Source: | https://github.com/extendr/b64 |
Create an alphabet from a set of standard base64 alphabets, or use your own.
alphabet(which = "standard") new_alphabet(chars)
alphabet(which = "standard") new_alphabet(chars)
which |
default |
chars |
a character scalar contains 64 unique characters. |
"bcrypt"
: bcrypt alphabet
"bin_hex"
: alphabet used in BinHex 4.0 files
"crypt"
: crypt(3) alphabet (with . and / as the first two characters)
"imap_mutf7"
: alphabet used in IMAP-modified UTF-7 (with + and ,)
"standard"
: standard alphabet (with + and /) specified in RFC 4648
"url_safe"
: URL-safe alphabet (with - and _) specified in RFC 4648
See base64 crate from where these definitions come.
an object of class alphabet
alphabet("standard") alphabet("bcrypt") alphabet("bin_hex") alphabet("crypt") alphabet("imap_mutf7") alphabet("url_safe") new_alphabet("qwertyuiop[]asdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890")
alphabet("standard") alphabet("bcrypt") alphabet("bin_hex") alphabet("crypt") alphabet("imap_mutf7") alphabet("url_safe") new_alphabet("qwertyuiop[]asdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890")
Functions to perform common tasks when working with base64 encoded strings.
b64_chunk(encoded, width) b64_wrap(chunks, newline)
b64_chunk(encoded, width) b64_wrap(chunks, newline)
encoded |
a character vector of base64 encoded strings. |
width |
a numeric scalar defining the width of the chunks. Must be divisible by 4. |
chunks |
a character vector of base64 encoded strings. |
newline |
a character scalar defining the newline character. |
b64_chunk()
splits a character vector of base64 encoded strings into chunks of a
specified width.
b64_wrap()
wraps a character vector of base64 encoded strings with a newline character.
b64_chunk()
returns a list of character vectors.
b64_wrap()
returns a scalar character vector.
encoded <- encode("Hello, world!") chunked <- b64_chunk(encoded, 4) chunked b64_wrap(chunked, "\n")
encoded <- encode("Hello, world!") chunked <- b64_chunk(encoded, 4) chunked b64_wrap(chunked, "\n")
Encode and decode using base64
encode(what, eng = engine()) decode(what, eng = engine()) encode_file(path, eng = engine()) decode_file(path, eng = engine())
encode(what, eng = engine()) decode(what, eng = engine()) encode_file(path, eng = engine()) decode_file(path, eng = engine())
what |
a character, raw, or blob vector |
eng |
a base64 engine. See |
path |
a path to a base64 encoded file. |
Both encode()
and decode()
are vectorized. They will return a character
and blob vector the same length as what
, respectively.
# encode hello world encoded <- encode("Hello world") encoded # decode to a blob decoded <- decode(encoded) decoded # convert back to a character rawToChar(decoded[[1]])
# encode hello world encoded <- encode("Hello world") encoded # decode to a blob decoded <- decode(encoded) decoded # convert back to a character rawToChar(decoded[[1]])
Create an encoding engine
engine(which = "standard") new_engine(.alphabet = alphabet(), .config = new_config())
engine(which = "standard") new_engine(.alphabet = alphabet(), .config = new_config())
which |
default |
.alphabet |
an object of class |
.config |
an object of class |
By default, the "standard" base64 engine is used which is specified in RFC 4648.
Additional pre-configured base64 engines are provided these are:
"standard_no_pad"
: uses the standard engine without padding
"url_safe"
: uses a url-safe alphabet with padding
"url_safe_no_pad"
: uses a url-safe alphabet without padding
See base64 crate for more.
an object of class engine
.
engine() new_engine(alphabet("bcrypt"), new_config())
engine() new_engine(alphabet("bcrypt"), new_config())
Create a custom encoding engine
new_config( encode_padding = TRUE, decode_padding_trailing_bits = FALSE, decode_padding_mode = c("canonical", "indifferent", "none") )
new_config( encode_padding = TRUE, decode_padding_trailing_bits = FALSE, decode_padding_mode = c("canonical", "indifferent", "none") )
encode_padding |
default |
decode_padding_trailing_bits |
default |
decode_padding_mode |
default |
See base64 crate for more details.
There are three modes that can be used for decode_padding_mode
argument.
"canonical"
: padding must consist of 0, 1, or 2 =
characters
"none"
: there must be no padding characters present
"indifferent"
: canonical padding is used, but omitted padding
characters are also permitted
an object of class engine_config
# create a new nonsensicle config new_config(FALSE, TRUE, "none")
# create a new nonsensicle config new_config(FALSE, TRUE, "none")