@otplib/plugin-crypto-node 13.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Gerald Yeo
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.
package/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # @otplib/plugin-crypto-node
2
+
3
+ Node.js crypto plugin for otplib using the built-in `crypto` module.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @otplib/plugin-crypto-node
9
+ pnpm add @otplib/plugin-crypto-node
10
+ yarn add @otplib/plugin-crypto-node
11
+ ```
12
+
13
+ ## Overview
14
+
15
+ This plugin provides HMAC and random byte generation using Node.js's built-in `crypto` module. It supports all hash algorithms available in Node.js:
16
+
17
+ - `sha1`
18
+ - `sha256`
19
+ - `sha512`
20
+
21
+ ## Usage
22
+
23
+ ### Basic Usage
24
+
25
+ ```typescript
26
+ import { generateSecret, generate } from "otplib";
27
+ import { NodeCryptoPlugin } from "@otplib/plugin-crypto-node";
28
+ import { ScureBase32Plugin } from "@otplib/plugin-base32-scure";
29
+
30
+ const crypto = new NodeCryptoPlugin();
31
+ const base32 = new ScureBase32Plugin();
32
+
33
+ // Generate a secret
34
+ const secret = generateSecret({ crypto, base32 });
35
+
36
+ // Generate a token
37
+ const token = await generate({
38
+ secret,
39
+ crypto,
40
+ base32,
41
+ });
42
+ ```
43
+
44
+ ### With Custom Algorithm
45
+
46
+ ```typescript
47
+ import { generate } from "otplib";
48
+ import { NodeCryptoPlugin } from "@otplib/plugin-crypto-node";
49
+ import { ScureBase32Plugin } from "@otplib/plugin-base32-scure";
50
+
51
+ const crypto = new NodeCryptoPlugin();
52
+ const base32 = new ScureBase32Plugin();
53
+
54
+ const token = await generate({
55
+ secret: "JBSWY3DPEHPK3PXP",
56
+ algorithm: "sha256",
57
+ crypto,
58
+ base32,
59
+ });
60
+ ```
61
+
62
+ ### Synchronous HMAC
63
+
64
+ The Node.js crypto plugin supports both synchronous and asynchronous HMAC operations:
65
+
66
+ ```typescript
67
+ import { NodeCryptoPlugin } from "@otplib/plugin-crypto-node";
68
+
69
+ const crypto = new NodeCryptoPlugin();
70
+
71
+ // Sync HMAC (faster, but blocks event loop)
72
+ const digest = crypto.hmacSync("sha1", key, data);
73
+
74
+ // Async HMAC (doesn't block event loop)
75
+ const digest = await crypto.hmac("sha1", key, data);
76
+ ```
77
+
78
+ ## When to Use
79
+
80
+ Use this plugin when:
81
+
82
+ - Running in Node.js environment
83
+ - Need maximum performance
84
+ - Want to use Node.js built-in crypto (no external dependencies)
85
+ - Need synchronous HMAC operations
86
+
87
+ ## Platform Support
88
+
89
+ - Node.js (all versions)
90
+ - Not available in browsers (use `@otplib/plugin-crypto-web` instead)
91
+ - Not available in edge runtimes (use `@otplib/plugin-crypto-web` instead)
92
+
93
+ ## Documentation
94
+
95
+ Full documentation available at [otplib.yeojz.dev](https://otplib.yeojz.dev):
96
+
97
+ - [Getting Started Guide](https://otplib.yeojz.dev/guide/getting-started)
98
+ - [API Reference](https://otplib.yeojz.dev/api/)
99
+
100
+ ## License
101
+
102
+ [MIT](./LICENSE) © 2026 Gerald Yeo
package/dist/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";var y=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var c=Object.prototype.hasOwnProperty;var l=(n,r)=>{for(var e in r)y(n,e,{get:r[e],enumerable:!0})},p=(n,r,e,a)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of u(r))!c.call(n,t)&&t!==e&&y(n,t,{get:()=>r[t],enumerable:!(a=m(r,t))||a.enumerable});return n};var f=n=>p(y({},"__esModule",{value:!0}),n);var U={};l(U,{NodeCryptoPlugin:()=>s,crypto:()=>A,default:()=>g});module.exports=f(U);var i=require("crypto"),o=require("@otplib/core"),s=class{name="node";hmac(r,e,a){let t=(0,i.createHmac)(r,e);return t.update(a),new Uint8Array(t.digest())}randomBytes(r){return new Uint8Array((0,i.randomBytes)(r))}constantTimeEqual(r,e){let a=(0,o.stringToBytes)(r),t=(0,o.stringToBytes)(e);return(0,o.validateByteLengthEqual)(a,t)?(0,i.timingSafeEqual)(a,t):!1}},A=Object.freeze(new s),g=s;0&&(module.exports={NodeCryptoPlugin,crypto});
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { createHmac, randomBytes, timingSafeEqual } from \"node:crypto\";\n\nimport { stringToBytes, validateByteLengthEqual, type CryptoPlugin } from \"@otplib/core\";\n\n/**\n * Node.js crypto module implementation of CryptoPlugin\n *\n * This plugin uses Node.js's built-in crypto module which provides:\n * - OpenSSL-backed HMAC operations\n * - Cryptographically secure random byte generation\n * - Synchronous API for optimal performance\n *\n * @example\n * ```ts\n * import { NodeCryptoPlugin } from '@otplib/plugin-crypto-node';\n *\n * const crypto = new NodeCryptoPlugin();\n * const hmac = await crypto.hmac('sha1', key, data);\n * const random = crypto.randomBytes(20);\n * ```\n */\nexport class NodeCryptoPlugin implements CryptoPlugin {\n /**\n * Plugin name for identification\n */\n readonly name = \"node\";\n\n /**\n * Compute HMAC using Node.js crypto module\n *\n * Synchronous implementation using createHmac.\n *\n * @param algorithm - Hash algorithm to use\n * @param key - Secret key\n * @param data - Data to authenticate\n * @returns HMAC digest\n */\n hmac(algorithm: \"sha1\" | \"sha256\" | \"sha512\", key: Uint8Array, data: Uint8Array): Uint8Array {\n const hmac = createHmac(algorithm, key);\n hmac.update(data);\n return new Uint8Array(hmac.digest());\n }\n\n /**\n * Generate cryptographically secure random bytes\n *\n * Uses Node.js's randomBytes which is backed by OpenSSL.\n *\n * @param length - Number of bytes to generate\n * @returns Random bytes\n */\n randomBytes(length: number): Uint8Array {\n return new Uint8Array(randomBytes(length));\n }\n\n /**\n * Constant-time comparison using Node.js crypto.timingSafeEqual\n *\n * Uses Node.js's built-in timing-safe comparison which prevents\n * timing side-channel attacks.\n *\n * @param a - First value to compare\n * @param b - Second value to compare\n * @returns true if values are equal, false otherwise\n */\n constantTimeEqual(a: string | Uint8Array, b: string | Uint8Array): boolean {\n const bufA = stringToBytes(a);\n const bufB = stringToBytes(b);\n\n if (!validateByteLengthEqual(bufA, bufB)) {\n return false;\n }\n\n return timingSafeEqual(bufA, bufB);\n }\n}\n\n/**\n * Default singleton instance for convenience\n *\n * @example\n * ```ts\n * import { crypto } from '@otplib/plugin-crypto-node';\n *\n * const hmac = crypto.hmac('sha1', key, data);\n * ```\n */\nexport const crypto: CryptoPlugin = Object.freeze(new NodeCryptoPlugin());\n\nexport default NodeCryptoPlugin;\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,sBAAAE,EAAA,WAAAC,EAAA,YAAAC,IAAA,eAAAC,EAAAL,GAAA,IAAAM,EAAyD,kBAEzDC,EAA0E,wBAmB7DL,EAAN,KAA+C,CAI3C,KAAO,OAYhB,KAAKM,EAAyCC,EAAiBC,EAA8B,CAC3F,IAAMC,KAAO,cAAWH,EAAWC,CAAG,EACtC,OAAAE,EAAK,OAAOD,CAAI,EACT,IAAI,WAAWC,EAAK,OAAO,CAAC,CACrC,CAUA,YAAYC,EAA4B,CACtC,OAAO,IAAI,cAAW,eAAYA,CAAM,CAAC,CAC3C,CAYA,kBAAkBC,EAAwBC,EAAiC,CACzE,IAAMC,KAAO,iBAAcF,CAAC,EACtBG,KAAO,iBAAcF,CAAC,EAE5B,SAAK,2BAAwBC,EAAMC,CAAI,KAIhC,mBAAgBD,EAAMC,CAAI,EAHxB,EAIX,CACF,EAYab,EAAuB,OAAO,OAAO,IAAID,CAAkB,EAEjEE,EAAQF","names":["index_exports","__export","NodeCryptoPlugin","crypto","index_default","__toCommonJS","import_node_crypto","import_core","algorithm","key","data","hmac","length","a","b","bufA","bufB"]}
@@ -0,0 +1,69 @@
1
+ import { CryptoPlugin } from '@otplib/core';
2
+
3
+ /**
4
+ * Node.js crypto module implementation of CryptoPlugin
5
+ *
6
+ * This plugin uses Node.js's built-in crypto module which provides:
7
+ * - OpenSSL-backed HMAC operations
8
+ * - Cryptographically secure random byte generation
9
+ * - Synchronous API for optimal performance
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { NodeCryptoPlugin } from '@otplib/plugin-crypto-node';
14
+ *
15
+ * const crypto = new NodeCryptoPlugin();
16
+ * const hmac = await crypto.hmac('sha1', key, data);
17
+ * const random = crypto.randomBytes(20);
18
+ * ```
19
+ */
20
+ declare class NodeCryptoPlugin implements CryptoPlugin {
21
+ /**
22
+ * Plugin name for identification
23
+ */
24
+ readonly name = "node";
25
+ /**
26
+ * Compute HMAC using Node.js crypto module
27
+ *
28
+ * Synchronous implementation using createHmac.
29
+ *
30
+ * @param algorithm - Hash algorithm to use
31
+ * @param key - Secret key
32
+ * @param data - Data to authenticate
33
+ * @returns HMAC digest
34
+ */
35
+ hmac(algorithm: "sha1" | "sha256" | "sha512", key: Uint8Array, data: Uint8Array): Uint8Array;
36
+ /**
37
+ * Generate cryptographically secure random bytes
38
+ *
39
+ * Uses Node.js's randomBytes which is backed by OpenSSL.
40
+ *
41
+ * @param length - Number of bytes to generate
42
+ * @returns Random bytes
43
+ */
44
+ randomBytes(length: number): Uint8Array;
45
+ /**
46
+ * Constant-time comparison using Node.js crypto.timingSafeEqual
47
+ *
48
+ * Uses Node.js's built-in timing-safe comparison which prevents
49
+ * timing side-channel attacks.
50
+ *
51
+ * @param a - First value to compare
52
+ * @param b - Second value to compare
53
+ * @returns true if values are equal, false otherwise
54
+ */
55
+ constantTimeEqual(a: string | Uint8Array, b: string | Uint8Array): boolean;
56
+ }
57
+ /**
58
+ * Default singleton instance for convenience
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * import { crypto } from '@otplib/plugin-crypto-node';
63
+ *
64
+ * const hmac = crypto.hmac('sha1', key, data);
65
+ * ```
66
+ */
67
+ declare const crypto: CryptoPlugin;
68
+
69
+ export { NodeCryptoPlugin, crypto, NodeCryptoPlugin as default };
@@ -0,0 +1,69 @@
1
+ import { CryptoPlugin } from '@otplib/core';
2
+
3
+ /**
4
+ * Node.js crypto module implementation of CryptoPlugin
5
+ *
6
+ * This plugin uses Node.js's built-in crypto module which provides:
7
+ * - OpenSSL-backed HMAC operations
8
+ * - Cryptographically secure random byte generation
9
+ * - Synchronous API for optimal performance
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { NodeCryptoPlugin } from '@otplib/plugin-crypto-node';
14
+ *
15
+ * const crypto = new NodeCryptoPlugin();
16
+ * const hmac = await crypto.hmac('sha1', key, data);
17
+ * const random = crypto.randomBytes(20);
18
+ * ```
19
+ */
20
+ declare class NodeCryptoPlugin implements CryptoPlugin {
21
+ /**
22
+ * Plugin name for identification
23
+ */
24
+ readonly name = "node";
25
+ /**
26
+ * Compute HMAC using Node.js crypto module
27
+ *
28
+ * Synchronous implementation using createHmac.
29
+ *
30
+ * @param algorithm - Hash algorithm to use
31
+ * @param key - Secret key
32
+ * @param data - Data to authenticate
33
+ * @returns HMAC digest
34
+ */
35
+ hmac(algorithm: "sha1" | "sha256" | "sha512", key: Uint8Array, data: Uint8Array): Uint8Array;
36
+ /**
37
+ * Generate cryptographically secure random bytes
38
+ *
39
+ * Uses Node.js's randomBytes which is backed by OpenSSL.
40
+ *
41
+ * @param length - Number of bytes to generate
42
+ * @returns Random bytes
43
+ */
44
+ randomBytes(length: number): Uint8Array;
45
+ /**
46
+ * Constant-time comparison using Node.js crypto.timingSafeEqual
47
+ *
48
+ * Uses Node.js's built-in timing-safe comparison which prevents
49
+ * timing side-channel attacks.
50
+ *
51
+ * @param a - First value to compare
52
+ * @param b - Second value to compare
53
+ * @returns true if values are equal, false otherwise
54
+ */
55
+ constantTimeEqual(a: string | Uint8Array, b: string | Uint8Array): boolean;
56
+ }
57
+ /**
58
+ * Default singleton instance for convenience
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * import { crypto } from '@otplib/plugin-crypto-node';
63
+ *
64
+ * const hmac = crypto.hmac('sha1', key, data);
65
+ * ```
66
+ */
67
+ declare const crypto: CryptoPlugin;
68
+
69
+ export { NodeCryptoPlugin, crypto, NodeCryptoPlugin as default };
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import{createHmac as o,randomBytes as s,timingSafeEqual as y}from"crypto";import{stringToBytes as i,validateByteLengthEqual as m}from"@otplib/core";var e=class{name="node";hmac(r,a,n){let t=o(r,a);return t.update(n),new Uint8Array(t.digest())}randomBytes(r){return new Uint8Array(s(r))}constantTimeEqual(r,a){let n=i(r),t=i(a);return m(n,t)?y(n,t):!1}},p=Object.freeze(new e),f=e;export{e as NodeCryptoPlugin,p as crypto,f as default};
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { createHmac, randomBytes, timingSafeEqual } from \"node:crypto\";\n\nimport { stringToBytes, validateByteLengthEqual, type CryptoPlugin } from \"@otplib/core\";\n\n/**\n * Node.js crypto module implementation of CryptoPlugin\n *\n * This plugin uses Node.js's built-in crypto module which provides:\n * - OpenSSL-backed HMAC operations\n * - Cryptographically secure random byte generation\n * - Synchronous API for optimal performance\n *\n * @example\n * ```ts\n * import { NodeCryptoPlugin } from '@otplib/plugin-crypto-node';\n *\n * const crypto = new NodeCryptoPlugin();\n * const hmac = await crypto.hmac('sha1', key, data);\n * const random = crypto.randomBytes(20);\n * ```\n */\nexport class NodeCryptoPlugin implements CryptoPlugin {\n /**\n * Plugin name for identification\n */\n readonly name = \"node\";\n\n /**\n * Compute HMAC using Node.js crypto module\n *\n * Synchronous implementation using createHmac.\n *\n * @param algorithm - Hash algorithm to use\n * @param key - Secret key\n * @param data - Data to authenticate\n * @returns HMAC digest\n */\n hmac(algorithm: \"sha1\" | \"sha256\" | \"sha512\", key: Uint8Array, data: Uint8Array): Uint8Array {\n const hmac = createHmac(algorithm, key);\n hmac.update(data);\n return new Uint8Array(hmac.digest());\n }\n\n /**\n * Generate cryptographically secure random bytes\n *\n * Uses Node.js's randomBytes which is backed by OpenSSL.\n *\n * @param length - Number of bytes to generate\n * @returns Random bytes\n */\n randomBytes(length: number): Uint8Array {\n return new Uint8Array(randomBytes(length));\n }\n\n /**\n * Constant-time comparison using Node.js crypto.timingSafeEqual\n *\n * Uses Node.js's built-in timing-safe comparison which prevents\n * timing side-channel attacks.\n *\n * @param a - First value to compare\n * @param b - Second value to compare\n * @returns true if values are equal, false otherwise\n */\n constantTimeEqual(a: string | Uint8Array, b: string | Uint8Array): boolean {\n const bufA = stringToBytes(a);\n const bufB = stringToBytes(b);\n\n if (!validateByteLengthEqual(bufA, bufB)) {\n return false;\n }\n\n return timingSafeEqual(bufA, bufB);\n }\n}\n\n/**\n * Default singleton instance for convenience\n *\n * @example\n * ```ts\n * import { crypto } from '@otplib/plugin-crypto-node';\n *\n * const hmac = crypto.hmac('sha1', key, data);\n * ```\n */\nexport const crypto: CryptoPlugin = Object.freeze(new NodeCryptoPlugin());\n\nexport default NodeCryptoPlugin;\n"],"mappings":"AAAA,OAAS,cAAAA,EAAY,eAAAC,EAAa,mBAAAC,MAAuB,SAEzD,OAAS,iBAAAC,EAAe,2BAAAC,MAAkD,eAmBnE,IAAMC,EAAN,KAA+C,CAI3C,KAAO,OAYhB,KAAKC,EAAyCC,EAAiBC,EAA8B,CAC3F,IAAMC,EAAOT,EAAWM,EAAWC,CAAG,EACtC,OAAAE,EAAK,OAAOD,CAAI,EACT,IAAI,WAAWC,EAAK,OAAO,CAAC,CACrC,CAUA,YAAYC,EAA4B,CACtC,OAAO,IAAI,WAAWT,EAAYS,CAAM,CAAC,CAC3C,CAYA,kBAAkBC,EAAwBC,EAAiC,CACzE,IAAMC,EAAOV,EAAcQ,CAAC,EACtBG,EAAOX,EAAcS,CAAC,EAE5B,OAAKR,EAAwBS,EAAMC,CAAI,EAIhCZ,EAAgBW,EAAMC,CAAI,EAHxB,EAIX,CACF,EAYaC,EAAuB,OAAO,OAAO,IAAIV,CAAkB,EAEjEW,EAAQX","names":["createHmac","randomBytes","timingSafeEqual","stringToBytes","validateByteLengthEqual","NodeCryptoPlugin","algorithm","key","data","hmac","length","a","b","bufA","bufB","crypto","index_default"]}
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@otplib/plugin-crypto-node",
3
+ "version": "13.0.0",
4
+ "description": "Node.js crypto module adapter for otplib",
5
+ "license": "MIT",
6
+ "author": "Gerald Yeo <support@yeojz.dev>",
7
+ "homepage": "https://otplib.yeojz.dev",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/yeojz/otplib.git",
11
+ "directory": "packages/plugin-crypto-node"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/yeojz/otplib/issues"
15
+ },
16
+ "keywords": [
17
+ "otp",
18
+ "crypto",
19
+ "nodejs",
20
+ "hmac",
21
+ "plugin"
22
+ ],
23
+ "sideEffects": false,
24
+ "type": "module",
25
+ "main": "./dist/index.cjs",
26
+ "module": "./dist/index.js",
27
+ "types": "./dist/index.d.ts",
28
+ "exports": {
29
+ ".": {
30
+ "import": {
31
+ "types": "./dist/index.d.ts",
32
+ "default": "./dist/index.js"
33
+ },
34
+ "require": {
35
+ "types": "./dist/index.d.cts",
36
+ "default": "./dist/index.cjs"
37
+ }
38
+ }
39
+ },
40
+ "files": [
41
+ "dist",
42
+ "README.md",
43
+ "LICENSE"
44
+ ],
45
+ "dependencies": {
46
+ "@noble/hashes": "^1.3.3",
47
+ "@otplib/core": "13.0.0"
48
+ },
49
+ "devDependencies": {
50
+ "tsup": "^8.0.1",
51
+ "typescript": "^5.3.3",
52
+ "vitest": "^4.0.16"
53
+ },
54
+ "publishConfig": {
55
+ "access": "public"
56
+ },
57
+ "scripts": {
58
+ "build": "tsup",
59
+ "dev": "tsup --watch",
60
+ "test": "vitest",
61
+ "test:ci": "vitest run --coverage",
62
+ "typecheck": "tsc --noEmit",
63
+ "lint": "eslint src/",
64
+ "clean": "rm -rf dist .tsbuildinfo"
65
+ }
66
+ }