@sebastienrousseau/crypto-edge 0.0.8
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 +8 -0
- package/README.md +276 -0
- package/dist/detect.d.ts +5 -0
- package/dist/detect.d.ts.map +1 -0
- package/dist/detect.js +62 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/polyfill.d.ts +19 -0
- package/dist/polyfill.d.ts.map +1 -0
- package/dist/polyfill.js +172 -0
- package/dist/types.d.ts +39 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/webcrypto.d.ts +13 -0
- package/dist/webcrypto.d.ts.map +1 -0
- package/dist/webcrypto.js +143 -0
- package/package.json +76 -0
package/LICENSE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
<!-- SPDX-License-Identifier: Apache-2.0 OR MIT -->
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img src="https://raw.githubusercontent.com/sebastienrousseau/crypto-service/main/assets/crypto-edge-logo.svg" alt="crypto-edge logo" width="360" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<h1 align="center">@sebastienrousseau/crypto-edge</h1>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
Edge-runtime cryptographic operations using the Web Crypto API, optimized for Cloudflare Workers, Vercel Edge, and Deno.
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
<p align="center">
|
|
14
|
+
<a href="https://github.com/sebastienrousseau/crypto-service/actions"><img src="https://img.shields.io/github/actions/workflow/status/sebastienrousseau/crypto-service/ci.yml?branch=main&style=for-the-badge&logo=github" alt="Build" /></a>
|
|
15
|
+
<a href="https://coveralls.io/github/sebastienrousseau/crypto-service?branch=main"><img src="https://img.shields.io/coveralls/github/sebastienrousseau/crypto-service?branch=main&style=for-the-badge" alt="Coverage" /></a>
|
|
16
|
+
<a href="https://www.npmjs.com/package/@sebastienrousseau/crypto-edge"><img src="https://img.shields.io/npm/v/@sebastienrousseau/crypto-edge.svg?style=for-the-badge&color=f14041&logo=npm" alt="Registry" /></a>
|
|
17
|
+
<a href="https://sebastienrousseau.github.io/crypto-service/"><img src="https://img.shields.io/badge/docs-TypeDoc-blue.svg?style=for-the-badge&labelColor=555555&logo=typescript" alt="Docs" /></a>
|
|
18
|
+
<a href="https://scorecard.dev/viewer/?uri=github.com/sebastienrousseau/crypto-service" title="ossf-scorecard"><img src="https://img.shields.io/badge/OpenSSF-Scorecard-blue?style=for-the-badge&logo=openssf" alt="OpenSSF Scorecard" /></a>
|
|
19
|
+
<a href="https://github.com/sebastienrousseau/crypto-service/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-Apache--2.0%20OR%20MIT-blue.svg?style=for-the-badge" alt="License: Apache-2.0 OR MIT" /></a>
|
|
20
|
+
<a href="https://github.com/sebastienrousseau/crypto-service/blob/main/docs/POLICIES.md"><img src="https://img.shields.io/badge/Node.js-%3E%3D22-93450a.svg?style=for-the-badge&logo=node.js" alt="Node.js 22 or newer" /></a>
|
|
21
|
+
</p>
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Contents
|
|
26
|
+
|
|
27
|
+
**Getting started**
|
|
28
|
+
|
|
29
|
+
- [Install](#install) — installation via pnpm, npm, or yarn
|
|
30
|
+
- [Requirements](#requirements) — runtime floor and environment prerequisites
|
|
31
|
+
- [Quick Start](#quick-start) — minimal working usage sample
|
|
32
|
+
|
|
33
|
+
**The Crypto Service ecosystem**
|
|
34
|
+
|
|
35
|
+
- [The Crypto Service ecosystem](#the-crypto-service-ecosystem) — full 14-package suite overview
|
|
36
|
+
|
|
37
|
+
**Package reference**
|
|
38
|
+
|
|
39
|
+
- [Reference & Usage](#overview) — features, configuration, and capabilities
|
|
40
|
+
- [Examples](#examples) — runnable sample code
|
|
41
|
+
|
|
42
|
+
**Operational**
|
|
43
|
+
|
|
44
|
+
- [Development](#development) — build, lint, format, and test targets
|
|
45
|
+
- [Security](#security) — vulnerability disclosure and cryptographic invariants
|
|
46
|
+
- [Documentation](#documentation) — TypeDoc API docs and ecosystem guides
|
|
47
|
+
- [Stability guarantees](#stability-guarantees) — SemVer axis and release policy
|
|
48
|
+
- [License](#license)
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Install
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pnpm add @sebastienrousseau/crypto-edge
|
|
56
|
+
# or
|
|
57
|
+
npm install @sebastienrousseau/crypto-edge
|
|
58
|
+
# or
|
|
59
|
+
yarn add @sebastienrousseau/crypto-edge
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
<p align="right"><a href="#contents">Back to Top</a></p>
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## Requirements
|
|
67
|
+
|
|
68
|
+
- **Node.js**: `^22.0.0` or `>=24.0.0` (active and maintenance LTS releases)
|
|
69
|
+
- **Package Manager**: `pnpm >=9` (recommended) or `npm >=10`
|
|
70
|
+
- **TypeScript**: `>=5.0` (when compiling with TypeScript)
|
|
71
|
+
|
|
72
|
+
<p align="right"><a href="#contents">Back to Top</a></p>
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## Quick Start
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
import { hash, detectRuntime } from "@sebastienrousseau/crypto-edge";
|
|
80
|
+
|
|
81
|
+
console.log("Running on:", detectRuntime());
|
|
82
|
+
|
|
83
|
+
const digest = await hash("SHA-256", "hello world");
|
|
84
|
+
console.log(digest);
|
|
85
|
+
// b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
<p align="right"><a href="#contents">Back to Top</a></p>
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
## The Crypto Service ecosystem
|
|
93
|
+
|
|
94
|
+
Crypto Service provides a complete cryptography stack across 14 specialized packages:
|
|
95
|
+
|
|
96
|
+
| Package | Role | Description |
|
|
97
|
+
| :---------------------------------------------------------------------- | :--------------- | :------------------------------------------------------------------------------------------------------------------------------------- |
|
|
98
|
+
| [`@sebastienrousseau/crypto-api`](../crypto-api) | API Schemas | Shared TypeScript types and utilities for the Crypto Service Suite, defining the canonical API surface. |
|
|
99
|
+
| [`@sebastienrousseau/crypto-cli`](../crypto-cli) | Terminal CLI | An interactive command-line interface for cryptographic operations, supporting both legacy OpenPGP and modern post-quantum algorithms. |
|
|
100
|
+
| **[`@sebastienrousseau/crypto-edge`](../crypto-edge)** _(this package)_ | **Edge Runtime** | **Edge-runtime cryptographic operations using the Web Crypto API, optimized for Cloudflare Workers, Vercel Edge, and Deno.** |
|
|
101
|
+
| [`@sebastienrousseau/crypto-kms`](../crypto-kms) | Cloud KMS | Unified Key Management Service interface for AWS KMS, GCP Cloud KMS, Azure Key Vault, and HashiCorp Vault. |
|
|
102
|
+
| [`@sebastienrousseau/crypto-lib`](../crypto-lib) | Core Library | A modern cryptographic library for TypeScript, with post-quantum support, zero unsafe dependencies, and 100% test coverage. |
|
|
103
|
+
| [`@sebastienrousseau/crypto-middleware`](../crypto-middleware) | Middleware | Framework-agnostic cryptographic middleware for Express, Fastify, and Koa applications. |
|
|
104
|
+
| [`@sebastienrousseau/crypto-prisma`](../crypto-prisma) | ORM Adapter | Transparent field-level encryption extension for Prisma Client, using XChaCha20-Poly1305. |
|
|
105
|
+
| [`@sebastienrousseau/crypto-react`](../crypto-react) | React Hooks | React hooks and context provider for client-side cryptographic operations with zero boilerplate. |
|
|
106
|
+
| [`@sebastienrousseau/crypto-sdk`](../crypto-sdk) | Client SDK | A zero-dependency, typed HTTP client for the Crypto Service REST API, with full post-quantum support. |
|
|
107
|
+
| [`@sebastienrousseau/crypto-server`](../crypto-server) | HTTP API | A hardened Fastify REST API for cryptographic operations, with rate limiting, OpenAPI schemas, and post-quantum endpoints. |
|
|
108
|
+
| [`@sebastienrousseau/crypto-testing`](../crypto-testing) | Test Support | Deterministic keys, fast mocks, and test fixtures for crypto-lib |
|
|
109
|
+
| [`@sebastienrousseau/crypto-typeorm`](../crypto-typeorm) | ORM Adapter | TypeORM column-level encryption with a single decorator, powered by crypto-lib. |
|
|
110
|
+
| [`@sebastienrousseau/crypto-vue`](../crypto-vue) | Vue Composables | Vue 3 composables for client-side cryptography |
|
|
111
|
+
| [`@sebastienrousseau/crypto-wasm`](../crypto-wasm) | Acceleration | WebAssembly performance accelerator for crypto-lib |
|
|
112
|
+
|
|
113
|
+
<p align="right"><a href="#contents">Back to Top</a></p>
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## Overview
|
|
118
|
+
|
|
119
|
+
crypto-edge adapts the Crypto Service Suite for edge and serverless
|
|
120
|
+
runtimes. It provides runtime detection, a Web Crypto wrapper for
|
|
121
|
+
hashing, AES-GCM encryption, HMAC signing, and key generation, plus
|
|
122
|
+
polyfills for minimal environments. All functions use only the
|
|
123
|
+
standard Web Crypto API -- no Node.js built-ins are imported.
|
|
124
|
+
|
|
125
|
+
<p align="right"><a href="#contents">Back to Top</a></p>
|
|
126
|
+
|
|
127
|
+
## Supported Runtimes
|
|
128
|
+
|
|
129
|
+
| Runtime | Identifier | Web Crypto | Notes |
|
|
130
|
+
| :----------------- | :------------------- | :--------: | :------------------------------- |
|
|
131
|
+
| Cloudflare Workers | `cloudflare-workers` | Yes | Full support |
|
|
132
|
+
| Vercel Edge | `vercel-edge` | Yes | Full support |
|
|
133
|
+
| Deno | `deno` | Yes | Full support |
|
|
134
|
+
| Bun | `bun` | Yes | Full support |
|
|
135
|
+
| Browsers | `browser` | Yes | Modern browsers (Chrome 37+) |
|
|
136
|
+
| Node.js | `node` | Yes | Node >= 15 (globalThis.crypto) |
|
|
137
|
+
| Unknown | `unknown` | Varies | Use `getCapabilities()` to check |
|
|
138
|
+
|
|
139
|
+
<p align="right"><a href="#contents">Back to Top</a></p>
|
|
140
|
+
|
|
141
|
+
## Features
|
|
142
|
+
|
|
143
|
+
### Runtime Detection
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
import {
|
|
147
|
+
detectRuntime,
|
|
148
|
+
getCapabilities,
|
|
149
|
+
isEdgeCryptoAvailable,
|
|
150
|
+
} from "@sebastienrousseau/crypto-edge";
|
|
151
|
+
|
|
152
|
+
const runtime = detectRuntime();
|
|
153
|
+
const caps = getCapabilities();
|
|
154
|
+
if (isEdgeCryptoAvailable()) {
|
|
155
|
+
// Safe to call hash(), encrypt(), etc.
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Web Crypto API
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
import {
|
|
163
|
+
hash,
|
|
164
|
+
generateKey,
|
|
165
|
+
encrypt,
|
|
166
|
+
decrypt,
|
|
167
|
+
} from "@sebastienrousseau/crypto-edge";
|
|
168
|
+
|
|
169
|
+
// Hashing
|
|
170
|
+
const sha256 = await hash("SHA-256", "hello");
|
|
171
|
+
|
|
172
|
+
// Encryption / Decryption (AES-GCM)
|
|
173
|
+
const key = await generateKey({ algorithm: "AES-GCM", length: 256 });
|
|
174
|
+
const { ciphertext } = await encrypt({
|
|
175
|
+
key,
|
|
176
|
+
plaintext: new TextEncoder().encode("secret"),
|
|
177
|
+
});
|
|
178
|
+
const plaintext = await decrypt({ key, ciphertext });
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Polyfills
|
|
182
|
+
|
|
183
|
+
Call `installPolyfills()` once at startup for minimal runtimes
|
|
184
|
+
missing standard globals:
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
import { installPolyfills } from "@sebastienrousseau/crypto-edge";
|
|
188
|
+
|
|
189
|
+
const installed = installPolyfills();
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Polyfills provided: `TextEncoder`, `TextDecoder`, `btoa`, and `atob`.
|
|
193
|
+
`crypto.getRandomValues` is never polyfilled: without a native CSPRNG,
|
|
194
|
+
`randomBytes()` throws rather than fall back to predictable output.
|
|
195
|
+
|
|
196
|
+
<p align="right"><a href="#contents">Back to Top</a></p>
|
|
197
|
+
|
|
198
|
+
## Examples
|
|
199
|
+
|
|
200
|
+
All examples are self-contained TypeScript files in the `examples/`
|
|
201
|
+
directory. Run any example with:
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
npx ts-node examples/<name>.ts
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
| Category | Example | Purpose |
|
|
208
|
+
| :--------- | :---------------------------------- | :-------------------------- |
|
|
209
|
+
| Detection | [detect.ts](examples/detect.ts) | Runtime detection |
|
|
210
|
+
| Hashing | [hash.ts](examples/hash.ts) | Edge-compatible hashing |
|
|
211
|
+
| Encryption | [encrypt.ts](examples/encrypt.ts) | AES-GCM encrypt and decrypt |
|
|
212
|
+
| Workers | [workers.ts](examples/workers.ts) | Cloudflare Workers usage |
|
|
213
|
+
| Vercel | [vercel.ts](examples/vercel.ts) | Vercel Edge Function usage |
|
|
214
|
+
| Browser | [browser.ts](examples/browser.ts) | Browser usage |
|
|
215
|
+
| Polyfills | [polyfill.ts](examples/polyfill.ts) | Polyfill installation |
|
|
216
|
+
|
|
217
|
+
<p align="right"><a href="#contents">Back to Top</a></p>
|
|
218
|
+
|
|
219
|
+
<p align="right"><a href="#contents">Back to Top</a></p>
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## Development
|
|
224
|
+
|
|
225
|
+
```bash
|
|
226
|
+
pnpm --filter @sebastienrousseau/crypto-edge run build
|
|
227
|
+
pnpm --filter @sebastienrousseau/crypto-edge run test
|
|
228
|
+
pnpm --filter @sebastienrousseau/crypto-edge run lint
|
|
229
|
+
pnpm --filter @sebastienrousseau/crypto-edge run format
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
All 18 packages in the Crypto Service workspace maintain a **100% coverage floor** across statements, branches, functions, and lines.
|
|
233
|
+
|
|
234
|
+
<p align="right"><a href="#contents">Back to Top</a></p>
|
|
235
|
+
|
|
236
|
+
---
|
|
237
|
+
|
|
238
|
+
## Security
|
|
239
|
+
|
|
240
|
+
Report vulnerabilities privately via [GitHub Security Advisories](https://github.com/sebastienrousseau/crypto-service/security/advisories) or according to [`SECURITY.md`](../../SECURITY.md). Never report security issues publicly.
|
|
241
|
+
|
|
242
|
+
Cryptographic operations use the `@noble/*` libraries, Node.js `crypto` and OpenPGP.js. `@noble/post-quantum` has not been independently audited and does not guarantee constant-time execution, and no module in this suite is FIPS 140-3 validated. Key zeroization is limited: JavaScript strings and garbage-collected buffers cannot be reliably wiped. See [`SECURITY.md`](../../SECURITY.md).
|
|
243
|
+
|
|
244
|
+
<p align="right"><a href="#contents">Back to Top</a></p>
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
## Documentation
|
|
249
|
+
|
|
250
|
+
- [Full Suite Documentation](https://sebastienrousseau.github.io/crypto-service/)
|
|
251
|
+
- [API Reference (TypeDoc)](https://sebastienrousseau.github.io/crypto-service/)
|
|
252
|
+
- [Developer Guide](../../DEVELOPMENT.md)
|
|
253
|
+
- [Security Policy](../../SECURITY.md)
|
|
254
|
+
- [Architecture & Design](../../ARCHITECTURE.md)
|
|
255
|
+
|
|
256
|
+
<p align="right"><a href="#contents">Back to Top</a></p>
|
|
257
|
+
|
|
258
|
+
---
|
|
259
|
+
|
|
260
|
+
## Stability guarantees
|
|
261
|
+
|
|
262
|
+
Versions advance strictly one step at a time on the `0.0.x` line (`v0.0.1` → `v0.0.2` → `v0.0.3` ... → `v0.0.999` → `v0.1.0`). Work for every release iteration begins on a dedicated `feat/v<version>` branch.
|
|
263
|
+
|
|
264
|
+
All 18 packages in the workspace move in lockstep. Public API signatures, cipher output formats, and serialization schemas are strictly versioned. Breaking changes to serialized formats or algorithm defaults are considered major breaking changes. Minimum toolchain upgrades (e.g. Node.js LTS floor) are governed by [POLICIES.md](../../docs/POLICIES.md).
|
|
265
|
+
|
|
266
|
+
<p align="right"><a href="#contents">Back to Top</a></p>
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
## License
|
|
271
|
+
|
|
272
|
+
Dual-licensed under [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0) or [MIT](https://opensource.org/licenses/MIT), at your option.
|
|
273
|
+
|
|
274
|
+
Copyright (c) 2022-2026 Sebastien Rousseau and The Crypto Service Suite contributors.
|
|
275
|
+
|
|
276
|
+
<p align="right"><a href="#contents">Back to Top</a></p>
|
package/dist/detect.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { EdgeRuntime, RuntimeCapabilities } from "./types";
|
|
2
|
+
export declare function detectRuntime(): EdgeRuntime;
|
|
3
|
+
export declare function getCapabilities(): RuntimeCapabilities;
|
|
4
|
+
export declare function isEdgeCryptoAvailable(): boolean;
|
|
5
|
+
//# sourceMappingURL=detect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"detect.d.ts","sourceRoot":"","sources":["../src/detect.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AA8BhE,wBAAgB,aAAa,IAAI,WAAW,CA2C3C;AAmBD,wBAAgB,eAAe,IAAI,mBAAmB,CAqCrD;AAgBD,wBAAgB,qBAAqB,IAAI,OAAO,CAG/C"}
|
package/dist/detect.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.detectRuntime = detectRuntime;
|
|
4
|
+
exports.getCapabilities = getCapabilities;
|
|
5
|
+
exports.isEdgeCryptoAvailable = isEdgeCryptoAvailable;
|
|
6
|
+
const g = globalThis;
|
|
7
|
+
function detectRuntime() {
|
|
8
|
+
if (typeof g.navigator !== "undefined" &&
|
|
9
|
+
typeof g.navigator.userAgent === "string" &&
|
|
10
|
+
g.navigator.userAgent.includes("Cloudflare-Workers")) {
|
|
11
|
+
return "cloudflare-workers";
|
|
12
|
+
}
|
|
13
|
+
if (typeof g.EdgeRuntime === "string") {
|
|
14
|
+
return "vercel-edge";
|
|
15
|
+
}
|
|
16
|
+
if (typeof g.Deno !== "undefined" && typeof g.Deno.version === "object") {
|
|
17
|
+
return "deno";
|
|
18
|
+
}
|
|
19
|
+
if (typeof g.Bun !== "undefined") {
|
|
20
|
+
return "bun";
|
|
21
|
+
}
|
|
22
|
+
if (typeof g.window !== "undefined" && typeof g.document !== "undefined") {
|
|
23
|
+
return "browser";
|
|
24
|
+
}
|
|
25
|
+
if (typeof g.process !== "undefined" &&
|
|
26
|
+
typeof g.process.versions === "object" &&
|
|
27
|
+
typeof g.process.versions.node === "string") {
|
|
28
|
+
return "node";
|
|
29
|
+
}
|
|
30
|
+
return "unknown";
|
|
31
|
+
}
|
|
32
|
+
function getCapabilities() {
|
|
33
|
+
const runtime = detectRuntime();
|
|
34
|
+
const hasWebCrypto = typeof g.crypto !== "undefined" && g.crypto !== null;
|
|
35
|
+
const hasSubtle = hasWebCrypto &&
|
|
36
|
+
typeof g.crypto.subtle !== "undefined" &&
|
|
37
|
+
g.crypto.subtle !== null;
|
|
38
|
+
let hasNodeCrypto = false;
|
|
39
|
+
try {
|
|
40
|
+
if (typeof g.process !== "undefined" &&
|
|
41
|
+
typeof g.process.versions === "object" &&
|
|
42
|
+
typeof g.process.versions.node === "string") {
|
|
43
|
+
require("node:crypto");
|
|
44
|
+
hasNodeCrypto = true;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
hasNodeCrypto = false;
|
|
49
|
+
}
|
|
50
|
+
const hasTextEncoder = typeof g.TextEncoder === "function";
|
|
51
|
+
return {
|
|
52
|
+
runtime,
|
|
53
|
+
hasWebCrypto,
|
|
54
|
+
hasSubtle,
|
|
55
|
+
hasNodeCrypto,
|
|
56
|
+
hasTextEncoder,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function isEdgeCryptoAvailable() {
|
|
60
|
+
return getCapabilities().hasSubtle;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=detect.js.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type { EdgeRuntime, RuntimeCapabilities, EdgeHashAlgorithm, EdgeKeyAlgorithm, EdgeSignAlgorithm, EdgeEncryptOptions, EdgeEncryptResult, EdgeDecryptOptions, EdgeHmacSignOptions, EdgeHmacVerifyOptions, EdgeGenerateKeyOptions, } from "./types";
|
|
2
|
+
export { detectRuntime, getCapabilities, isEdgeCryptoAvailable, } from "./detect";
|
|
3
|
+
export { hash, encrypt, decrypt, sign, verify, generateKey, toHex, toBytes, randomBytes, concat, } from "./webcrypto";
|
|
4
|
+
export { installPolyfills, TextEncoderPolyfill, TextDecoderPolyfill, btoaPolyfill, atobPolyfill, _resetPolyfillState, } from "./polyfill";
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA6BA,YAAY,EACV,WAAW,EACX,mBAAmB,EACnB,iBAAiB,EACjB,gBAAgB,EAChB,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAClB,mBAAmB,EACnB,qBAAqB,EACrB,sBAAsB,GACvB,MAAM,SAAS,CAAC;AAGjB,OAAO,EACL,aAAa,EACb,eAAe,EACf,qBAAqB,GACtB,MAAM,UAAU,CAAC;AAGlB,OAAO,EACL,IAAI,EACJ,OAAO,EACP,OAAO,EACP,IAAI,EACJ,MAAM,EACN,WAAW,EACX,KAAK,EACL,OAAO,EACP,WAAW,EACX,MAAM,GACP,MAAM,aAAa,CAAC;AAGrB,OAAO,EACL,gBAAgB,EAChB,mBAAmB,EACnB,mBAAmB,EACnB,YAAY,EACZ,YAAY,EACZ,mBAAmB,GACpB,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports._resetPolyfillState = exports.atobPolyfill = exports.btoaPolyfill = exports.TextDecoderPolyfill = exports.TextEncoderPolyfill = exports.installPolyfills = exports.concat = exports.randomBytes = exports.toBytes = exports.toHex = exports.generateKey = exports.verify = exports.sign = exports.decrypt = exports.encrypt = exports.hash = exports.isEdgeCryptoAvailable = exports.getCapabilities = exports.detectRuntime = void 0;
|
|
4
|
+
var detect_1 = require("./detect");
|
|
5
|
+
Object.defineProperty(exports, "detectRuntime", { enumerable: true, get: function () { return detect_1.detectRuntime; } });
|
|
6
|
+
Object.defineProperty(exports, "getCapabilities", { enumerable: true, get: function () { return detect_1.getCapabilities; } });
|
|
7
|
+
Object.defineProperty(exports, "isEdgeCryptoAvailable", { enumerable: true, get: function () { return detect_1.isEdgeCryptoAvailable; } });
|
|
8
|
+
var webcrypto_1 = require("./webcrypto");
|
|
9
|
+
Object.defineProperty(exports, "hash", { enumerable: true, get: function () { return webcrypto_1.hash; } });
|
|
10
|
+
Object.defineProperty(exports, "encrypt", { enumerable: true, get: function () { return webcrypto_1.encrypt; } });
|
|
11
|
+
Object.defineProperty(exports, "decrypt", { enumerable: true, get: function () { return webcrypto_1.decrypt; } });
|
|
12
|
+
Object.defineProperty(exports, "sign", { enumerable: true, get: function () { return webcrypto_1.sign; } });
|
|
13
|
+
Object.defineProperty(exports, "verify", { enumerable: true, get: function () { return webcrypto_1.verify; } });
|
|
14
|
+
Object.defineProperty(exports, "generateKey", { enumerable: true, get: function () { return webcrypto_1.generateKey; } });
|
|
15
|
+
Object.defineProperty(exports, "toHex", { enumerable: true, get: function () { return webcrypto_1.toHex; } });
|
|
16
|
+
Object.defineProperty(exports, "toBytes", { enumerable: true, get: function () { return webcrypto_1.toBytes; } });
|
|
17
|
+
Object.defineProperty(exports, "randomBytes", { enumerable: true, get: function () { return webcrypto_1.randomBytes; } });
|
|
18
|
+
Object.defineProperty(exports, "concat", { enumerable: true, get: function () { return webcrypto_1.concat; } });
|
|
19
|
+
var polyfill_1 = require("./polyfill");
|
|
20
|
+
Object.defineProperty(exports, "installPolyfills", { enumerable: true, get: function () { return polyfill_1.installPolyfills; } });
|
|
21
|
+
Object.defineProperty(exports, "TextEncoderPolyfill", { enumerable: true, get: function () { return polyfill_1.TextEncoderPolyfill; } });
|
|
22
|
+
Object.defineProperty(exports, "TextDecoderPolyfill", { enumerable: true, get: function () { return polyfill_1.TextDecoderPolyfill; } });
|
|
23
|
+
Object.defineProperty(exports, "btoaPolyfill", { enumerable: true, get: function () { return polyfill_1.btoaPolyfill; } });
|
|
24
|
+
Object.defineProperty(exports, "atobPolyfill", { enumerable: true, get: function () { return polyfill_1.atobPolyfill; } });
|
|
25
|
+
Object.defineProperty(exports, "_resetPolyfillState", { enumerable: true, get: function () { return polyfill_1._resetPolyfillState; } });
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
declare class TextEncoderPolyfill {
|
|
2
|
+
readonly encoding = "utf-8";
|
|
3
|
+
encode(input: string): Uint8Array;
|
|
4
|
+
}
|
|
5
|
+
declare class TextDecoderPolyfill {
|
|
6
|
+
readonly encoding = "utf-8";
|
|
7
|
+
decode(input?: ArrayBufferView | ArrayBuffer): string;
|
|
8
|
+
}
|
|
9
|
+
declare function btoaPolyfill(input: string): string;
|
|
10
|
+
declare function atobPolyfill(input: string): string;
|
|
11
|
+
export declare function installPolyfills(): {
|
|
12
|
+
textEncoder: boolean;
|
|
13
|
+
textDecoder: boolean;
|
|
14
|
+
btoa: boolean;
|
|
15
|
+
atob: boolean;
|
|
16
|
+
};
|
|
17
|
+
export declare function _resetPolyfillState(): void;
|
|
18
|
+
export { TextEncoderPolyfill, TextDecoderPolyfill, btoaPolyfill, atobPolyfill };
|
|
19
|
+
//# sourceMappingURL=polyfill.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"polyfill.d.ts","sourceRoot":"","sources":["../src/polyfill.ts"],"names":[],"mappings":"AAmGA,cAAM,mBAAmB;IAEvB,QAAQ,CAAC,QAAQ,WAAW;IAK5B,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU;CAkBlC;AAOD,cAAM,mBAAmB;IAEvB,QAAQ,CAAC,QAAQ,WAAW;IAK5B,MAAM,CAAC,KAAK,CAAC,EAAE,eAAe,GAAG,WAAW,GAAG,MAAM;CAmBtD;AA2BD,iBAAS,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAY3C;AAMD,iBAAS,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAqB3C;AA4BD,wBAAgB,gBAAgB,IAAI;IAElC,WAAW,EAAE,OAAO,CAAC;IAErB,WAAW,EAAE,OAAO,CAAC;IAErB,IAAI,EAAE,OAAO,CAAC;IAEd,IAAI,EAAE,OAAO,CAAC;CACf,CAwCA;AAcD,wBAAgB,mBAAmB,IAAI,IAAI,CAE1C;AAGD,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,YAAY,EAAE,YAAY,EAAE,CAAC"}
|
package/dist/polyfill.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TextDecoderPolyfill = exports.TextEncoderPolyfill = void 0;
|
|
4
|
+
exports.installPolyfills = installPolyfills;
|
|
5
|
+
exports._resetPolyfillState = _resetPolyfillState;
|
|
6
|
+
exports.btoaPolyfill = btoaPolyfill;
|
|
7
|
+
exports.atobPolyfill = atobPolyfill;
|
|
8
|
+
function pushUtf8Bytes(codePoint, bytes) {
|
|
9
|
+
if (codePoint < 0x80) {
|
|
10
|
+
bytes.push(codePoint);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (codePoint < 0x800) {
|
|
14
|
+
bytes.push(0xc0 | (codePoint >> 6), 0x80 | (codePoint & 0x3f));
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (codePoint < 0x10000) {
|
|
18
|
+
bytes.push(0xe0 | (codePoint >> 12), 0x80 | ((codePoint >> 6) & 0x3f), 0x80 | (codePoint & 0x3f));
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
bytes.push(0xf0 | (codePoint >> 18), 0x80 | ((codePoint >> 12) & 0x3f), 0x80 | ((codePoint >> 6) & 0x3f), 0x80 | (codePoint & 0x3f));
|
|
22
|
+
}
|
|
23
|
+
function readUtf8CodePoint(bytes, i) {
|
|
24
|
+
const b0 = bytes[i];
|
|
25
|
+
if (b0 < 0x80) {
|
|
26
|
+
return [b0, i + 1];
|
|
27
|
+
}
|
|
28
|
+
if ((b0 & 0xe0) === 0xc0) {
|
|
29
|
+
return [((b0 & 0x1f) << 6) | (bytes[i + 1] & 0x3f), i + 2];
|
|
30
|
+
}
|
|
31
|
+
if ((b0 & 0xf0) === 0xe0) {
|
|
32
|
+
return [
|
|
33
|
+
((b0 & 0x0f) << 12) |
|
|
34
|
+
((bytes[i + 1] & 0x3f) << 6) |
|
|
35
|
+
(bytes[i + 2] & 0x3f),
|
|
36
|
+
i + 3,
|
|
37
|
+
];
|
|
38
|
+
}
|
|
39
|
+
return [
|
|
40
|
+
((b0 & 0x07) << 18) |
|
|
41
|
+
((bytes[i + 1] & 0x3f) << 12) |
|
|
42
|
+
((bytes[i + 2] & 0x3f) << 6) |
|
|
43
|
+
(bytes[i + 3] & 0x3f),
|
|
44
|
+
i + 4,
|
|
45
|
+
];
|
|
46
|
+
}
|
|
47
|
+
function codePointToString(codePoint) {
|
|
48
|
+
if (codePoint <= 0xffff) {
|
|
49
|
+
return String.fromCharCode(codePoint);
|
|
50
|
+
}
|
|
51
|
+
const cp = codePoint - 0x10000;
|
|
52
|
+
return String.fromCharCode(0xd800 + (cp >> 10), 0xdc00 + (cp & 0x3ff));
|
|
53
|
+
}
|
|
54
|
+
class TextEncoderPolyfill {
|
|
55
|
+
encoding = "utf-8";
|
|
56
|
+
encode(input) {
|
|
57
|
+
const bytes = [];
|
|
58
|
+
for (let i = 0; i < input.length; i++) {
|
|
59
|
+
let codePoint = input.charCodeAt(i);
|
|
60
|
+
if (codePoint >= 0xd800 && codePoint <= 0xdbff && i + 1 < input.length) {
|
|
61
|
+
const low = input.charCodeAt(i + 1);
|
|
62
|
+
if (low >= 0xdc00 && low <= 0xdfff) {
|
|
63
|
+
codePoint = ((codePoint - 0xd800) << 10) + (low - 0xdc00) + 0x10000;
|
|
64
|
+
i++;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
pushUtf8Bytes(codePoint, bytes);
|
|
68
|
+
}
|
|
69
|
+
return new Uint8Array(bytes);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.TextEncoderPolyfill = TextEncoderPolyfill;
|
|
73
|
+
class TextDecoderPolyfill {
|
|
74
|
+
encoding = "utf-8";
|
|
75
|
+
decode(input) {
|
|
76
|
+
if (!input)
|
|
77
|
+
return "";
|
|
78
|
+
const bytes = input instanceof ArrayBuffer
|
|
79
|
+
? new Uint8Array(input)
|
|
80
|
+
: new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
|
81
|
+
let result = "";
|
|
82
|
+
let i = 0;
|
|
83
|
+
while (i < bytes.length) {
|
|
84
|
+
const [codePoint, nextI] = readUtf8CodePoint(bytes, i);
|
|
85
|
+
result += codePointToString(codePoint);
|
|
86
|
+
i = nextI;
|
|
87
|
+
}
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.TextDecoderPolyfill = TextDecoderPolyfill;
|
|
92
|
+
const BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
93
|
+
function encodeBase64Triplet(a, b, c) {
|
|
94
|
+
const idx1 = a >> 2;
|
|
95
|
+
const idx2 = ((a & 3) << 4) | (isNaN(b) ? 0 : b >> 4);
|
|
96
|
+
const char3 = isNaN(b)
|
|
97
|
+
? "="
|
|
98
|
+
: BASE64_CHARS[((b & 15) << 2) | (isNaN(c) ? 0 : c >> 6)];
|
|
99
|
+
const char4 = isNaN(c) ? "=" : BASE64_CHARS[c & 63];
|
|
100
|
+
return BASE64_CHARS[idx1] + BASE64_CHARS[idx2] + char3 + char4;
|
|
101
|
+
}
|
|
102
|
+
function btoaPolyfill(input) {
|
|
103
|
+
let result = "";
|
|
104
|
+
let i = 0;
|
|
105
|
+
while (i < input.length) {
|
|
106
|
+
const a = input.charCodeAt(i++);
|
|
107
|
+
const b = i < input.length ? input.charCodeAt(i++) : NaN;
|
|
108
|
+
const c = i < input.length ? input.charCodeAt(i++) : NaN;
|
|
109
|
+
result += encodeBase64Triplet(a, b, c);
|
|
110
|
+
}
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
function atobPolyfill(input) {
|
|
114
|
+
let str = input;
|
|
115
|
+
while (str.endsWith("=")) {
|
|
116
|
+
str = str.slice(0, -1);
|
|
117
|
+
}
|
|
118
|
+
let result = "";
|
|
119
|
+
for (let i = 0; i < str.length; i += 4) {
|
|
120
|
+
const a = BASE64_CHARS.indexOf(str[i]);
|
|
121
|
+
const b = BASE64_CHARS.indexOf(str[i + 1]);
|
|
122
|
+
const c = BASE64_CHARS.indexOf(str[i + 2]);
|
|
123
|
+
const d = BASE64_CHARS.indexOf(str[i + 3]);
|
|
124
|
+
const bits = (a << 18) | (b << 12) | (c << 6) | d;
|
|
125
|
+
result += String.fromCharCode((bits >> 16) & 0xff);
|
|
126
|
+
if (c !== -1)
|
|
127
|
+
result += String.fromCharCode((bits >> 8) & 0xff);
|
|
128
|
+
if (d !== -1)
|
|
129
|
+
result += String.fromCharCode(bits & 0xff);
|
|
130
|
+
}
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
let _installed = false;
|
|
134
|
+
function installPolyfills() {
|
|
135
|
+
if (_installed) {
|
|
136
|
+
return {
|
|
137
|
+
textEncoder: false,
|
|
138
|
+
textDecoder: false,
|
|
139
|
+
btoa: false,
|
|
140
|
+
atob: false,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
const g = globalThis;
|
|
144
|
+
const result = {
|
|
145
|
+
textEncoder: false,
|
|
146
|
+
textDecoder: false,
|
|
147
|
+
btoa: false,
|
|
148
|
+
atob: false,
|
|
149
|
+
};
|
|
150
|
+
if (typeof g.TextEncoder !== "function") {
|
|
151
|
+
g.TextEncoder = TextEncoderPolyfill;
|
|
152
|
+
result.textEncoder = true;
|
|
153
|
+
}
|
|
154
|
+
if (typeof g.TextDecoder !== "function") {
|
|
155
|
+
g.TextDecoder = TextDecoderPolyfill;
|
|
156
|
+
result.textDecoder = true;
|
|
157
|
+
}
|
|
158
|
+
if (typeof g.btoa !== "function") {
|
|
159
|
+
g.btoa = btoaPolyfill;
|
|
160
|
+
result.btoa = true;
|
|
161
|
+
}
|
|
162
|
+
if (typeof g.atob !== "function") {
|
|
163
|
+
g.atob = atobPolyfill;
|
|
164
|
+
result.atob = true;
|
|
165
|
+
}
|
|
166
|
+
_installed = true;
|
|
167
|
+
return result;
|
|
168
|
+
}
|
|
169
|
+
function _resetPolyfillState() {
|
|
170
|
+
_installed = false;
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=polyfill.js.map
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export type EdgeRuntime = "cloudflare-workers" | "vercel-edge" | "deno" | "bun" | "browser" | "node" | "unknown";
|
|
2
|
+
export interface RuntimeCapabilities {
|
|
3
|
+
runtime: EdgeRuntime;
|
|
4
|
+
hasWebCrypto: boolean;
|
|
5
|
+
hasSubtle: boolean;
|
|
6
|
+
hasNodeCrypto: boolean;
|
|
7
|
+
hasTextEncoder: boolean;
|
|
8
|
+
}
|
|
9
|
+
export type EdgeHashAlgorithm = "SHA-1" | "SHA-256" | "SHA-384" | "SHA-512";
|
|
10
|
+
export type EdgeKeyAlgorithm = "AES-GCM" | "AES-CBC" | "AES-CTR";
|
|
11
|
+
export type EdgeSignAlgorithm = "HMAC" | "ECDSA" | "RSASSA-PKCS1-v1_5";
|
|
12
|
+
export interface EdgeEncryptOptions {
|
|
13
|
+
key: Uint8Array;
|
|
14
|
+
plaintext: Uint8Array;
|
|
15
|
+
aad?: Uint8Array;
|
|
16
|
+
}
|
|
17
|
+
export interface EdgeEncryptResult {
|
|
18
|
+
ciphertext: Uint8Array;
|
|
19
|
+
ivLength: number;
|
|
20
|
+
}
|
|
21
|
+
export interface EdgeDecryptOptions {
|
|
22
|
+
key: Uint8Array;
|
|
23
|
+
ciphertext: Uint8Array;
|
|
24
|
+
ivLength?: number;
|
|
25
|
+
aad?: Uint8Array;
|
|
26
|
+
}
|
|
27
|
+
export interface EdgeHmacSignOptions {
|
|
28
|
+
key: Uint8Array;
|
|
29
|
+
data: Uint8Array;
|
|
30
|
+
hash?: EdgeHashAlgorithm;
|
|
31
|
+
}
|
|
32
|
+
export interface EdgeHmacVerifyOptions extends EdgeHmacSignOptions {
|
|
33
|
+
signature: Uint8Array;
|
|
34
|
+
}
|
|
35
|
+
export interface EdgeGenerateKeyOptions {
|
|
36
|
+
algorithm: EdgeKeyAlgorithm;
|
|
37
|
+
length?: 128 | 192 | 256;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AA8BA,MAAM,MAAM,WAAW,GACnB,oBAAoB,GACpB,aAAa,GACb,MAAM,GACN,KAAK,GACL,SAAS,GACT,MAAM,GACN,SAAS,CAAC;AAmBd,MAAM,WAAW,mBAAmB;IAElC,OAAO,EAAE,WAAW,CAAC;IAErB,YAAY,EAAE,OAAO,CAAC;IAEtB,SAAS,EAAE,OAAO,CAAC;IAEnB,aAAa,EAAE,OAAO,CAAC;IAEvB,cAAc,EAAE,OAAO,CAAC;CACzB;AAYD,MAAM,MAAM,iBAAiB,GAAG,OAAO,GAAG,SAAS,GAAG,SAAS,GAAG,SAAS,CAAC;AAY5E,MAAM,MAAM,gBAAgB,GAAG,SAAS,GAAG,SAAS,GAAG,SAAS,CAAC;AAYjE,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,OAAO,GAAG,mBAAmB,CAAC;AAevE,MAAM,WAAW,kBAAkB;IAEjC,GAAG,EAAE,UAAU,CAAC;IAEhB,SAAS,EAAE,UAAU,CAAC;IAEtB,GAAG,CAAC,EAAE,UAAU,CAAC;CAClB;AAeD,MAAM,WAAW,iBAAiB;IAEhC,UAAU,EAAE,UAAU,CAAC;IAEvB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAgBD,MAAM,WAAW,kBAAkB;IAEjC,GAAG,EAAE,UAAU,CAAC;IAEhB,UAAU,EAAE,UAAU,CAAC;IAEvB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB,GAAG,CAAC,EAAE,UAAU,CAAC;CAClB;AAgBD,MAAM,WAAW,mBAAmB;IAElC,GAAG,EAAE,UAAU,CAAC;IAEhB,IAAI,EAAE,UAAU,CAAC;IAEjB,IAAI,CAAC,EAAE,iBAAiB,CAAC;CAC1B;AAgBD,MAAM,WAAW,qBAAsB,SAAQ,mBAAmB;IAEhE,SAAS,EAAE,UAAU,CAAC;CACvB;AAeD,MAAM,WAAW,sBAAsB;IAErC,SAAS,EAAE,gBAAgB,CAAC;IAE5B,MAAM,CAAC,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;CAC1B"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { EdgeHashAlgorithm, EdgeEncryptOptions, EdgeEncryptResult, EdgeDecryptOptions, EdgeHmacSignOptions, EdgeHmacVerifyOptions, EdgeGenerateKeyOptions } from "./types";
|
|
2
|
+
declare function randomBytes(length: number): Uint8Array;
|
|
3
|
+
declare function toBytes(input: string | Uint8Array): Uint8Array;
|
|
4
|
+
declare function toHex(bytes: Uint8Array): string;
|
|
5
|
+
declare function concat(a: Uint8Array, b: Uint8Array): Uint8Array;
|
|
6
|
+
export declare function hash(algorithm: EdgeHashAlgorithm, data: string | Uint8Array): Promise<string>;
|
|
7
|
+
export declare function encrypt(options: EdgeEncryptOptions): Promise<EdgeEncryptResult>;
|
|
8
|
+
export declare function decrypt(options: EdgeDecryptOptions): Promise<Uint8Array>;
|
|
9
|
+
export declare function sign(options: EdgeHmacSignOptions): Promise<Uint8Array>;
|
|
10
|
+
export declare function verify(options: EdgeHmacVerifyOptions): Promise<boolean>;
|
|
11
|
+
export declare function generateKey(options: EdgeGenerateKeyOptions): Promise<Uint8Array>;
|
|
12
|
+
export { toHex, toBytes, randomBytes, concat };
|
|
13
|
+
//# sourceMappingURL=webcrypto.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webcrypto.d.ts","sourceRoot":"","sources":["../src/webcrypto.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EACV,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAClB,mBAAmB,EACnB,qBAAqB,EACrB,sBAAsB,EAEvB,MAAM,SAAS,CAAC;AAuCjB,iBAAS,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAc/C;AAaD,iBAAS,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,GAAG,UAAU,CAGvD;AAyBD,iBAAS,KAAK,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAMxC;AAaD,iBAAS,MAAM,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,UAAU,CAKxD;AAkBD,wBAAsB,IAAI,CACxB,SAAS,EAAE,iBAAiB,EAC5B,IAAI,EAAE,MAAM,GAAG,UAAU,GACxB,OAAO,CAAC,MAAM,CAAC,CAKjB;AAuBD,wBAAsB,OAAO,CAC3B,OAAO,EAAE,kBAAkB,GAC1B,OAAO,CAAC,iBAAiB,CAAC,CAoC5B;AAmBD,wBAAsB,OAAO,CAC3B,OAAO,EAAE,kBAAkB,GAC1B,OAAO,CAAC,UAAU,CAAC,CAqCrB;AAoBD,wBAAsB,IAAI,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,UAAU,CAAC,CAe5E;AAqBD,wBAAsB,MAAM,CAAC,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,OAAO,CAAC,CAc7E;AAkBD,wBAAsB,WAAW,CAC/B,OAAO,EAAE,sBAAsB,GAC9B,OAAO,CAAC,UAAU,CAAC,CAcrB;AAuBD,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,CAAC"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hash = hash;
|
|
4
|
+
exports.encrypt = encrypt;
|
|
5
|
+
exports.decrypt = decrypt;
|
|
6
|
+
exports.sign = sign;
|
|
7
|
+
exports.verify = verify;
|
|
8
|
+
exports.generateKey = generateKey;
|
|
9
|
+
exports.toHex = toHex;
|
|
10
|
+
exports.toBytes = toBytes;
|
|
11
|
+
exports.randomBytes = randomBytes;
|
|
12
|
+
exports.concat = concat;
|
|
13
|
+
const IV_LENGTH = 12;
|
|
14
|
+
function getSubtle() {
|
|
15
|
+
if (typeof globalThis.crypto !== "undefined" &&
|
|
16
|
+
typeof globalThis.crypto.subtle !== "undefined") {
|
|
17
|
+
return globalThis.crypto.subtle;
|
|
18
|
+
}
|
|
19
|
+
throw new Error("Web Crypto API (crypto.subtle) is not available in this runtime.");
|
|
20
|
+
}
|
|
21
|
+
function randomBytes(length) {
|
|
22
|
+
const source = globalThis.crypto;
|
|
23
|
+
if (typeof source === "undefined" ||
|
|
24
|
+
typeof source.getRandomValues !== "function") {
|
|
25
|
+
throw new Error("No cryptographically secure random source available: " +
|
|
26
|
+
"crypto.getRandomValues is missing in this runtime.");
|
|
27
|
+
}
|
|
28
|
+
const buf = new Uint8Array(length);
|
|
29
|
+
source.getRandomValues(buf);
|
|
30
|
+
return buf;
|
|
31
|
+
}
|
|
32
|
+
function toBytes(input) {
|
|
33
|
+
if (input instanceof Uint8Array)
|
|
34
|
+
return input;
|
|
35
|
+
return new TextEncoder().encode(input);
|
|
36
|
+
}
|
|
37
|
+
function buf(data) {
|
|
38
|
+
return data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
|
|
39
|
+
}
|
|
40
|
+
function toHex(bytes) {
|
|
41
|
+
let hex = "";
|
|
42
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
43
|
+
hex += bytes[i].toString(16).padStart(2, "0");
|
|
44
|
+
}
|
|
45
|
+
return hex;
|
|
46
|
+
}
|
|
47
|
+
function concat(a, b) {
|
|
48
|
+
const result = new Uint8Array(a.length + b.length);
|
|
49
|
+
result.set(a, 0);
|
|
50
|
+
result.set(b, a.length);
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
async function hash(algorithm, data) {
|
|
54
|
+
const subtle = getSubtle();
|
|
55
|
+
const input = toBytes(data);
|
|
56
|
+
const digest = await subtle.digest(algorithm, buf(input));
|
|
57
|
+
return toHex(new Uint8Array(digest));
|
|
58
|
+
}
|
|
59
|
+
async function encrypt(options) {
|
|
60
|
+
const subtle = getSubtle();
|
|
61
|
+
const { key, plaintext, aad } = options;
|
|
62
|
+
if (key.length !== 16 && key.length !== 32) {
|
|
63
|
+
throw new Error(`Key must be 16 bytes (128-bit) or 32 bytes (256-bit), got ${key.length}`);
|
|
64
|
+
}
|
|
65
|
+
const iv = randomBytes(IV_LENGTH);
|
|
66
|
+
const cryptoKey = await subtle.importKey("raw", buf(key), { name: "AES-GCM" }, false, ["encrypt"]);
|
|
67
|
+
const params = {
|
|
68
|
+
name: "AES-GCM",
|
|
69
|
+
iv: buf(iv),
|
|
70
|
+
tagLength: 128,
|
|
71
|
+
};
|
|
72
|
+
if (aad) {
|
|
73
|
+
params.additionalData = buf(aad);
|
|
74
|
+
}
|
|
75
|
+
const encrypted = await subtle.encrypt(params, cryptoKey, buf(plaintext));
|
|
76
|
+
const sealed = new Uint8Array(encrypted);
|
|
77
|
+
return {
|
|
78
|
+
ciphertext: concat(iv, sealed),
|
|
79
|
+
ivLength: IV_LENGTH,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
async function decrypt(options) {
|
|
83
|
+
const subtle = getSubtle();
|
|
84
|
+
const { key, ciphertext, aad } = options;
|
|
85
|
+
const ivLen = options.ivLength ?? IV_LENGTH;
|
|
86
|
+
if (key.length !== 16 && key.length !== 32) {
|
|
87
|
+
throw new Error(`Key must be 16 bytes (128-bit) or 32 bytes (256-bit), got ${key.length}`);
|
|
88
|
+
}
|
|
89
|
+
if (ciphertext.length < ivLen + 16) {
|
|
90
|
+
throw new Error("Ciphertext too short -- missing IV or auth tag");
|
|
91
|
+
}
|
|
92
|
+
const iv = ciphertext.slice(0, ivLen);
|
|
93
|
+
const sealed = ciphertext.slice(ivLen);
|
|
94
|
+
const cryptoKey = await subtle.importKey("raw", buf(key), { name: "AES-GCM" }, false, ["decrypt"]);
|
|
95
|
+
const params = {
|
|
96
|
+
name: "AES-GCM",
|
|
97
|
+
iv: buf(iv),
|
|
98
|
+
tagLength: 128,
|
|
99
|
+
};
|
|
100
|
+
if (aad) {
|
|
101
|
+
params.additionalData = buf(aad);
|
|
102
|
+
}
|
|
103
|
+
const decrypted = await subtle.decrypt(params, cryptoKey, buf(sealed));
|
|
104
|
+
return new Uint8Array(decrypted);
|
|
105
|
+
}
|
|
106
|
+
async function sign(options) {
|
|
107
|
+
const subtle = getSubtle();
|
|
108
|
+
const { key, data } = options;
|
|
109
|
+
const hashAlg = options.hash ?? "SHA-256";
|
|
110
|
+
const cryptoKey = await subtle.importKey("raw", buf(key), { name: "HMAC", hash: { name: hashAlg } }, false, ["sign"]);
|
|
111
|
+
const signature = await subtle.sign("HMAC", cryptoKey, buf(data));
|
|
112
|
+
return new Uint8Array(signature);
|
|
113
|
+
}
|
|
114
|
+
async function verify(options) {
|
|
115
|
+
const subtle = getSubtle();
|
|
116
|
+
const { key, data, signature } = options;
|
|
117
|
+
const hashAlg = options.hash ?? "SHA-256";
|
|
118
|
+
const cryptoKey = await subtle.importKey("raw", buf(key), { name: "HMAC", hash: { name: hashAlg } }, false, ["verify"]);
|
|
119
|
+
return subtle.verify("HMAC", cryptoKey, buf(signature), buf(data));
|
|
120
|
+
}
|
|
121
|
+
async function generateKey(options) {
|
|
122
|
+
const subtle = getSubtle();
|
|
123
|
+
const { algorithm } = options;
|
|
124
|
+
const length = options.length ?? 256;
|
|
125
|
+
const algParam = buildKeyGenParams(algorithm, length);
|
|
126
|
+
const cryptoKey = await subtle.generateKey(algParam, true, [
|
|
127
|
+
"encrypt",
|
|
128
|
+
"decrypt",
|
|
129
|
+
]);
|
|
130
|
+
const raw = await subtle.exportKey("raw", cryptoKey);
|
|
131
|
+
return new Uint8Array(raw);
|
|
132
|
+
}
|
|
133
|
+
function buildKeyGenParams(algorithm, length) {
|
|
134
|
+
switch (algorithm) {
|
|
135
|
+
case "AES-GCM":
|
|
136
|
+
case "AES-CBC":
|
|
137
|
+
case "AES-CTR":
|
|
138
|
+
return { name: algorithm, length };
|
|
139
|
+
default:
|
|
140
|
+
throw new Error(`Unsupported key generation algorithm: ${algorithm}`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=webcrypto.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sebastienrousseau/crypto-edge",
|
|
3
|
+
"version": "0.0.8",
|
|
4
|
+
"description": "Edge and serverless runtime adapter for crypto-lib -- runs on Cloudflare Workers, Vercel Edge, Deno, Bun, and browsers.",
|
|
5
|
+
"author": "Sebastien Rousseau <sebastienrousseau@users.noreply.github.com>",
|
|
6
|
+
"license": "MIT OR Apache-2.0",
|
|
7
|
+
"homepage": "https://crypto-service.co",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/sebastienrousseau/crypto-service.git",
|
|
11
|
+
"directory": "packages/crypto-edge"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/sebastienrousseau/crypto-service/issues"
|
|
15
|
+
},
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"module": "./dist/index.js",
|
|
18
|
+
"browser": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"require": "./dist/index.js",
|
|
25
|
+
"import": "./dist/index.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist/**/*.js",
|
|
30
|
+
"dist/**/*.d.ts",
|
|
31
|
+
"dist/**/*.d.ts.map"
|
|
32
|
+
],
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@sebastienrousseau/crypto-lib": "0.0.8"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@sebastienrousseau/mocha-config": "^0.0.5",
|
|
38
|
+
"@types/chai": "^4.3.7",
|
|
39
|
+
"@types/chai-as-promised": "^8.0.2",
|
|
40
|
+
"@types/mocha": "^10.0.2",
|
|
41
|
+
"@types/node": "^26.6.3",
|
|
42
|
+
"c8": "^12.0.0",
|
|
43
|
+
"chai": "^4.5.0",
|
|
44
|
+
"chai-as-promised": "^7.1.2",
|
|
45
|
+
"mocha": "^10.8.2",
|
|
46
|
+
"rimraf": "^6.1.3",
|
|
47
|
+
"ts-node": "^10.9.2",
|
|
48
|
+
"typedoc": "^0.28.20",
|
|
49
|
+
"typescript": "~5.9.3"
|
|
50
|
+
},
|
|
51
|
+
"keywords": [
|
|
52
|
+
"crypto",
|
|
53
|
+
"edge",
|
|
54
|
+
"cloudflare-workers",
|
|
55
|
+
"vercel-edge",
|
|
56
|
+
"deno",
|
|
57
|
+
"bun",
|
|
58
|
+
"webcrypto",
|
|
59
|
+
"serverless",
|
|
60
|
+
"runtime-detection"
|
|
61
|
+
],
|
|
62
|
+
"publishConfig": {
|
|
63
|
+
"access": "public"
|
|
64
|
+
},
|
|
65
|
+
"private": false,
|
|
66
|
+
"scripts": {
|
|
67
|
+
"build": "tsc --build tsconfig.json",
|
|
68
|
+
"clean": "rimraf ./coverage ./dist ./docs",
|
|
69
|
+
"test": "c8 --check-coverage --lines 100 --branches 100 --functions 100 --reporter=lcov --reporter=text mocha --config=.mocharc.cjs",
|
|
70
|
+
"docs": "typedoc",
|
|
71
|
+
"lint": "eslint --ext .ts src __tests__",
|
|
72
|
+
"lint:fix": "eslint --cache --fix --ext .ts src __tests__",
|
|
73
|
+
"format": "prettier --write src/**/*.ts",
|
|
74
|
+
"format:check": "prettier --check \"src/**/*.ts\""
|
|
75
|
+
}
|
|
76
|
+
}
|