The buffer-based encoding utility method for node.js.
Following types of encodings are supported:
| Name | Description | Binary-Safe |
|---|---|---|
base64 |
The standard BASE64 encoding. | Yes |
base64url |
The URL-safe BASE64 encoding. | Yes |
base62x |
The BASE62x encoding. | Yes |
base32 |
The standard BASE32 encoding. | Yes |
hex |
The hexadecimal encoding. | Yes |
urlencode |
The purely URL-safe encoding. | Yes |
strict_uri |
The extended URL-safe encoding. | No |
This is a real urlencode implement, it will escape all bytes as
%xxformat, exceptingA-Z,a-zand0-9. This is a binary-safe encoding.Binary-safe means it could work with binary data perfectly, without gibberish. And it's all the same below.
This is a simple reference of method
encodeURIComponent, so it works as methodencodeURIComponentdoes. And it's not a binary-safe encoding.This is based on
uri, but all special chars including"-",".","_","!","*","(",")","~","'"will be escaped. However, it's still not a binary-safe encoding.NOTE: The data of
strict_uriencoding, could be simply decoded by functiondecodeURIComponent.This is based on
base64, while charactors"=","+","/"will be replaced with URL-safe charactors. This is a binary-safe encoding.This is a variation of
base64. And it is a binary-safe encoding.
npm i @litert/encodings -Simport * as Enc from "@litert/encodings";
const hex = Enc.stringToHex("hello world"); // Encode a string into hex
const b62 = Enc.stringToBase62x("hello world"); // Encode a string into BASE62x
const b64 = Enc.stringToBase64("hello world"); // Encode a string into BASE64
const b64url = Enc.stringToBase64Url("hello world"); // Encode a string into BASE64URL
const b32 = Enc.stringToBase32("hello world"); // Encode a string into BASE32
const url = Enc.stringToUrlencode("hello world"); // Encode a string into URL
const strict = Enc.stringToStrictUri("hello world"); // Encode a string into Strict-URI
// Also, buffer is accepted as input.
Enc.bufferToHex(Buffer.from("hello world"));
Enc.bufferToBase62x(Buffer.from("hello world"));
Enc.bufferToBase64(Buffer.from("hello world"));
Enc.bufferToBase64Url(Buffer.from("hello world"));
Enc.bufferToBase32(Buffer.from("hello world"));
Enc.bufferToUrlencode(Buffer.from("hello world"));
Enc.bufferToStrictUri(Buffer.from("hello world"));
Enc.stringFromHex(hex); // Decode a hex-encoded data as a string
Enc.stringFromBase62x(b62); // Decode a BASE62x-encoded data as a string
Enc.stringFromBase64(b64); // Decode a BASE64-encoded data as a string
Enc.stringFromBase64Url(b64url); // Decode a BASE64URL-encoded data as a string
Enc.stringFromBase32(b32); // Decode a BASE32-encoded data as a string
Enc.stringFromUrlencode(url); // Decode a urlencoded data as a string
Enc.stringFromStrictUri(strict); // Decode a Strict-URI-encoded data as a string
Enc.bufferFromHex(hex);
Enc.bufferFromBase62x(b62);
Enc.bufferFromBase64(b64);
Enc.bufferFromBase64Url(b64url);
Enc.bufferFromBase32(b32);
Enc.bufferFromUrlencode(url);
Enc.bufferFromStrictUri(strict);This library is published under Apache-2.0 license.