{"id":14066140,"url":"https://github.com/extendr/b64","last_synced_at":"2025-04-21T08:32:18.961Z","repository":{"id":216685757,"uuid":"741954064","full_name":"extendr/b64","owner":"extendr","description":"A lightweight and very fast base64 encoder and decoder. ","archived":false,"fork":false,"pushed_at":"2025-04-14T22:18:58.000Z","size":11704,"stargazers_count":17,"open_issues_count":1,"forks_count":2,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-21T03:04:09.332Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://extendr.github.io/b64/","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/extendr.png","metadata":{"files":{"readme":"README.Rmd","changelog":"NEWS.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-11T13:06:49.000Z","updated_at":"2025-04-14T22:17:09.000Z","dependencies_parsed_at":"2024-01-20T16:43:25.713Z","dependency_job_id":null,"html_url":"https://github.com/extendr/b64","commit_stats":null,"previous_names":["extendr/b64"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extendr%2Fb64","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extendr%2Fb64/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extendr%2Fb64/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extendr%2Fb64/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/extendr","download_url":"https://codeload.github.com/extendr/b64/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249990400,"owners_count":21357065,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-13T07:04:57.410Z","updated_at":"2025-04-21T08:32:18.939Z","avatar_url":"https://github.com/extendr.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n\n# b64 \u003cimg src=\"man/figures/logo.svg\" align=\"right\" height=\"139\" alt=\"\" /\u003e\n\n\u003c!-- badges: start --\u003e\n[![R-CMD-check](https://github.com/extendr/b64/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/extendr/b64/actions/workflows/R-CMD-check.yaml)\n\u003c!-- badges: end --\u003e\n\nb64 is a dependency free, fast, lightweight, and vectorized base64 encoder and decoder.\n\n## Installation\n\nYou can install b64 from CRAN with\n\n```r\ninstall.packages(\"b64\")\n```\n\n## Example\n\nEncode to base64 using `encode()`.\n\n```{r example}\nlibrary(b64)\n\nhello \u003c- encode(\"Hello, from extendr\")\nhello\n```\n\nDecode using `decode()`. Note that the returned object will always have the `\"blob\"` class. To achieve 0 dependencies, the `blob` package is only listed as a suggested dependency but if you attach it, its print method will be used.\n\n```{r}\nlibrary(blob)\ndecoded \u003c- decode(hello)\ndecoded\n```\n\nWe can convert the decoded base64 to characters and see how it worked.\n\n```{r}\nrawToChar(decoded[[1]])\n```\n\n\n### Vectorized \n\nBoth `encode()` and `decode()` are vectorized. \n\n```{r}\nlorem \u003c- unlist(lorem::ipsum(5, 1,  5))\nlorem\n\nencoded \u003c- encode(lorem)\nencoded\n```\n\nWe can decode all of these using `decode()` as well.\n\n```{r}\ndecode(encoded)\n```\n\n\n## Encoding and decoding files\n\n`b64` shines when encoding and decoding files. `encode_file()` and `decode_file()` both work by reading a file as a stream making it far faster than the alternative.\n\n```{r message = FALSE, warn = FALSE}\ntmp \u003c- tempfile() \nfp \u003c- \"https://github.com/extendr/b64/blob/main/src/rust/vendor.tar.xz\"\n\ndownload.file(fp, tmp)\n\nbench::mark(\n  b64 = encode_file(tmp),\n  base64enc = base64enc::base64encode(tmp)\n)\n```\n\nWhile the encoding is very impressive, better yet is the decoding performance. \n\n```{r}\n# create a temp file\ntmp2 \u003c- tempfile()\n\n# encode it and write to tmep file\nencode_file(tmp) |\u003e\n  charToRaw() |\u003e\n  writeBin(tmp2)\n\nbench::mark(\n  b64 = decode_file(tmp2),\n  base64enc = base64enc::base64decode(file(tmp2))\n)\n```\n\n## Alternative engines \n\nOut of the box, `b64` provides a number of pre-configured engines that can be used. The function `engine()` allows you to choose one of these different engines For example, `engine(\"url_safe\")` provides a standard engine that uses a url-safe alphabet with padding.\n\n\n```{r}\nurl_engine \u003c- engine(\"url_safe\")\nurl_safe_encoded \u003c- encode(\"\\xfa\\xec U\", url_engine)\nurl_safe_encoded\n```\n\nIf we try to decode this using the standard engine, we will encounter an error. \n\n```{r error=TRUE}\ndecode(url_safe_encoded)\n```\n\nWe can use our new engine to decode it.\n\n```{r}\ndecode(url_safe_encoded, url_engine)\n```\n\n### Custom Engines\n\nWe can create custom engines with `new_engine()`. This allows us to provide our on alphabet and configuration. \n\nWe can use one of the many predefined alphabets or create one our selves with `new_alphabet()`. We can also specify our engine config using `new_config()` which lets us choose whether or not to pad and how to handle decoding. \n\n```{r}\nmy_eng \u003c- new_engine(\n  alphabet(\"crypt\"),\n  new_config(TRUE, TRUE, \"none\")\n)\n```\n\nThis engine can be used to encode or decode text. \n\n```{r}\ntxt \u003c- \"lorem ipsum sit dolor amet\"\n\nencode(txt, my_eng)\n```\n\nCompare this to the standard encoder: \n\n```{r}\nencode(txt)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fextendr%2Fb64","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fextendr%2Fb64","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fextendr%2Fb64/lists"}