{"id":13777418,"url":"https://github.com/spacewander/lua-resty-base-encoding","last_synced_at":"2025-04-15T03:32:00.868Z","repository":{"id":71091872,"uuid":"115904722","full_name":"spacewander/lua-resty-base-encoding","owner":"spacewander","description":"A faster alternative to base64 encoding and provides missing base encoding for OpenResty application","archived":false,"fork":false,"pushed_at":"2021-08-05T09:26:35.000Z","size":47,"stargazers_count":15,"open_issues_count":0,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-02-14T20:39:41.453Z","etag":null,"topics":["base16","base2","base32","base64","base85","encoding","hexadecimal-format","lua-resty","oepnresty"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spacewander.png","metadata":{"files":{"readme":"README.md","changelog":null,"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}},"created_at":"2018-01-01T07:09:56.000Z","updated_at":"2022-07-21T10:41:40.000Z","dependencies_parsed_at":"2023-03-06T01:30:22.782Z","dependency_job_id":null,"html_url":"https://github.com/spacewander/lua-resty-base-encoding","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spacewander%2Flua-resty-base-encoding","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spacewander%2Flua-resty-base-encoding/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spacewander%2Flua-resty-base-encoding/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spacewander%2Flua-resty-base-encoding/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spacewander","download_url":"https://codeload.github.com/spacewander/lua-resty-base-encoding/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249001554,"owners_count":21196397,"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":["base16","base2","base32","base64","base85","encoding","hexadecimal-format","lua-resty","oepnresty"],"created_at":"2024-08-03T18:00:43.094Z","updated_at":"2025-04-15T03:32:00.627Z","avatar_url":"https://github.com/spacewander.png","language":"C","readme":"# Name\n\nlua-resty-base-encoding - Faster alternative to base64 encoding and provides missing base encoding for OpenResty application\n\nAll encoding are implemented in optimized C code with LuaJIT FFI binding.\n\nMost of the inner encoding implementations are from Nick Galbreath's [stringencoders](https://github.com/client9/stringencoders).\nThe base32 encoding is implemented by myself, but also inspired from his art work.\n\nBuild status: [![Travis](https://travis-ci.org/spacewander/lua-resty-base-encoding.svg?branch=master)](https://travis-ci.org/spacewander/lua-resty-base-encoding)\n\nTable of Contents\n=================\n\n* [Name](#name)\n* [MUST READ](#must-read)\n* [Synopsis](#synopsis)\n* [Installation](#installation)\n* [Benchmark](#benchmark)\n* [Methods](#methods)\n    * [encode_base2](#encode_base2)\n    * [decode_base2](#decode_base2)\n    * [encode_base16](#encode_base16)\n    * [decode_base16](#decode_base16)\n    * [encode_base32](#encode_base32)\n    * [decode_base32](#decode_base32)\n    * [encode_base32hex](#encode_base32hex)\n    * [decode_base32hex](#decode_base32hex)\n    * [encode_base64](#encode_base64)\n    * [decode_base64](#decode_base64)\n    * [encode_base64url](#encode_base64url)\n    * [decode_base64url](#decode_base64url)\n    * [encode_base85](#encode_base85)\n    * [decode_base85](#decode_base85)\n\n## MUST READ\n\n* The base64 encoding algorithm is ENDIAN DEPENDENT. The default version only works\n  with little endian. To compile the big endian version, run `make CEXTRAFLAGS=\"-DWORDS_BIGENDIAN\"` instead.\n* The base64 encoding algorithm assumes input string is ALIGNED, so it could be used only on x86(x64) and modern ARM architecture.\n\n## Synopsis\n\n```lua\nlocal base_encoding = require \"resty.base_encoding\"\nlocal raw = \"0123456789\"\n\n-- base32\nlocal encoded = base_encoding.encode_base32(raw)\n-- Or without '=' padding: local encoded = base_encoding.encode_base32(raw, true)\nbase_encoding.decode_base32(encoded) -- 0123456789\n\n-- base64/base64_url (drop-in alternative to official API from lua-resty-core)\nbase_encoding.encode_base64(raw)\nbase_encoding.decode_base64(encoded)\nbase_encoding.encode_base64url(raw)\nbase_encoding.decode_base64url(encoded)\n```\n\nFor more examples, read the `t/base*.t` files.\n\n## Installation\n\nRun `make`. Then copy the `librestybaseencoding.so` to one of your `lua_package_cpath`.\nYes, this library uses a trick to load the shared object from cpath instead of system shared library path.\nFinally, add the `$pwd/lib` to your `lua_package_path`.\n\n[Back to TOC](#table-of-contents)\n\n## Benchmark\n\nIn short, it is quite faster than pure Lua alternatives, and a little faster than other C/FFI implementation.\nGo to [Benchmark](benchmark/readme.md) page for the details.\n\n[Back to TOC](#table-of-contents)\n\n## Methods\n\n### encode_base2\n`syntax: encoded = encode_base2(raw)`\n\nEncode given string into base2 format(aka. bin format). Note that the input is string.\nTherefore, the `encode_base2` result of `1` is `00110001`, because the ascii value of `1` is 49, and\nthe binary format of 49 is `00110001`. And don't forget that the output of `encode_base2` is a string\ninstead of a binary number.\n\n[Back to TOC](#table-of-contents)\n\n### decode_base2\n`syntax: raw, err = decode_base2(encoded)`\n\nDecode base2 format string into its raw value.\nIf the given string is not valid base2 encoded, the `raw` will be `nil` and `err` will be `\"invalid input\"`.\nAny character in the input string which is not `1` will be considered as `0`. For example, `aa11aaa1` is equal\nto `00110001`. There is no RFC requires we should treat character not in `0` and `1` as invalid input, and\ncheck if a character is '0' or not will slow the performance down by 50%.\n\n[Back to TOC](#table-of-contents)\n\n### encode_base16\n`syntax: encoded = encode_base16(raw[, out_in_lowercase])`\n\nEncode given string into base16 format(aka. hex/hexadecimal format).\nThis method may be named `to_hex` or `encodeHex` in other languages.\nThe default output letters are in `[0-9A-F]`. If you specify the `out_in_lowercase` to `true`, the output will be in `[0-9a-f]`.\n\n[Back to TOC](#table-of-contents)\n\n### decode_base16\n`syntax: raw, err = decode_base16(encoded)`\n\nDecode base16 format(aka. hex/hexadecimal format) string into its raw value.\nThis method may be named `from_hex` or `decodeHex` in other languages.\nIf the given string is not valid base16 encoded, the `raw` will be `nil` and `err` will be `\"invalid input\"`.\nLetters in `[0-9a-fA-F]` are considered valid.\n\n[Back to TOC](#table-of-contents)\n\n### encode_base32\n`syntax: encoded = encode_base32(raw[, no_padding])`\n\nEncode given string into base32 format with/without padding '='. The default value of `no_padding` is false.\n\n[Back to TOC](#table-of-contents)\n\n### decode_base32\n`syntax: raw, err = decode_base32(encoded)`\n\nDecode base32 format string into its raw value. If the given string is not valid base32 encoded, the `raw` will be `nil` and `err` will be `\"invalid input\"`.\n\n[Back to TOC](#table-of-contents)\n\n### encode_base32hex\n`syntax: encoded = encode_base32hex(raw[, no_padding])`\n\nEncode given string into base32hex format with/without padding '='. The default value of `no_padding` is false.\nFor more info of base32hex format, see https://tools.ietf.org/html/rfc4648#section-7.\n\n[Back to TOC](#table-of-contents)\n\n### decode_base32hex\n`syntax: raw, err = decode_base32(encoded)`\n\nDecode base32hex format string into its raw value. If the given string is not valid base32hex encoded, the `raw` will be `nil` and `err` will be `\"invalid input\"`.\nFor more info of base32hex format, see https://tools.ietf.org/html/rfc4648#section-7.\n\n[Back to TOC](#table-of-contents)\n\n### encode_base64\n### decode_base64\n### encode_base64url\n### decode_base64url\n\nDrop-in alternative to the official implementation in lua-resty-core. Read their official documentation instead.\nThe encode method is 40% faster, and the decode method is 200% faster. Note that the implementation is endian and architecture dependent.\nRead the 'Must Read' section for more info.\n\n[Back to TOC](#table-of-contents)\n\n### encode_base85\n`syntax: encoded = encode_base85(raw)`\n\nEncode given string into base85 format with/without padding '='.\nNote that there is not a standard but too many variants of so-called base85.\nThis module's implementation should be compatiable with Go's encoding/ascii85\nmodule (not in the level of API argument, but in the level of encode/decode rules).\n\n[Back to TOC](#table-of-contents)\n\n### decode_base85\n`syntax: raw, err = decode_base85(encoded)`\n\nDecode base85 format string into its raw value. If the given string is not valid base85 encoded, the `raw` will be `nil` and `err` will be `\"invalid input\"`.\n\n[Back to TOC](#table-of-contents)\n\n","funding_links":[],"categories":["Libraries"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspacewander%2Flua-resty-base-encoding","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspacewander%2Flua-resty-base-encoding","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspacewander%2Flua-resty-base-encoding/lists"}