├── .eslintignore ├── .github ├── FUNDING.yml └── workflows │ └── publish.yaml ├── .gitignore ├── docs ├── favicon.ico ├── og-logo.jpg ├── pages │ ├── reference │ │ ├── index.md │ │ └── main │ │ │ ├── encodeHexLowerCase.md │ │ │ ├── encodeHexUpperCase.md │ │ │ ├── encodeBase64.md │ │ │ ├── encodeBase64url.md │ │ │ ├── encodeBase64NoPadding.md │ │ │ ├── decodeHex.md │ │ │ ├── encodeBase32LowerCase.md │ │ │ ├── encodeBase32UpperCase.md │ │ │ ├── encodeBase64urlNoPadding.md │ │ │ ├── decodeBase64.md │ │ │ ├── encodeBase32LowerCaseNoPadding.md │ │ │ ├── encodeBase32UpperCaseNoPadding.md │ │ │ ├── encodeBase32.md │ │ │ ├── decodeBase64url.md │ │ │ ├── decodeBase32IgnorePadding.md │ │ │ ├── decodeBase64IgnorePadding.md │ │ │ ├── encodeBase32NoPadding.md │ │ │ ├── decodeBase32.md │ │ │ ├── decodeBase64urlIgnorePadding.md │ │ │ └── index.md │ ├── examples │ │ ├── hex.md │ │ ├── base32.md │ │ └── base64.md │ └── index.md ├── malta.config.json └── logo.svg ├── .prettierrc.json ├── .prettierignore ├── tsconfig.build.json ├── tsconfig.json ├── src ├── index.ts ├── hex.test.ts ├── hex.ts ├── base64.test.ts ├── base32.test.ts ├── base32.ts └── base64.ts ├── README.md ├── .eslintrc.cjs ├── CHANGELOG.md ├── LICENSE ├── package.json └── CONTRIBUTING.md /.eslintignore: -------------------------------------------------------------------------------- 1 | dist/** -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: pilcrowOnPaper 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | pnpm-lock.yaml 2 | dist 3 | node_modules 4 | package-lock.json -------------------------------------------------------------------------------- /docs/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oslo-project/encoding/HEAD/docs/favicon.ico -------------------------------------------------------------------------------- /docs/og-logo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oslo-project/encoding/HEAD/docs/og-logo.jpg -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "useTabs": true, 3 | "trailingComma": "none", 4 | "printWidth": 100 5 | } 6 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /dist 4 | 5 | pnpm-lock.yaml 6 | package-lock.json 7 | yarn.lock 8 | 9 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "include": ["src"], 4 | "exclude": ["src/**/*.test.ts"] 5 | } 6 | -------------------------------------------------------------------------------- /docs/pages/reference/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "API reference" 3 | --- 4 | 5 | # API reference 6 | 7 | ## Modules 8 | 9 | - [`main`](/reference/main) 10 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "outDir": "dist", 4 | "declaration": true, 5 | "esModuleInterop": true, 6 | "skipLibCheck": true, 7 | "target": "es2022", 8 | "verbatimModuleSyntax": true, 9 | "allowJs": true, 10 | "resolveJsonModule": true, 11 | "moduleDetection": "force", 12 | "strict": true, 13 | "moduleResolution": "NodeNext", 14 | "module": "NodeNext" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /docs/pages/reference/main/encodeHexLowerCase.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "encodeHexLowerCase()" 3 | --- 4 | 5 | # encodeHexLowerCase() 6 | 7 | Encodes data into hex with lower case letters based on [RFC 4648 §8](https://datatracker.ietf.org/doc/html/rfc4648#section-8). 8 | 9 | ## Definition 10 | 11 | ```ts 12 | function encodeHexLowerCase(data: Uint8Array): string; 13 | ``` 14 | 15 | ### Parameters 16 | 17 | - `data` 18 | -------------------------------------------------------------------------------- /docs/pages/reference/main/encodeHexUpperCase.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "encodeHexUpperCase()" 3 | --- 4 | 5 | # encodeHexUpperCase() 6 | 7 | Encodes data into hex with upper case letters based on [RFC 4648 §8](https://datatracker.ietf.org/doc/html/rfc4648#section-8). 8 | 9 | ## Definition 10 | 11 | ```ts 12 | function encodeHexUpperCase(data: Uint8Array): string; 13 | ``` 14 | 15 | ### Parameters 16 | 17 | - `data` 18 | -------------------------------------------------------------------------------- /docs/pages/reference/main/encodeBase64.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "encodeBase64()" 3 | --- 4 | 5 | # encodeBase64() 6 | 7 | Encodes a byte array into [canonical](https://datatracker.ietf.org/doc/html/rfc4648#autoid-8) base64 based on [RFC 4648 §4](https://datatracker.ietf.org/doc/html/rfc4648#autoid-9) with padding. 8 | 9 | ## Definition 10 | 11 | ```ts 12 | function encodeBase64(bytes: Uint8Array): string; 13 | ``` 14 | 15 | ### Parameters 16 | 17 | - `bytes` 18 | -------------------------------------------------------------------------------- /docs/pages/reference/main/encodeBase64url.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "encodeBase64url()" 3 | --- 4 | 5 | # encodeBase64url() 6 | 7 | Encodes a byte array into [canonical](https://datatracker.ietf.org/doc/html/rfc4648#autoid-8) base64 with url-safe alphabet based on [RFC 4648 §5](https://datatracker.ietf.org/doc/html/rfc4648#autoid-10) with padding. 8 | 9 | ## Definition 10 | 11 | ```ts 12 | function encodeBase64url(bytes: Uint8Array): string; 13 | ``` 14 | 15 | ### Parameters 16 | 17 | - `bytes` 18 | -------------------------------------------------------------------------------- /docs/pages/reference/main/encodeBase64NoPadding.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "encodeBase64NoPadding()" 3 | --- 4 | 5 | # encodeBase64NoPadding() 6 | 7 | Encodes a byte array into [canonical](https://datatracker.ietf.org/doc/html/rfc4648#autoid-8) base64 based on [RFC 4648 §6](https://datatracker.ietf.org/doc/html/rfc4648#autoid-11) with the padding omitted. 8 | 9 | ## Definition 10 | 11 | ```ts 12 | function encodeBase64NoPadding(bytes: Uint8Array): string; 13 | ``` 14 | 15 | ### Parameters 16 | 17 | - `bytes` 18 | -------------------------------------------------------------------------------- /docs/pages/reference/main/decodeHex.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "decodeHex()" 3 | --- 4 | 5 | # decodeHex() 6 | 7 | Decodes hex-encoded strings based on [RFC 4648 §8](https://datatracker.ietf.org/doc/html/rfc4648#section-8). This method is case-insensitive and the string can be encoded either with lower case or upper case letters. Throws an `Error` if the hex string is malformed. 8 | 9 | ## Definition 10 | 11 | ```ts 12 | function decodeHex(data: string): Uint8Array; 13 | ``` 14 | 15 | ### Parameters 16 | 17 | - `data` 18 | -------------------------------------------------------------------------------- /docs/pages/reference/main/encodeBase32LowerCase.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "encodeBase32LowerCase()" 3 | --- 4 | 5 | # encodeBase32UpperCase() 6 | 7 | Encodes a byte array into [canonical](https://datatracker.ietf.org/doc/html/rfc4648#autoid-8) base32 based on [RFC 4648 §6](https://datatracker.ietf.org/doc/html/rfc4648#autoid-11) with lower case letters and padding. 8 | 9 | ## Definition 10 | 11 | ```ts 12 | function encodeBase32LowerCase(bytes: Uint8Array): string; 13 | ``` 14 | 15 | ### Parameters 16 | 17 | - `bytes` 18 | -------------------------------------------------------------------------------- /docs/pages/reference/main/encodeBase32UpperCase.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "encodeBase32UpperCase()" 3 | --- 4 | 5 | # encodeBase32UpperCase() 6 | 7 | Encodes a byte array into [canonical](https://datatracker.ietf.org/doc/html/rfc4648#autoid-8) base32 based on [RFC 4648 §6](https://datatracker.ietf.org/doc/html/rfc4648#autoid-11) with upper case letters and padding. 8 | 9 | ## Definition 10 | 11 | ```ts 12 | function encodeBase32UpperCase(bytes: Uint8Array): string; 13 | ``` 14 | 15 | ### Parameters 16 | 17 | - `bytes` 18 | -------------------------------------------------------------------------------- /docs/pages/reference/main/encodeBase64urlNoPadding.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "encodeBase64urlNoPadding()" 3 | --- 4 | 5 | # encodeBase64urlNoPadding() 6 | 7 | Encodes a byte array into [canonical](https://datatracker.ietf.org/doc/html/rfc4648#autoid-8) base64 with url-safe alphabet based on [RFC 4648 §5](https://datatracker.ietf.org/doc/html/rfc4648#autoid-10) with the padding omitted. 8 | 9 | ## Definition 10 | 11 | ```ts 12 | function encodeBase64urlNoPadding(bytes: Uint8Array): string; 13 | ``` 14 | 15 | ### Parameters 16 | 17 | - `bytes` 18 | -------------------------------------------------------------------------------- /docs/pages/reference/main/decodeBase64.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "decodeBase64()" 3 | --- 4 | 5 | # decodeBase64() 6 | 7 | Decodes a base64 encoded string into a byte array based on [RFC 4648 §4](https://datatracker.ietf.org/doc/html/rfc4648#autoid-9). The string must be [canonical](https://datatracker.ietf.org/doc/html/rfc4648#autoid-8) and include padding. Throws an `Error` if the encoding is invalid. 8 | 9 | ## Definition 10 | 11 | ```ts 12 | function decodeBase64(encoded: string): Uint8Array; 13 | ``` 14 | 15 | ### Parameters 16 | 17 | - `encoded` 18 | -------------------------------------------------------------------------------- /docs/pages/examples/hex.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Hex encoding" 3 | --- 4 | 5 | # Hex encoding 6 | 7 | Use `encodeHexUpperCase()` or `encodeHexLowerCase()` to encode data and `decodeHex()` to decode hex-encoded strings. `decodeHex()` is case-insensitive. 8 | 9 | ```ts 10 | import { encodeUpperCase, encodeHexLowerCase, decodeHex } from "@oslojs/encoding"; 11 | 12 | const data: Uint8Array = new TextEncoder().encode("hello world"); 13 | const hex = encodeHexUpperCase(data); 14 | const hex = encodeHexLowerCase(data); 15 | const decoded = decodeHex(hex); 16 | ``` 17 | -------------------------------------------------------------------------------- /docs/pages/reference/main/encodeBase32LowerCaseNoPadding.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "encodeBase32LowerCaseNoPadding()" 3 | --- 4 | 5 | # encodeBase32LowerCaseNoPadding() 6 | 7 | Encodes a byte array into [canonical](https://datatracker.ietf.org/doc/html/rfc4648#autoid-8) base32 based on [RFC 4648 §6](https://datatracker.ietf.org/doc/html/rfc4648#autoid-11) with lower case letters and the padding omitted. 8 | 9 | ## Definition 10 | 11 | ```ts 12 | function encodeBase32LowerCaseNoPadding(bytes: Uint8Array): string; 13 | ``` 14 | 15 | ### Parameters 16 | 17 | - `bytes` 18 | -------------------------------------------------------------------------------- /docs/pages/reference/main/encodeBase32UpperCaseNoPadding.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "encodeBase32UpperCaseNoPadding()" 3 | --- 4 | 5 | # encodeBase32UpperCaseNoPadding() 6 | 7 | Encodes a byte array into [canonical](https://datatracker.ietf.org/doc/html/rfc4648#autoid-8) base32 based on [RFC 4648 §6](https://datatracker.ietf.org/doc/html/rfc4648#autoid-11) with upper case letters and the padding omitted. 8 | 9 | ## Definition 10 | 11 | ```ts 12 | function encodeBase32UpperCaseNoPadding(bytes: Uint8Array): string; 13 | ``` 14 | 15 | ### Parameters 16 | 17 | - `bytes` 18 | -------------------------------------------------------------------------------- /docs/pages/reference/main/encodeBase32.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "encodeBase32()" 3 | --- 4 | 5 | # encodeBase32() 6 | 7 | _Replaced: Use [`encodeBase32UpperCase()`](/reference/main/encodeBase32UpperCase) instead._ 8 | 9 | Encodes a byte array into [canonical](https://datatracker.ietf.org/doc/html/rfc4648#autoid-8) base32 based on [RFC 4648 §6](https://datatracker.ietf.org/doc/html/rfc4648#autoid-11) with upper case letters and padding. 10 | 11 | ## Definition 12 | 13 | ```ts 14 | function encodeBase32(bytes: Uint8Array): string; 15 | ``` 16 | 17 | ### Parameters 18 | 19 | - `bytes` 20 | -------------------------------------------------------------------------------- /docs/pages/reference/main/decodeBase64url.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "decodeBase64url()" 3 | --- 4 | 5 | # decodeBase64url() 6 | 7 | Decodes a base64 encoded string with url-safe alphabet into a byte array based on [RFC 4648 §5](https://datatracker.ietf.org/doc/html/rfc4648#autoid-10). The string must be [canonical](https://datatracker.ietf.org/doc/html/rfc4648#autoid-8) and include padding. Throws an `Error` if the encoding is invalid. 8 | 9 | ## Definition 10 | 11 | ```ts 12 | function decodeBase64url(encoded: string): Uint8Array; 13 | ``` 14 | 15 | ### Parameters 16 | 17 | - `encoded` 18 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export { encodeHexLowerCase, encodeHexUpperCase, decodeHex } from "./hex.js"; 2 | export { 3 | encodeBase32, 4 | encodeBase32NoPadding, 5 | encodeBase32LowerCase, 6 | encodeBase32LowerCaseNoPadding, 7 | encodeBase32UpperCase, 8 | encodeBase32UpperCaseNoPadding, 9 | decodeBase32, 10 | decodeBase32IgnorePadding 11 | } from "./base32.js"; 12 | export { 13 | encodeBase64, 14 | encodeBase64NoPadding, 15 | encodeBase64url, 16 | encodeBase64urlNoPadding, 17 | decodeBase64, 18 | decodeBase64IgnorePadding, 19 | decodeBase64url, 20 | decodeBase64urlIgnorePadding 21 | } from "./base64.js"; 22 | -------------------------------------------------------------------------------- /docs/pages/reference/main/decodeBase32IgnorePadding.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "decodeBase32IgnorePadding()" 3 | --- 4 | 5 | # decodeBase32IgnorePadding() 6 | 7 | Decodes a base32 encoded string into a byte array based on [RFC 4648 §6](https://datatracker.ietf.org/doc/html/rfc4648#autoid-11). The string must be [canonical](https://datatracker.ietf.org/doc/html/rfc4648#autoid-8) but can be partially or fully missing its padding. Throws an `Error` if the encoding is invalid. 8 | 9 | ## Definition 10 | 11 | ```ts 12 | function decodeBase32IgnorePadding(encoded: string): Uint8Array; 13 | ``` 14 | 15 | ### Parameters 16 | 17 | - `encoded` 18 | -------------------------------------------------------------------------------- /docs/pages/reference/main/decodeBase64IgnorePadding.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "decodeBase64IgnorePadding()" 3 | --- 4 | 5 | # decodeBase64IgnorePadding() 6 | 7 | Decodes a base64 encoded string into a byte array based on [RFC 4648 §5](https://datatracker.ietf.org/doc/html/rfc4648#autoid-10). The string must be [canonical](https://datatracker.ietf.org/doc/html/rfc4648#autoid-8) but can be partially or fully missing its padding. Throws an `Error` if the encoding is invalid. 8 | 9 | ## Definition 10 | 11 | ```ts 12 | function decodeBase64IgnorePadding(encoded: string): Uint8Array; 13 | ``` 14 | 15 | ### Parameters 16 | 17 | - `encoded` 18 | -------------------------------------------------------------------------------- /docs/pages/reference/main/encodeBase32NoPadding.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "encodeBase32NoPadding()" 3 | --- 4 | 5 | # encodeBase32NoPadding() 6 | 7 | _Replaced: Use [`encodeBase32UpperCaseNoPadding()`](/reference/main/encodeBase32UpperCaseNoPadding) instead._ 8 | 9 | Encodes a byte array into [canonical](https://datatracker.ietf.org/doc/html/rfc4648#autoid-8) base32 based on [RFC 4648 §6](https://datatracker.ietf.org/doc/html/rfc4648#autoid-11) with upper case letters and the padding omitted. 10 | 11 | ## Definition 12 | 13 | ```ts 14 | function encodeBase32NoPadding(bytes: Uint8Array): string; 15 | ``` 16 | 17 | ### Parameters 18 | 19 | - `bytes` 20 | -------------------------------------------------------------------------------- /docs/pages/reference/main/decodeBase32.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "decodeBase32()" 3 | --- 4 | 5 | # decodeBase32() 6 | 7 | Decodes a base32 encoded string into a byte array based on [RFC 4648 §6](https://datatracker.ietf.org/doc/html/rfc4648#autoid-11). The string must be [canonical](https://datatracker.ietf.org/doc/html/rfc4648#autoid-8) and include padding. This method is case-insensitive and the string can be encoded either with lower case or upper case letters. Throws an `Error` if the encoding is invalid. 8 | 9 | ## Definition 10 | 11 | ```ts 12 | function decodeBase32(encoded: string): Uint8Array; 13 | ``` 14 | 15 | ### Parameters 16 | 17 | - `encoded` 18 | -------------------------------------------------------------------------------- /docs/pages/reference/main/decodeBase64urlIgnorePadding.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "decodeBase64IgnorePadding()" 3 | --- 4 | 5 | # decodeBase64IgnorePadding() 6 | 7 | Decodes a base64 encoded string with url-safe alphabet into a byte array based on [RFC 4648 §5](https://datatracker.ietf.org/doc/html/rfc4648#autoid-10). The string must be [canonical](https://datatracker.ietf.org/doc/html/rfc4648#autoid-8) but can be partially or fully missing its padding. Throws an `Error` if the encoding is invalid. 8 | 9 | ## Definition 10 | 11 | ```ts 12 | function decodeBase64IgnorePadding(encoded: string): Uint8Array; 13 | ``` 14 | 15 | ### Parameters 16 | 17 | - `encoded` 18 | -------------------------------------------------------------------------------- /docs/pages/examples/base32.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Base32 encoding" 3 | --- 4 | 5 | # Base32 encoding 6 | 7 | Use `encodeBase32UpperCase()` or `encodeBase32LowerCase()` to encode data with base32. Use `encodeBase32UpperCaseNoPadding()` or `encodeBase32LowerCaseNoPadding()` to omit padding. `decodeBase32()` requires padding while `decodeBase32IgnorePadding()` ignores padding entirely. Both decoding methods are case insensitive. 8 | 9 | ```ts 10 | import { encodeBase32UpperCase, decodeBase32 } from "@oslojs/encoding"; 11 | 12 | const data: Uint8Array = new TextEncoder().encode("hello world"); 13 | const encoded = encodeBase32UpperCase(data); 14 | const decoded = decodeBase32(encoded); 15 | ``` 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @oslojs/encoding 2 | 3 | **Documentation: https://encoding.oslojs.dev** 4 | 5 | A JavaScript library for encoding and decoding data with hexadecimal, base32, base64, and base64url encoding schemes based on [RFC 4648](https://datatracker.ietf.org/doc/html/rfc4648). Implementations may be stricter than most to follow the RFC as close as possible. 6 | 7 | - Runtime-agnostic 8 | - No third-party dependencies 9 | - Fully typed 10 | 11 | ```ts 12 | import { encodeBase64, decodeBase64 } from "@oslojs/encoding"; 13 | 14 | const data: Uint8Array = new TextEncoder().encode("hello world"); 15 | const encoded = encodeBase64(data); 16 | const decoded = decodeBase64(encoded); 17 | ``` 18 | 19 | ## Installation 20 | 21 | ``` 22 | npm i @oslojs/encoding 23 | ``` 24 | -------------------------------------------------------------------------------- /.eslintrc.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | rules: { 3 | "@typescript-eslint/no-explicit-any": "off", 4 | "@typescript-eslint/no-empty-function": "off", 5 | "@typescript-eslint/ban-types": "off", 6 | "@typescript-eslint/no-unused-vars": [ 7 | "error", 8 | { 9 | argsIgnorePattern: "^_", 10 | varsIgnorePattern: "^_", 11 | caughtErrorsIgnorePattern: "^_" 12 | } 13 | ], 14 | "@typescript-eslint/no-empty-interface": "off", 15 | "@typescript-eslint/explicit-function-return-type": "error", 16 | "no-async-promise-executor": "off", 17 | "no-useless-catch": "off" 18 | }, 19 | parser: "@typescript-eslint/parser", 20 | extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"], 21 | plugins: ["@typescript-eslint"], 22 | env: { 23 | node: true 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /docs/pages/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "@oslojs/encoding documentation" 3 | --- 4 | 5 | # @oslojs/encoding documentation 6 | 7 | A JavaScript library for encoding and decoding data with hexadecimal, base32, base64, and base64url encoding schemes based on [RFC 4648](https://datatracker.ietf.org/doc/html/rfc4648). Implementations may be stricter than most to follow the RFC as close as possible. 8 | 9 | - Runtime-agnostic 10 | - No third-party dependencies 11 | - Fully typed 12 | 13 | ```ts 14 | import { encodeBase64, decodeBase64 } from "@oslojs/encoding"; 15 | 16 | const data: Uint8Array = new TextEncoder().encode("hello world"); 17 | const encoded = encodeBase64(data); 18 | const decoded = decodeBase64(encoded); 19 | ``` 20 | 21 | ## Installation 22 | 23 | ``` 24 | npm i @oslojs/encoding 25 | ``` 26 | -------------------------------------------------------------------------------- /docs/pages/examples/base64.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Base64 encoding" 3 | --- 4 | 5 | # Base64 encoding 6 | 7 | Use `encodeBase64()` or `encodeBase64NoPadding()` to omit padding. `decodeBase64()` requires padding while `decodeBase64IgnorePadding()` ignores padding entirely. A URL-safe variant (base64url) versions for each method is also available. 8 | 9 | ```ts 10 | import { encodeBase64, decodeBase64 } from "@oslojs/encoding"; 11 | 12 | const data: Uint8Array = new TextEncoder().encode("hello world"); 13 | const encoded = encodeBase64(data); 14 | const decoded = decodeBase64(encoded); 15 | ``` 16 | 17 | ```ts 18 | import { encodeBase64url, decodeBase64url } from "@oslojs/encoding"; 19 | 20 | const data: Uint8Array = new TextEncoder().encode("hello world"); 21 | const encoded = encodeBase64url(data); 22 | const decoded = decodeBase64url(encoded); 23 | ``` 24 | -------------------------------------------------------------------------------- /docs/malta.config.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@oslojs/encoding", 3 | "description": "Encode binary data with hex, base32, and base64.", 4 | "domain": "https://encoding.oslojs.dev", 5 | "twitter": "@pilcrowonpaper", 6 | "asset_hashing": true, 7 | "sidebar": [ 8 | { 9 | "title": "Examples", 10 | "pages": [ 11 | ["Hex encoding", "/examples/hex"], 12 | ["Base64 encoding", "/examples/base64"], 13 | ["Base32 encoding", "/examples/base32"] 14 | ] 15 | }, 16 | { 17 | "title": "API reference", 18 | "pages": [["@oslojs/encoding", "/reference/main"]] 19 | }, 20 | { 21 | "title": "Links", 22 | "pages": [ 23 | ["GitHub", "https://github.com/oslo-project/encoding"], 24 | ["Oslo", "https://oslojs.dev"], 25 | ["Twitter", "https://twitter.com/pilcrowonpaper"], 26 | ["Donate", "https://github.com/sponsors/pilcrowOnPaper"] 27 | ] 28 | } 29 | ] 30 | } 31 | -------------------------------------------------------------------------------- /.github/workflows/publish.yaml: -------------------------------------------------------------------------------- 1 | name: "Publish" 2 | on: 3 | push: 4 | branches: 5 | - main 6 | 7 | env: 8 | CLOUDFLARE_API_TOKEN: ${{secrets.CLOUDFLARE_PAGES_API_TOKEN}} 9 | 10 | jobs: 11 | publish: 12 | name: Publish 13 | runs-on: ubuntu-latest 14 | steps: 15 | - name: setup actions 16 | uses: actions/checkout@v3 17 | - name: setup node 18 | uses: actions/setup-node@v3 19 | with: 20 | node-version: 20.5.1 21 | registry-url: https://registry.npmjs.org 22 | - name: install malta 23 | working-directory: docs 24 | run: | 25 | curl -o malta.tgz -L https://github.com/pilcrowonpaper/malta/releases/latest/download/linux-amd64.tgz 26 | tar -xvzf malta.tgz 27 | - name: build 28 | working-directory: docs 29 | run: ./linux-amd64/malta build 30 | - name: install wrangler 31 | run: npm i -g wrangler 32 | - name: deploy 33 | run: wrangler pages deploy docs/dist --project-name oslo-encoding --branch main 34 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # @oslojs/encoding 2 | 3 | ## 1.1.0 4 | 5 | - Add `encodeBase32UpperCase()`, `encodeBase32UpperCaseNoPadding()`, `encodeBase32LowerCase()`, and `encodeBase32LowerCaseNoPadding()`. 6 | 7 | ## 1.0.0 8 | 9 | - No changes. 10 | 11 | ## 0.4.1 12 | 13 | - Remove redundant code. 14 | 15 | ## 0.4.0 16 | 17 | - [Breaking] Replace `base64`, `bas64url`, and `base32` 18 | - [Breaking] Remove `base32` 19 | - [Breaking] Replace `encodeHex()` with `encodeHexLowerCase()` and `encodeHexUpperCase()`. 20 | - [Breaking] Remove `Encoding` interface. 21 | 22 | ## 0.3.0 23 | 24 | - [Breaking] Add `Encoding.encodeNoPadding()` and `Encoding.decodeIgnorePadding()` 25 | - [Breaking] Remove `includingPadding` option from `Base64Encoding.encode()` and `Base32Encoding.encode()` 26 | - [Breaking] Remove `strict` option from `Base64Encoding.decodeIgnorePadding()` and `Base32Encoding.decodeIgnorePadding()` 27 | - Add `Encoding.encodeNoPadding()`, `Base64Encoding.encodeNoPadding()`, `Base32Encoding.encodeNoPadding()` 28 | - Add `Encoding.decodeIgnorePadding()`, `Base64Encoding.decodeIgnorePadding()`, `Base32Encoding.decodeIgnorePadding()` 29 | -------------------------------------------------------------------------------- /docs/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 pilcrowOnPaper 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@oslojs/encoding", 3 | "type": "module", 4 | "version": "1.1.0", 5 | "description": "Runtime-agnostic library for encoding and decoding data", 6 | "scripts": { 7 | "build": "rm -rf dist/* && tsc --project tsconfig.build.json", 8 | "format": "prettier -w .", 9 | "lint": "eslint src", 10 | "test": "vitest run --sequence.concurrent" 11 | }, 12 | "files": [ 13 | "/dist/" 14 | ], 15 | "main": "dist/index.js", 16 | "module": "dist/index.js", 17 | "types": "dist/index.d.ts", 18 | "keywords": [ 19 | "auth", 20 | "encoding", 21 | "hex", 22 | "base16", 23 | "base32", 24 | "base64", 25 | "base64url" 26 | ], 27 | "repository": { 28 | "type": "git", 29 | "url": "https://github.com/oslo-project/encoding" 30 | }, 31 | "author": "pilcrowOnPaper", 32 | "license": "MIT", 33 | "devDependencies": { 34 | "@scure/base": "^1.1.6", 35 | "@types/node": "^20.11.25", 36 | "@typescript-eslint/eslint-plugin": "^6.7.5", 37 | "@typescript-eslint/parser": "^6.7.5", 38 | "auri": "^2.0.0", 39 | "eslint": "^8.51.0", 40 | "prettier": "^3.0.3", 41 | "typescript": "^5.2.2", 42 | "vitest": "^0.34.6" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributor manual 2 | 3 | ## Contributing to the docs 4 | 5 | Coming soon. 6 | 7 | ## Contributing to the source code 8 | 9 | We are open to most contributions, but please open a new issue before creating a pull request, especially for new features. It's likely your PR will be rejected if not. We have intentionally limited the scope of the project and we would like to keep the package lean. 10 | 11 | ### Set up 12 | 13 | Install dependencies with PNPM. 14 | 15 | ``` 16 | pnpm i 17 | ``` 18 | 19 | ### Testing 20 | 21 | Run `pnpm test` to run tests and `pnpm build` to build the package. 22 | 23 | ``` 24 | pnpm test 25 | 26 | pnpm build 27 | ``` 28 | 29 | ### Creating changesets 30 | 31 | When creating a PR, create a changeset with `pnpm auri add`. If you made multiple changes, create multiple changesets. Use `minor` for new features, and use `patch` for bug fixes: 32 | 33 | ``` 34 | pnpm auri add minor 35 | pnpm auri add patch 36 | ``` 37 | 38 | A new markdown file should be created in `.changesets` directory. Write a short summary of the change: 39 | 40 | ``` 41 | Fix: Handle negative numbers in `sqrt()` 42 | ``` 43 | 44 | ``` 45 | Feat: Add `greet()` 46 | ``` 47 | -------------------------------------------------------------------------------- /src/hex.test.ts: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "vitest"; 2 | import { encodeHexLowerCase, encodeHexUpperCase, decodeHex } from "./hex.js"; 3 | 4 | test("encodeHexLowerCase()", () => { 5 | const cases = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 6 | for (const length of cases) { 7 | const data = crypto.getRandomValues(new Uint8Array(length)); 8 | expect(encodeHexLowerCase(data)).toBe(Buffer.from(data).toString("hex")); 9 | } 10 | }); 11 | 12 | test("encodeHexUpperCase()", () => { 13 | const cases = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 14 | for (const length of cases) { 15 | const data = crypto.getRandomValues(new Uint8Array(length)); 16 | expect(encodeHexUpperCase(data)).toBe(Buffer.from(data).toString("hex").toUpperCase()); 17 | } 18 | }); 19 | 20 | describe("Base32.decodeIgnorePadding()", () => { 21 | test("Returns encoded data", () => { 22 | for (let i = 0; i < 100; i++) { 23 | const data = crypto.getRandomValues(new Uint8Array(i)); 24 | expect(decodeHex(encodeHexLowerCase(data))).toStrictEqual(data); 25 | expect(decodeHex(encodeHexUpperCase(data))).toStrictEqual(data); 26 | } 27 | }); 28 | test("Throws Error if data is invalid", () => { 29 | expect(() => decodeHex("a")).toThrowError(); 30 | expect(() => decodeHex("x")).toThrowError(); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /docs/pages/reference/main/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "@oslojs/encoding" 3 | --- 4 | 5 | # @oslojs/encoding 6 | 7 | # Functions 8 | 9 | - [`decodeBase32()`](/reference/main/decodeBase32) 10 | - [`decodeBase32IgnorePadding()`](/reference/main/decodeBase32IgnorePadding) 11 | - [`decodeBase64()`](/reference/main/decodeBase64) 12 | - [`decodeBase64IgnorePadding()`](/reference/main/decodeBase64IgnorePadding) 13 | - [`decodeBase64url()`](/reference/main/decodeBase64url) 14 | - [`decodeBase64urlIgnorePadding()`](/reference/main/decodeBase64urlIgnorePadding) 15 | - [`decodeHex()`](/reference/main/decodeHex) 16 | - [`encodeBase32LowerCase()`](/reference/main/encodeBase32LowerCase) 17 | - [`encodeBase32LowerCaseNoPadding()`](/reference/main/encodeBase32LowerCaseNoPadding) 18 | - [`encodeBase32UpperCase()`](/reference/main/encodeBase32UpperCase) 19 | - [`encodeBase32UpperCaseNoPadding()`](/reference/main/encodeBase32UpperCaseNoPadding) 20 | - [`encodeBase64()`](/reference/main/encodeBase64) 21 | - [`encodeBase64NoPadding()`](/reference/main/encodeBase64NoPadding) 22 | - [`encodeBase64url()`](/reference/main/encodeBase64url) 23 | - [`encodeBase64urlNoPadding()`](/reference/main/encodeBase64urlNoPadding) 24 | - [`encodeHexLowerCase()`](/reference/main/encodeHexLowerCase) 25 | - [`encodeHexUpperCase()`](/reference/main/encodeHexUpperCase) 26 | - _Replaced_ [`encodeBase32()`](/reference/main/encodeBase32) 27 | - _Replaced_ [`encodeBase32NoPadding()`](/reference/main/encodeBase32NoPadding) 28 | -------------------------------------------------------------------------------- /src/hex.ts: -------------------------------------------------------------------------------- 1 | export function encodeHexUpperCase(data: Uint8Array): string { 2 | let result = ""; 3 | for (let i = 0; i < data.length; i++) { 4 | result += alphabetUpperCase[data[i] >> 4]; 5 | result += alphabetUpperCase[data[i] & 0x0f]; 6 | } 7 | return result; 8 | } 9 | 10 | export function encodeHexLowerCase(data: Uint8Array): string { 11 | let result = ""; 12 | for (let i = 0; i < data.length; i++) { 13 | result += alphabetLowerCase[data[i] >> 4]; 14 | result += alphabetLowerCase[data[i] & 0x0f]; 15 | } 16 | return result; 17 | } 18 | 19 | export function decodeHex(data: string): Uint8Array { 20 | if (data.length % 2 !== 0) { 21 | throw new Error("Invalid hex string"); 22 | } 23 | const result = new Uint8Array(data.length / 2); 24 | for (let i = 0; i < data.length; i += 2) { 25 | if (!(data[i] in decodeMap)) { 26 | throw new Error("Invalid character"); 27 | } 28 | if (!(data[i + 1] in decodeMap)) { 29 | throw new Error("Invalid character"); 30 | } 31 | result[i / 2] |= decodeMap[data[i]] << 4; 32 | result[i / 2] |= decodeMap[data[i + 1]]; 33 | } 34 | return result; 35 | } 36 | 37 | const alphabetUpperCase = "0123456789ABCDEF"; 38 | const alphabetLowerCase = "0123456789abcdef"; 39 | 40 | const decodeMap: Record = { 41 | "0": 0, 42 | "1": 1, 43 | "2": 2, 44 | "3": 3, 45 | "4": 4, 46 | "5": 5, 47 | "6": 6, 48 | "7": 7, 49 | "8": 8, 50 | "9": 9, 51 | a: 10, 52 | A: 10, 53 | b: 11, 54 | B: 11, 55 | c: 12, 56 | C: 12, 57 | d: 13, 58 | D: 13, 59 | e: 14, 60 | E: 14, 61 | f: 15, 62 | F: 15 63 | }; 64 | -------------------------------------------------------------------------------- /src/base64.test.ts: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | import { 3 | decodeBase64, 4 | decodeBase64IgnorePadding, 5 | encodeBase64, 6 | encodeBase64NoPadding 7 | } from "./base64.js"; 8 | 9 | test("encodeBase64()", () => { 10 | expect(encodeBase64(new Uint8Array())).toBe(""); 11 | for (let i = 1; i <= 100; i++) { 12 | const bytes = new Uint8Array(i); 13 | crypto.getRandomValues(bytes); 14 | expect(encodeBase64(bytes)).toBe(Buffer.from(bytes).toString("base64")); 15 | } 16 | }); 17 | 18 | test("encodeBase64NoPadding()", () => { 19 | expect(encodeBase64(new Uint8Array())).toBe(""); 20 | for (let i = 1; i <= 100; i++) { 21 | const bytes = new Uint8Array(i); 22 | crypto.getRandomValues(bytes); 23 | expect(encodeBase64NoPadding(bytes)).toBe(encodeBase64(bytes).replaceAll("=", "")); 24 | } 25 | }); 26 | 27 | test("decodeBase64()", () => { 28 | expect(decodeBase64("")).toStrictEqual(new Uint8Array()); 29 | for (let i = 1; i <= 100; i++) { 30 | const bytes = new Uint8Array(i); 31 | crypto.getRandomValues(bytes); 32 | expect(decodeBase64(encodeBase64(bytes))).toStrictEqual(bytes); 33 | } 34 | }); 35 | 36 | test("decodeBase64IgnorePadding()", () => { 37 | expect(decodeBase64IgnorePadding("")).toStrictEqual(new Uint8Array()); 38 | for (let i = 1; i <= 100; i++) { 39 | const bytes = new Uint8Array(i); 40 | crypto.getRandomValues(bytes); 41 | expect(decodeBase64IgnorePadding(encodeBase64NoPadding(bytes))).toStrictEqual(bytes); 42 | } 43 | // includes padding but invalid padding count 44 | for (let i = 1; i <= 100; i++) { 45 | const bytes = new Uint8Array(i); 46 | crypto.getRandomValues(bytes); 47 | expect(decodeBase64IgnorePadding(encodeBase64(bytes).replace("=", ""))).toStrictEqual(bytes); 48 | } 49 | }); 50 | 51 | test("decodeBase64() throws on invalid padding", () => { 52 | expect(() => decodeBase64("qqo")).toThrowError(); 53 | expect(() => decodeBase64("qqp=")).toThrowError(); 54 | expect(() => decodeBase64("q===")).toThrowError(); 55 | expect(() => decodeBase64("====")).toThrowError(); 56 | expect(() => decodeBase64("=")).toThrowError(); 57 | expect(() => decodeBase64("q=q=")).toThrowError(); 58 | expect(() => decodeBase64("qqqqq===")).toThrowError(); 59 | expect(() => decodeBase64("qqqq====")).toThrowError(); 60 | expect(() => decodeBase64("qqqqq=qq")).toThrowError(); 61 | }); 62 | -------------------------------------------------------------------------------- /src/base32.test.ts: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | import { base32 as base32Reference } from "@scure/base"; 3 | import { 4 | decodeBase32, 5 | decodeBase32IgnorePadding, 6 | encodeBase32, 7 | encodeBase32LowerCase, 8 | encodeBase32LowerCaseNoPadding, 9 | encodeBase32NoPadding, 10 | encodeBase32UpperCase, 11 | encodeBase32UpperCaseNoPadding 12 | } from "./base32.js"; 13 | 14 | test("encodeBase32()", () => { 15 | expect(encodeBase32(new Uint8Array())).toBe(""); 16 | for (let i = 1; i <= 100; i++) { 17 | const bytes = new Uint8Array(i); 18 | crypto.getRandomValues(bytes); 19 | expect(encodeBase32(bytes)).toBe(base32Reference.encode(bytes)); 20 | } 21 | }); 22 | 23 | test("encodeBase32NoPadding()", () => { 24 | expect(encodeBase32(new Uint8Array())).toBe(""); 25 | for (let i = 1; i <= 100; i++) { 26 | const bytes = new Uint8Array(i); 27 | crypto.getRandomValues(bytes); 28 | expect(encodeBase32NoPadding(bytes)).toBe(base32Reference.encode(bytes).replaceAll("=", "")); 29 | } 30 | }); 31 | 32 | test("encodeBase32UpperCase()", () => { 33 | expect(encodeBase32(new Uint8Array())).toBe(""); 34 | for (let i = 1; i <= 100; i++) { 35 | const bytes = new Uint8Array(i); 36 | crypto.getRandomValues(bytes); 37 | expect(encodeBase32UpperCase(bytes)).toBe(base32Reference.encode(bytes)); 38 | } 39 | }); 40 | 41 | test("encodeBase32UpperCaseNoPadding()", () => { 42 | expect(encodeBase32(new Uint8Array())).toBe(""); 43 | for (let i = 1; i <= 100; i++) { 44 | const bytes = new Uint8Array(i); 45 | crypto.getRandomValues(bytes); 46 | expect(encodeBase32UpperCaseNoPadding(bytes)).toBe( 47 | base32Reference.encode(bytes).replaceAll("=", "") 48 | ); 49 | } 50 | }); 51 | 52 | test("encodeBase32LowerCase()", () => { 53 | expect(encodeBase32(new Uint8Array())).toBe(""); 54 | for (let i = 1; i <= 100; i++) { 55 | const bytes = new Uint8Array(i); 56 | crypto.getRandomValues(bytes); 57 | expect(encodeBase32LowerCase(bytes)).toBe(base32Reference.encode(bytes).toLowerCase()); 58 | } 59 | }); 60 | 61 | test("encodeBase32LowerCaseNoPadding()", () => { 62 | expect(encodeBase32(new Uint8Array())).toBe(""); 63 | for (let i = 1; i <= 100; i++) { 64 | const bytes = new Uint8Array(i); 65 | crypto.getRandomValues(bytes); 66 | expect(encodeBase32LowerCaseNoPadding(bytes)).toBe( 67 | base32Reference.encode(bytes).toLowerCase().replaceAll("=", "") 68 | ); 69 | } 70 | }); 71 | 72 | test("decodeBase32()", () => { 73 | expect(decodeBase32("")).toStrictEqual(new Uint8Array()); 74 | for (let i = 1; i <= 100; i++) { 75 | const bytes = new Uint8Array(i); 76 | crypto.getRandomValues(bytes); 77 | expect(decodeBase32(encodeBase32(bytes))).toStrictEqual(bytes); 78 | } 79 | for (let i = 1; i <= 100; i++) { 80 | const bytes = new Uint8Array(i); 81 | crypto.getRandomValues(bytes); 82 | expect(decodeBase32(encodeBase32(bytes).toLowerCase())).toStrictEqual(bytes); 83 | } 84 | }); 85 | 86 | test("decodeBase32IgnorePadding()", () => { 87 | expect(decodeBase32IgnorePadding("")).toStrictEqual(new Uint8Array()); 88 | for (let i = 1; i <= 100; i++) { 89 | const bytes = new Uint8Array(i); 90 | crypto.getRandomValues(bytes); 91 | expect(decodeBase32IgnorePadding(encodeBase32NoPadding(bytes))).toStrictEqual(bytes); 92 | } 93 | // includes padding but invalid padding count 94 | for (let i = 1; i <= 100; i++) { 95 | const bytes = new Uint8Array(i); 96 | crypto.getRandomValues(bytes); 97 | expect(decodeBase32IgnorePadding(encodeBase32(bytes).replace("=", ""))).toStrictEqual(bytes); 98 | } 99 | }); 100 | 101 | test("decodeBase32() throws on invalid padding", () => { 102 | expect(() => decodeBase32("VKVA")).toThrowError(); 103 | expect(() => decodeBase32("VKVK====")).toThrowError(); 104 | expect(() => decodeBase32("V=======")).toThrowError(); 105 | expect(() => decodeBase32("========")).toThrowError(); 106 | expect(() => decodeBase32("=")).toThrowError(); 107 | expect(() => decodeBase32("V=VKVKVK")).toThrowError(); 108 | expect(() => decodeBase32("VKVKVKVK========")).toThrowError(); 109 | expect(() => decodeBase32("VKVKVKVKV=VKVKVK")).toThrowError(); 110 | expect(() => decodeBase32("VKVKVKVKV=======")).toThrowError(); 111 | }); 112 | -------------------------------------------------------------------------------- /src/base32.ts: -------------------------------------------------------------------------------- 1 | export function encodeBase32UpperCase(bytes: Uint8Array): string { 2 | return encodeBase32_internal(bytes, base32UpperCaseAlphabet, EncodingPadding.Include); 3 | } 4 | 5 | export function encodeBase32UpperCaseNoPadding(bytes: Uint8Array): string { 6 | return encodeBase32_internal(bytes, base32UpperCaseAlphabet, EncodingPadding.None); 7 | } 8 | 9 | export function encodeBase32LowerCase(bytes: Uint8Array): string { 10 | return encodeBase32_internal(bytes, base32LowerCaseAlphabet, EncodingPadding.Include); 11 | } 12 | 13 | export function encodeBase32LowerCaseNoPadding(bytes: Uint8Array): string { 14 | return encodeBase32_internal(bytes, base32LowerCaseAlphabet, EncodingPadding.None); 15 | } 16 | 17 | /** Replaced: Use encodeBase32UpperCase() instead. */ 18 | export function encodeBase32(bytes: Uint8Array): string { 19 | return encodeBase32UpperCase(bytes); 20 | } 21 | 22 | /** Replaced: Use encodeBase32UpperCaseNoPadding() instead. */ 23 | export function encodeBase32NoPadding(bytes: Uint8Array): string { 24 | return encodeBase32UpperCaseNoPadding(bytes); 25 | } 26 | 27 | function encodeBase32_internal( 28 | bytes: Uint8Array, 29 | alphabet: string, 30 | padding: EncodingPadding 31 | ): string { 32 | let result = ""; 33 | for (let i = 0; i < bytes.byteLength; i += 5) { 34 | let buffer = 0n; 35 | let bufferBitSize = 0; 36 | for (let j = 0; j < 5 && i + j < bytes.byteLength; j++) { 37 | buffer = (buffer << 8n) | BigInt(bytes[i + j]); 38 | bufferBitSize += 8; 39 | } 40 | if (bufferBitSize % 5 !== 0) { 41 | buffer = buffer << BigInt(5 - (bufferBitSize % 5)); 42 | bufferBitSize += 5 - (bufferBitSize % 5); 43 | } 44 | for (let j = 0; j < 8; j++) { 45 | if (bufferBitSize >= 5) { 46 | result += alphabet[Number((buffer >> BigInt(bufferBitSize - 5)) & 0x1fn)]; 47 | bufferBitSize -= 5; 48 | } else if (bufferBitSize > 0) { 49 | result += alphabet[Number((buffer << BigInt(6 - bufferBitSize)) & 0x3fn)]; 50 | bufferBitSize = 0; 51 | } else if (padding === EncodingPadding.Include) { 52 | result += "="; 53 | } 54 | } 55 | } 56 | return result; 57 | } 58 | 59 | export function decodeBase32(encoded: string): Uint8Array { 60 | return decodeBase32_internal(encoded, base32DecodeMap, DecodingPadding.Required); 61 | } 62 | 63 | export function decodeBase32IgnorePadding(encoded: string): Uint8Array { 64 | return decodeBase32_internal(encoded, base32DecodeMap, DecodingPadding.Ignore); 65 | } 66 | 67 | function decodeBase32_internal( 68 | encoded: string, 69 | decodeMap: Record, 70 | padding: DecodingPadding 71 | ): Uint8Array { 72 | const result = new Uint8Array(Math.ceil(encoded.length / 8) * 5); 73 | let totalBytes = 0; 74 | for (let i = 0; i < encoded.length; i += 8) { 75 | let chunk = 0n; 76 | let bitsRead = 0; 77 | for (let j = 0; j < 8; j++) { 78 | if (padding === DecodingPadding.Required) { 79 | if (encoded[i + j] === "=") { 80 | continue; 81 | } 82 | if (i + j >= encoded.length) { 83 | throw new Error("Invalid padding"); 84 | } 85 | } 86 | if (padding === DecodingPadding.Ignore) { 87 | if (i + j >= encoded.length || encoded[i + j] === "=") { 88 | continue; 89 | } 90 | } 91 | if (j > 0 && encoded[i + j - 1] === "=") { 92 | throw new Error("Invalid padding"); 93 | } 94 | if (!(encoded[i + j] in decodeMap)) { 95 | throw new Error("Invalid character"); 96 | } 97 | chunk |= BigInt(decodeMap[encoded[i + j]]) << BigInt((7 - j) * 5); 98 | bitsRead += 5; 99 | } 100 | if (bitsRead < 40) { 101 | let unused: bigint; 102 | if (bitsRead === 10) { 103 | unused = chunk & 0xffffffffn; 104 | } else if (bitsRead === 20) { 105 | unused = chunk & 0xffffffn; 106 | } else if (bitsRead === 25) { 107 | unused = chunk & 0xffffn; 108 | } else if (bitsRead === 35) { 109 | unused = chunk & 0xffn; 110 | } else { 111 | throw new Error("Invalid padding"); 112 | } 113 | if (unused !== 0n) { 114 | throw new Error("Invalid padding"); 115 | } 116 | } 117 | const byteLength = Math.floor(bitsRead / 8); 118 | for (let i = 0; i < byteLength; i++) { 119 | result[totalBytes] = Number((chunk >> BigInt(32 - i * 8)) & 0xffn); 120 | totalBytes++; 121 | } 122 | } 123 | return result.slice(0, totalBytes); 124 | } 125 | 126 | const base32UpperCaseAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; 127 | const base32LowerCaseAlphabet = "abcdefghijklmnopqrstuvwxyz234567"; 128 | 129 | const base32DecodeMap = { 130 | A: 0, 131 | B: 1, 132 | C: 2, 133 | D: 3, 134 | E: 4, 135 | F: 5, 136 | G: 6, 137 | H: 7, 138 | I: 8, 139 | J: 9, 140 | K: 10, 141 | L: 11, 142 | M: 12, 143 | N: 13, 144 | O: 14, 145 | P: 15, 146 | Q: 16, 147 | R: 17, 148 | S: 18, 149 | T: 19, 150 | U: 20, 151 | V: 21, 152 | W: 22, 153 | X: 23, 154 | Y: 24, 155 | Z: 25, 156 | a: 0, 157 | b: 1, 158 | c: 2, 159 | d: 3, 160 | e: 4, 161 | f: 5, 162 | g: 6, 163 | h: 7, 164 | i: 8, 165 | j: 9, 166 | k: 10, 167 | l: 11, 168 | m: 12, 169 | n: 13, 170 | o: 14, 171 | p: 15, 172 | q: 16, 173 | r: 17, 174 | s: 18, 175 | t: 19, 176 | u: 20, 177 | v: 21, 178 | w: 22, 179 | x: 23, 180 | y: 24, 181 | z: 25, 182 | "2": 26, 183 | "3": 27, 184 | "4": 28, 185 | "5": 29, 186 | "6": 30, 187 | "7": 31 188 | }; 189 | 190 | enum EncodingPadding { 191 | Include = 0, 192 | None 193 | } 194 | 195 | enum DecodingPadding { 196 | Required = 0, 197 | Ignore 198 | } 199 | -------------------------------------------------------------------------------- /src/base64.ts: -------------------------------------------------------------------------------- 1 | export function encodeBase64(bytes: Uint8Array): string { 2 | return encodeBase64_internal(bytes, base64Alphabet, EncodingPadding.Include); 3 | } 4 | 5 | export function encodeBase64NoPadding(bytes: Uint8Array): string { 6 | return encodeBase64_internal(bytes, base64Alphabet, EncodingPadding.None); 7 | } 8 | 9 | export function encodeBase64url(bytes: Uint8Array): string { 10 | return encodeBase64_internal(bytes, base64urlAlphabet, EncodingPadding.Include); 11 | } 12 | 13 | export function encodeBase64urlNoPadding(bytes: Uint8Array): string { 14 | return encodeBase64_internal(bytes, base64urlAlphabet, EncodingPadding.None); 15 | } 16 | 17 | function encodeBase64_internal( 18 | bytes: Uint8Array, 19 | alphabet: string, 20 | padding: EncodingPadding 21 | ): string { 22 | let result = ""; 23 | for (let i = 0; i < bytes.byteLength; i += 3) { 24 | let buffer = 0; 25 | let bufferBitSize = 0; 26 | for (let j = 0; j < 3 && i + j < bytes.byteLength; j++) { 27 | buffer = (buffer << 8) | bytes[i + j]; 28 | bufferBitSize += 8; 29 | } 30 | for (let j = 0; j < 4; j++) { 31 | if (bufferBitSize >= 6) { 32 | result += alphabet[(buffer >> (bufferBitSize - 6)) & 0x3f]; 33 | bufferBitSize -= 6; 34 | } else if (bufferBitSize > 0) { 35 | result += alphabet[(buffer << (6 - bufferBitSize)) & 0x3f]; 36 | bufferBitSize = 0; 37 | } else if (padding === EncodingPadding.Include) { 38 | result += "="; 39 | } 40 | } 41 | } 42 | return result; 43 | } 44 | 45 | const base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 46 | const base64urlAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; 47 | 48 | export function decodeBase64(encoded: string): Uint8Array { 49 | return decodeBase64_internal(encoded, base64DecodeMap, DecodingPadding.Required); 50 | } 51 | 52 | export function decodeBase64IgnorePadding(encoded: string): Uint8Array { 53 | return decodeBase64_internal(encoded, base64DecodeMap, DecodingPadding.Ignore); 54 | } 55 | 56 | export function decodeBase64url(encoded: string): Uint8Array { 57 | return decodeBase64_internal(encoded, base64urlDecodeMap, DecodingPadding.Required); 58 | } 59 | 60 | export function decodeBase64urlIgnorePadding(encoded: string): Uint8Array { 61 | return decodeBase64_internal(encoded, base64urlDecodeMap, DecodingPadding.Ignore); 62 | } 63 | 64 | function decodeBase64_internal( 65 | encoded: string, 66 | decodeMap: Record, 67 | padding: DecodingPadding 68 | ): Uint8Array { 69 | const result = new Uint8Array(Math.ceil(encoded.length / 4) * 3); 70 | let totalBytes = 0; 71 | for (let i = 0; i < encoded.length; i += 4) { 72 | let chunk = 0; 73 | let bitsRead = 0; 74 | for (let j = 0; j < 4; j++) { 75 | if (padding === DecodingPadding.Required && encoded[i + j] === "=") { 76 | continue; 77 | } 78 | if ( 79 | padding === DecodingPadding.Ignore && 80 | (i + j >= encoded.length || encoded[i + j] === "=") 81 | ) { 82 | continue; 83 | } 84 | if (j > 0 && encoded[i + j - 1] === "=") { 85 | throw new Error("Invalid padding"); 86 | } 87 | if (!(encoded[i + j] in decodeMap)) { 88 | throw new Error("Invalid character"); 89 | } 90 | chunk |= decodeMap[encoded[i + j]] << ((3 - j) * 6); 91 | bitsRead += 6; 92 | } 93 | if (bitsRead < 24) { 94 | let unused: number; 95 | if (bitsRead === 12) { 96 | unused = chunk & 0xffff; 97 | } else if (bitsRead === 18) { 98 | unused = chunk & 0xff; 99 | } else { 100 | throw new Error("Invalid padding"); 101 | } 102 | if (unused !== 0) { 103 | throw new Error("Invalid padding"); 104 | } 105 | } 106 | const byteLength = Math.floor(bitsRead / 8); 107 | for (let i = 0; i < byteLength; i++) { 108 | result[totalBytes] = (chunk >> (16 - i * 8)) & 0xff; 109 | totalBytes++; 110 | } 111 | } 112 | return result.slice(0, totalBytes); 113 | } 114 | 115 | enum EncodingPadding { 116 | Include = 0, 117 | None 118 | } 119 | 120 | enum DecodingPadding { 121 | Required = 0, 122 | Ignore 123 | } 124 | 125 | const base64DecodeMap = { 126 | "0": 52, 127 | "1": 53, 128 | "2": 54, 129 | "3": 55, 130 | "4": 56, 131 | "5": 57, 132 | "6": 58, 133 | "7": 59, 134 | "8": 60, 135 | "9": 61, 136 | A: 0, 137 | B: 1, 138 | C: 2, 139 | D: 3, 140 | E: 4, 141 | F: 5, 142 | G: 6, 143 | H: 7, 144 | I: 8, 145 | J: 9, 146 | K: 10, 147 | L: 11, 148 | M: 12, 149 | N: 13, 150 | O: 14, 151 | P: 15, 152 | Q: 16, 153 | R: 17, 154 | S: 18, 155 | T: 19, 156 | U: 20, 157 | V: 21, 158 | W: 22, 159 | X: 23, 160 | Y: 24, 161 | Z: 25, 162 | a: 26, 163 | b: 27, 164 | c: 28, 165 | d: 29, 166 | e: 30, 167 | f: 31, 168 | g: 32, 169 | h: 33, 170 | i: 34, 171 | j: 35, 172 | k: 36, 173 | l: 37, 174 | m: 38, 175 | n: 39, 176 | o: 40, 177 | p: 41, 178 | q: 42, 179 | r: 43, 180 | s: 44, 181 | t: 45, 182 | u: 46, 183 | v: 47, 184 | w: 48, 185 | x: 49, 186 | y: 50, 187 | z: 51, 188 | "+": 62, 189 | "/": 63 190 | }; 191 | 192 | const base64urlDecodeMap = { 193 | "0": 52, 194 | "1": 53, 195 | "2": 54, 196 | "3": 55, 197 | "4": 56, 198 | "5": 57, 199 | "6": 58, 200 | "7": 59, 201 | "8": 60, 202 | "9": 61, 203 | A: 0, 204 | B: 1, 205 | C: 2, 206 | D: 3, 207 | E: 4, 208 | F: 5, 209 | G: 6, 210 | H: 7, 211 | I: 8, 212 | J: 9, 213 | K: 10, 214 | L: 11, 215 | M: 12, 216 | N: 13, 217 | O: 14, 218 | P: 15, 219 | Q: 16, 220 | R: 17, 221 | S: 18, 222 | T: 19, 223 | U: 20, 224 | V: 21, 225 | W: 22, 226 | X: 23, 227 | Y: 24, 228 | Z: 25, 229 | a: 26, 230 | b: 27, 231 | c: 28, 232 | d: 29, 233 | e: 30, 234 | f: 31, 235 | g: 32, 236 | h: 33, 237 | i: 34, 238 | j: 35, 239 | k: 36, 240 | l: 37, 241 | m: 38, 242 | n: 39, 243 | o: 40, 244 | p: 41, 245 | q: 42, 246 | r: 43, 247 | s: 44, 248 | t: 45, 249 | u: 46, 250 | v: 47, 251 | w: 48, 252 | x: 49, 253 | y: 50, 254 | z: 51, 255 | "-": 62, 256 | _: 63 257 | }; 258 | --------------------------------------------------------------------------------