@otplib/plugin-crypto-web 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 +21 -0
- package/README.md +135 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +69 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +66 -0
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,135 @@
|
|
|
1
|
+
# @otplib/plugin-crypto-web
|
|
2
|
+
|
|
3
|
+
Web Crypto API plugin for otplib, compatible with browsers and edge runtimes.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @otplib/plugin-crypto-web
|
|
9
|
+
pnpm add @otplib/plugin-crypto-web
|
|
10
|
+
yarn add @otplib/plugin-crypto-web
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Overview
|
|
14
|
+
|
|
15
|
+
This plugin provides HMAC and random byte generation using the Web Crypto API. It supports all hash algorithms available in modern browsers:
|
|
16
|
+
|
|
17
|
+
- `SHA-1`
|
|
18
|
+
- `SHA-256`
|
|
19
|
+
- `SHA-512`
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Basic Usage
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { generateSecret, generate } from "otplib";
|
|
27
|
+
import { WebCryptoPlugin } from "@otplib/plugin-crypto-web";
|
|
28
|
+
import { ScureBase32Plugin } from "@otplib/plugin-base32-scure";
|
|
29
|
+
|
|
30
|
+
const crypto = new WebCryptoPlugin();
|
|
31
|
+
const base32 = new ScureBase32Plugin();
|
|
32
|
+
|
|
33
|
+
// Generate a secret
|
|
34
|
+
const secret = await 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 { WebCryptoPlugin } from "@otplib/plugin-crypto-web";
|
|
49
|
+
import { ScureBase32Plugin } from "@otplib/plugin-base32-scure";
|
|
50
|
+
|
|
51
|
+
const crypto = new WebCryptoPlugin();
|
|
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
|
+
### Asynchronous Operations
|
|
63
|
+
|
|
64
|
+
The Web Crypto API only supports asynchronous operations:
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { WebCryptoPlugin } from "@otplib/plugin-crypto-web";
|
|
68
|
+
|
|
69
|
+
const crypto = new WebCryptoPlugin();
|
|
70
|
+
|
|
71
|
+
// Async HMAC (required by Web Crypto API)
|
|
72
|
+
const digest = await crypto.hmac("SHA-1", key, data);
|
|
73
|
+
|
|
74
|
+
// Async random bytes
|
|
75
|
+
const bytes = await crypto.randomBytes(20);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Browser Compatibility
|
|
79
|
+
|
|
80
|
+
| Browser | SHA-1 | SHA-256 | SHA-512 |
|
|
81
|
+
| ----------- | ----- | ------- | ------- |
|
|
82
|
+
| Chrome 37+ | Yes | Yes | Yes |
|
|
83
|
+
| Firefox 34+ | Yes | Yes | Yes |
|
|
84
|
+
| Safari 7+ | Yes | Yes | Yes |
|
|
85
|
+
| Edge 79+ | Yes | Yes | Yes |
|
|
86
|
+
| IE 11 | No | No | No |
|
|
87
|
+
|
|
88
|
+
## Edge Runtime Support
|
|
89
|
+
|
|
90
|
+
Works in Cloudflare Workers, Vercel Edge Functions, Deno, and other edge runtimes that implement Web Crypto API:
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
// Cloudflare Worker example
|
|
94
|
+
import { WebCryptoPlugin } from "@otplib/plugin-crypto-web";
|
|
95
|
+
|
|
96
|
+
export default {
|
|
97
|
+
async fetch(request) {
|
|
98
|
+
const crypto = new WebCryptoPlugin();
|
|
99
|
+
// Use crypto for OTP operations
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## When to Use
|
|
105
|
+
|
|
106
|
+
Use this plugin when:
|
|
107
|
+
|
|
108
|
+
- Running in browsers (Chrome, Firefox, Safari, Edge)
|
|
109
|
+
- Using edge runtimes (Cloudflare Workers, Vercel Edge)
|
|
110
|
+
- Building web applications with React, Vue, etc.
|
|
111
|
+
- Need cross-platform crypto support
|
|
112
|
+
- Want modern browser-native crypto (no external dependencies)
|
|
113
|
+
|
|
114
|
+
## Performance
|
|
115
|
+
|
|
116
|
+
- All operations return Promises (asynchronous only)
|
|
117
|
+
- Uses native browser crypto implementations
|
|
118
|
+
- Uses OS-level crypto primitives
|
|
119
|
+
|
|
120
|
+
## Limitations
|
|
121
|
+
|
|
122
|
+
- Only supports asynchronous operations (no sync HMAC)
|
|
123
|
+
- Not available in Node.js (use `@otplib/plugin-crypto-node` instead)
|
|
124
|
+
- Requires modern browser with Web Crypto API support
|
|
125
|
+
|
|
126
|
+
## Documentation
|
|
127
|
+
|
|
128
|
+
Full documentation available at [otplib.yeojz.dev](https://otplib.yeojz.dev):
|
|
129
|
+
|
|
130
|
+
- [Getting Started Guide](https://otplib.yeojz.dev/guide/getting-started)
|
|
131
|
+
- [API Reference](https://otplib.yeojz.dev/api/)
|
|
132
|
+
|
|
133
|
+
## License
|
|
134
|
+
|
|
135
|
+
[MIT](./LICENSE) © 2026 Gerald Yeo
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var o=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var h=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var c=(t,e)=>{for(var r in e)o(t,r,{get:e[r],enumerable:!0})},m=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of h(e))!A.call(t,n)&&n!==r&&o(t,n,{get:()=>e[n],enumerable:!(s=b(e,n))||s.enumerable});return t};var g=t=>m(o({},"__esModule",{value:!0}),t);var C={};c(C,{WebCryptoPlugin:()=>a,crypto:()=>w,default:()=>U});module.exports=g(C);var y=require("@otplib/core"),p={sha1:"SHA-1",sha256:"SHA-256",sha512:"SHA-512"};function i(t){return t.byteOffset===0&&t.byteLength===t.buffer.byteLength?t.buffer:t.buffer.slice(t.byteOffset,t.byteOffset+t.byteLength)}var a=class{name="web";async hmac(e,r,s){let n=globalThis.crypto;if(!n?.subtle)throw new Error("Web Crypto API is not available in this environment");let l=p[e],f=await n.subtle.importKey("raw",i(r),{name:"HMAC",hash:l},!1,["sign"]),u=await n.subtle.sign("HMAC",f,i(s));return new Uint8Array(u)}randomBytes(e){let r=globalThis.crypto;if(!r?.getRandomValues)throw new Error("Web Crypto API getRandomValues is not available in this environment");let s=new Uint8Array(e);return r.getRandomValues(s),s}constantTimeEqual(e,r){return(0,y.constantTimeEqual)(e,r)}},w=Object.freeze(new a),U=a;0&&(module.exports={WebCryptoPlugin,crypto});
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { constantTimeEqual as constantTimeEqualUtil } from \"@otplib/core\";\n\nimport type { CryptoPlugin, HashAlgorithm } from \"@otplib/core\";\n\n/**\n * Web Crypto algorithm name mapping\n *\n * Maps our algorithm names to Web Crypto API algorithm identifiers.\n */\nconst ALGORITHM_MAP = {\n sha1: \"SHA-1\",\n sha256: \"SHA-256\",\n sha512: \"SHA-512\",\n} as const satisfies Record<HashAlgorithm, string>;\n\n/**\n * Get ArrayBuffer from Uint8Array, avoiding copy when possible\n *\n * Only slices when the Uint8Array is a view into a larger buffer.\n * When the array owns its buffer entirely, returns buffer directly.\n */\nfunction getArrayBuffer(arr: Uint8Array): ArrayBuffer {\n if (arr.byteOffset === 0 && arr.byteLength === arr.buffer.byteLength) {\n return arr.buffer as ArrayBuffer;\n }\n return arr.buffer.slice(arr.byteOffset, arr.byteOffset + arr.byteLength) as ArrayBuffer;\n}\n\n/**\n * Web Crypto API implementation of CryptoPlugin\n *\n * This plugin uses the browser's native Web Crypto API which provides:\n * - Hardware-accelerated cryptographic operations\n * - Secure key storage and generation\n * - Async API for non-blocking operations\n *\n * @example\n * ```ts\n * import { WebCryptoPlugin } from '@otplib/plugin-crypto-web';\n *\n * const crypto = new WebCryptoPlugin();\n * const hmac = await crypto.hmac('sha1', key, data);\n * const random = crypto.randomBytes(20);\n * ```\n */\nexport class WebCryptoPlugin implements CryptoPlugin {\n /**\n * Plugin name for identification\n */\n readonly name = \"web\";\n\n /**\n * Compute HMAC using Web Crypto API\n *\n * Async implementation using SubtleCrypto.\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 async hmac(\n algorithm: \"sha1\" | \"sha256\" | \"sha512\",\n key: Uint8Array,\n data: Uint8Array,\n ): Promise<Uint8Array> {\n const webCrypto = globalThis.crypto;\n\n if (!webCrypto?.subtle) {\n throw new Error(\"Web Crypto API is not available in this environment\");\n }\n\n const hashAlgorithm = ALGORITHM_MAP[algorithm];\n\n const cryptoKey = await webCrypto.subtle.importKey(\n \"raw\",\n getArrayBuffer(key),\n { name: \"HMAC\", hash: hashAlgorithm },\n false,\n [\"sign\"],\n );\n\n const signature = await webCrypto.subtle.sign(\"HMAC\", cryptoKey, getArrayBuffer(data));\n\n return new Uint8Array(signature);\n }\n\n /**\n * Generate cryptographically secure random bytes\n *\n * Uses Web Crypto API's getRandomValues.\n *\n * @param length - Number of bytes to generate\n * @returns Random bytes\n */\n randomBytes(length: number): Uint8Array {\n const webCrypto = globalThis.crypto;\n\n if (!webCrypto?.getRandomValues) {\n throw new Error(\"Web Crypto API getRandomValues is not available in this environment\");\n }\n\n const bytes = new Uint8Array(length);\n webCrypto.getRandomValues(bytes);\n return bytes;\n }\n\n /**\n * Constant-time comparison to prevent timing side-channel attacks\n *\n * Web Crypto API doesn't provide a built-in constant-time comparison,\n * so we use the core utility implementation.\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 return constantTimeEqualUtil(a, b);\n }\n}\n\n/**\n * Default singleton instance for convenience\n *\n * @example\n * ```ts\n * import { crypto } from '@otplib/plugin-crypto-web';\n *\n * const hmac = await crypto.hmac('sha1', key, data);\n * ```\n */\nexport const crypto: CryptoPlugin = Object.freeze(new WebCryptoPlugin());\n\nexport default WebCryptoPlugin;\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,qBAAAE,EAAA,WAAAC,EAAA,YAAAC,IAAA,eAAAC,EAAAL,GAAA,IAAAM,EAA2D,wBASrDC,EAAgB,CACpB,KAAM,QACN,OAAQ,UACR,OAAQ,SACV,EAQA,SAASC,EAAeC,EAA8B,CACpD,OAAIA,EAAI,aAAe,GAAKA,EAAI,aAAeA,EAAI,OAAO,WACjDA,EAAI,OAENA,EAAI,OAAO,MAAMA,EAAI,WAAYA,EAAI,WAAaA,EAAI,UAAU,CACzE,CAmBO,IAAMP,EAAN,KAA8C,CAI1C,KAAO,MAYhB,MAAM,KACJQ,EACAC,EACAC,EACqB,CACrB,IAAMC,EAAY,WAAW,OAE7B,GAAI,CAACA,GAAW,OACd,MAAM,IAAI,MAAM,qDAAqD,EAGvE,IAAMC,EAAgBP,EAAcG,CAAS,EAEvCK,EAAY,MAAMF,EAAU,OAAO,UACvC,MACAL,EAAeG,CAAG,EAClB,CAAE,KAAM,OAAQ,KAAMG,CAAc,EACpC,GACA,CAAC,MAAM,CACT,EAEME,EAAY,MAAMH,EAAU,OAAO,KAAK,OAAQE,EAAWP,EAAeI,CAAI,CAAC,EAErF,OAAO,IAAI,WAAWI,CAAS,CACjC,CAUA,YAAYC,EAA4B,CACtC,IAAMJ,EAAY,WAAW,OAE7B,GAAI,CAACA,GAAW,gBACd,MAAM,IAAI,MAAM,qEAAqE,EAGvF,IAAMK,EAAQ,IAAI,WAAWD,CAAM,EACnC,OAAAJ,EAAU,gBAAgBK,CAAK,EACxBA,CACT,CAYA,kBAAkBC,EAAwBC,EAAiC,CACzE,SAAO,EAAAC,mBAAsBF,EAAGC,CAAC,CACnC,CACF,EAYajB,EAAuB,OAAO,OAAO,IAAID,CAAiB,EAEhEE,EAAQF","names":["index_exports","__export","WebCryptoPlugin","crypto","index_default","__toCommonJS","import_core","ALGORITHM_MAP","getArrayBuffer","arr","algorithm","key","data","webCrypto","hashAlgorithm","cryptoKey","signature","length","bytes","a","b","constantTimeEqualUtil"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { CryptoPlugin } from '@otplib/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Web Crypto API implementation of CryptoPlugin
|
|
5
|
+
*
|
|
6
|
+
* This plugin uses the browser's native Web Crypto API which provides:
|
|
7
|
+
* - Hardware-accelerated cryptographic operations
|
|
8
|
+
* - Secure key storage and generation
|
|
9
|
+
* - Async API for non-blocking operations
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { WebCryptoPlugin } from '@otplib/plugin-crypto-web';
|
|
14
|
+
*
|
|
15
|
+
* const crypto = new WebCryptoPlugin();
|
|
16
|
+
* const hmac = await crypto.hmac('sha1', key, data);
|
|
17
|
+
* const random = crypto.randomBytes(20);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
declare class WebCryptoPlugin implements CryptoPlugin {
|
|
21
|
+
/**
|
|
22
|
+
* Plugin name for identification
|
|
23
|
+
*/
|
|
24
|
+
readonly name = "web";
|
|
25
|
+
/**
|
|
26
|
+
* Compute HMAC using Web Crypto API
|
|
27
|
+
*
|
|
28
|
+
* Async implementation using SubtleCrypto.
|
|
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): Promise<Uint8Array>;
|
|
36
|
+
/**
|
|
37
|
+
* Generate cryptographically secure random bytes
|
|
38
|
+
*
|
|
39
|
+
* Uses Web Crypto API's getRandomValues.
|
|
40
|
+
*
|
|
41
|
+
* @param length - Number of bytes to generate
|
|
42
|
+
* @returns Random bytes
|
|
43
|
+
*/
|
|
44
|
+
randomBytes(length: number): Uint8Array;
|
|
45
|
+
/**
|
|
46
|
+
* Constant-time comparison to prevent timing side-channel attacks
|
|
47
|
+
*
|
|
48
|
+
* Web Crypto API doesn't provide a built-in constant-time comparison,
|
|
49
|
+
* so we use the core utility implementation.
|
|
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-web';
|
|
63
|
+
*
|
|
64
|
+
* const hmac = await crypto.hmac('sha1', key, data);
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
declare const crypto: CryptoPlugin;
|
|
68
|
+
|
|
69
|
+
export { WebCryptoPlugin, crypto, WebCryptoPlugin as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { CryptoPlugin } from '@otplib/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Web Crypto API implementation of CryptoPlugin
|
|
5
|
+
*
|
|
6
|
+
* This plugin uses the browser's native Web Crypto API which provides:
|
|
7
|
+
* - Hardware-accelerated cryptographic operations
|
|
8
|
+
* - Secure key storage and generation
|
|
9
|
+
* - Async API for non-blocking operations
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { WebCryptoPlugin } from '@otplib/plugin-crypto-web';
|
|
14
|
+
*
|
|
15
|
+
* const crypto = new WebCryptoPlugin();
|
|
16
|
+
* const hmac = await crypto.hmac('sha1', key, data);
|
|
17
|
+
* const random = crypto.randomBytes(20);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
declare class WebCryptoPlugin implements CryptoPlugin {
|
|
21
|
+
/**
|
|
22
|
+
* Plugin name for identification
|
|
23
|
+
*/
|
|
24
|
+
readonly name = "web";
|
|
25
|
+
/**
|
|
26
|
+
* Compute HMAC using Web Crypto API
|
|
27
|
+
*
|
|
28
|
+
* Async implementation using SubtleCrypto.
|
|
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): Promise<Uint8Array>;
|
|
36
|
+
/**
|
|
37
|
+
* Generate cryptographically secure random bytes
|
|
38
|
+
*
|
|
39
|
+
* Uses Web Crypto API's getRandomValues.
|
|
40
|
+
*
|
|
41
|
+
* @param length - Number of bytes to generate
|
|
42
|
+
* @returns Random bytes
|
|
43
|
+
*/
|
|
44
|
+
randomBytes(length: number): Uint8Array;
|
|
45
|
+
/**
|
|
46
|
+
* Constant-time comparison to prevent timing side-channel attacks
|
|
47
|
+
*
|
|
48
|
+
* Web Crypto API doesn't provide a built-in constant-time comparison,
|
|
49
|
+
* so we use the core utility implementation.
|
|
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-web';
|
|
63
|
+
*
|
|
64
|
+
* const hmac = await crypto.hmac('sha1', key, data);
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
declare const crypto: CryptoPlugin;
|
|
68
|
+
|
|
69
|
+
export { WebCryptoPlugin, crypto, WebCryptoPlugin as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{constantTimeEqual as f}from"@otplib/core";var u={sha1:"SHA-1",sha256:"SHA-256",sha512:"SHA-512"};function o(t){return t.byteOffset===0&&t.byteLength===t.buffer.byteLength?t.buffer:t.buffer.slice(t.byteOffset,t.byteOffset+t.byteLength)}var s=class{name="web";async hmac(r,e,n){let a=globalThis.crypto;if(!a?.subtle)throw new Error("Web Crypto API is not available in this environment");let i=u[r],y=await a.subtle.importKey("raw",o(e),{name:"HMAC",hash:i},!1,["sign"]),l=await a.subtle.sign("HMAC",y,o(n));return new Uint8Array(l)}randomBytes(r){let e=globalThis.crypto;if(!e?.getRandomValues)throw new Error("Web Crypto API getRandomValues is not available in this environment");let n=new Uint8Array(r);return e.getRandomValues(n),n}constantTimeEqual(r,e){return f(r,e)}},h=Object.freeze(new s),A=s;export{s as WebCryptoPlugin,h as crypto,A as default};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { constantTimeEqual as constantTimeEqualUtil } from \"@otplib/core\";\n\nimport type { CryptoPlugin, HashAlgorithm } from \"@otplib/core\";\n\n/**\n * Web Crypto algorithm name mapping\n *\n * Maps our algorithm names to Web Crypto API algorithm identifiers.\n */\nconst ALGORITHM_MAP = {\n sha1: \"SHA-1\",\n sha256: \"SHA-256\",\n sha512: \"SHA-512\",\n} as const satisfies Record<HashAlgorithm, string>;\n\n/**\n * Get ArrayBuffer from Uint8Array, avoiding copy when possible\n *\n * Only slices when the Uint8Array is a view into a larger buffer.\n * When the array owns its buffer entirely, returns buffer directly.\n */\nfunction getArrayBuffer(arr: Uint8Array): ArrayBuffer {\n if (arr.byteOffset === 0 && arr.byteLength === arr.buffer.byteLength) {\n return arr.buffer as ArrayBuffer;\n }\n return arr.buffer.slice(arr.byteOffset, arr.byteOffset + arr.byteLength) as ArrayBuffer;\n}\n\n/**\n * Web Crypto API implementation of CryptoPlugin\n *\n * This plugin uses the browser's native Web Crypto API which provides:\n * - Hardware-accelerated cryptographic operations\n * - Secure key storage and generation\n * - Async API for non-blocking operations\n *\n * @example\n * ```ts\n * import { WebCryptoPlugin } from '@otplib/plugin-crypto-web';\n *\n * const crypto = new WebCryptoPlugin();\n * const hmac = await crypto.hmac('sha1', key, data);\n * const random = crypto.randomBytes(20);\n * ```\n */\nexport class WebCryptoPlugin implements CryptoPlugin {\n /**\n * Plugin name for identification\n */\n readonly name = \"web\";\n\n /**\n * Compute HMAC using Web Crypto API\n *\n * Async implementation using SubtleCrypto.\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 async hmac(\n algorithm: \"sha1\" | \"sha256\" | \"sha512\",\n key: Uint8Array,\n data: Uint8Array,\n ): Promise<Uint8Array> {\n const webCrypto = globalThis.crypto;\n\n if (!webCrypto?.subtle) {\n throw new Error(\"Web Crypto API is not available in this environment\");\n }\n\n const hashAlgorithm = ALGORITHM_MAP[algorithm];\n\n const cryptoKey = await webCrypto.subtle.importKey(\n \"raw\",\n getArrayBuffer(key),\n { name: \"HMAC\", hash: hashAlgorithm },\n false,\n [\"sign\"],\n );\n\n const signature = await webCrypto.subtle.sign(\"HMAC\", cryptoKey, getArrayBuffer(data));\n\n return new Uint8Array(signature);\n }\n\n /**\n * Generate cryptographically secure random bytes\n *\n * Uses Web Crypto API's getRandomValues.\n *\n * @param length - Number of bytes to generate\n * @returns Random bytes\n */\n randomBytes(length: number): Uint8Array {\n const webCrypto = globalThis.crypto;\n\n if (!webCrypto?.getRandomValues) {\n throw new Error(\"Web Crypto API getRandomValues is not available in this environment\");\n }\n\n const bytes = new Uint8Array(length);\n webCrypto.getRandomValues(bytes);\n return bytes;\n }\n\n /**\n * Constant-time comparison to prevent timing side-channel attacks\n *\n * Web Crypto API doesn't provide a built-in constant-time comparison,\n * so we use the core utility implementation.\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 return constantTimeEqualUtil(a, b);\n }\n}\n\n/**\n * Default singleton instance for convenience\n *\n * @example\n * ```ts\n * import { crypto } from '@otplib/plugin-crypto-web';\n *\n * const hmac = await crypto.hmac('sha1', key, data);\n * ```\n */\nexport const crypto: CryptoPlugin = Object.freeze(new WebCryptoPlugin());\n\nexport default WebCryptoPlugin;\n"],"mappings":"AAAA,OAAS,qBAAqBA,MAA6B,eAS3D,IAAMC,EAAgB,CACpB,KAAM,QACN,OAAQ,UACR,OAAQ,SACV,EAQA,SAASC,EAAeC,EAA8B,CACpD,OAAIA,EAAI,aAAe,GAAKA,EAAI,aAAeA,EAAI,OAAO,WACjDA,EAAI,OAENA,EAAI,OAAO,MAAMA,EAAI,WAAYA,EAAI,WAAaA,EAAI,UAAU,CACzE,CAmBO,IAAMC,EAAN,KAA8C,CAI1C,KAAO,MAYhB,MAAM,KACJC,EACAC,EACAC,EACqB,CACrB,IAAMC,EAAY,WAAW,OAE7B,GAAI,CAACA,GAAW,OACd,MAAM,IAAI,MAAM,qDAAqD,EAGvE,IAAMC,EAAgBR,EAAcI,CAAS,EAEvCK,EAAY,MAAMF,EAAU,OAAO,UACvC,MACAN,EAAeI,CAAG,EAClB,CAAE,KAAM,OAAQ,KAAMG,CAAc,EACpC,GACA,CAAC,MAAM,CACT,EAEME,EAAY,MAAMH,EAAU,OAAO,KAAK,OAAQE,EAAWR,EAAeK,CAAI,CAAC,EAErF,OAAO,IAAI,WAAWI,CAAS,CACjC,CAUA,YAAYC,EAA4B,CACtC,IAAMJ,EAAY,WAAW,OAE7B,GAAI,CAACA,GAAW,gBACd,MAAM,IAAI,MAAM,qEAAqE,EAGvF,IAAMK,EAAQ,IAAI,WAAWD,CAAM,EACnC,OAAAJ,EAAU,gBAAgBK,CAAK,EACxBA,CACT,CAYA,kBAAkBC,EAAwBC,EAAiC,CACzE,OAAOf,EAAsBc,EAAGC,CAAC,CACnC,CACF,EAYaC,EAAuB,OAAO,OAAO,IAAIZ,CAAiB,EAEhEa,EAAQb","names":["constantTimeEqualUtil","ALGORITHM_MAP","getArrayBuffer","arr","WebCryptoPlugin","algorithm","key","data","webCrypto","hashAlgorithm","cryptoKey","signature","length","bytes","a","b","crypto","index_default"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@otplib/plugin-crypto-web",
|
|
3
|
+
"version": "13.0.0",
|
|
4
|
+
"description": "Web Crypto API 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-web"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/yeojz/otplib/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"otp",
|
|
18
|
+
"crypto",
|
|
19
|
+
"webcrypto",
|
|
20
|
+
"browser",
|
|
21
|
+
"hmac",
|
|
22
|
+
"plugin"
|
|
23
|
+
],
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"type": "module",
|
|
26
|
+
"main": "./dist/index.cjs",
|
|
27
|
+
"module": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"default": "./dist/index.js"
|
|
34
|
+
},
|
|
35
|
+
"require": {
|
|
36
|
+
"types": "./dist/index.d.cts",
|
|
37
|
+
"default": "./dist/index.cjs"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"dist",
|
|
43
|
+
"README.md",
|
|
44
|
+
"LICENSE"
|
|
45
|
+
],
|
|
46
|
+
"dependencies": {
|
|
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
|
+
}
|