@ubay182/sveltekit-hpke-wrapper 1.0.1 → 1.0.2
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/README.md +71 -61
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# @
|
|
1
|
+
# @ubay182/sveltekit-hpke-wrapper
|
|
2
2
|
|
|
3
3
|
HPKE (Hybrid Public Key Encryption) wrapper for SvelteKit applications with end-to-end encryption support.
|
|
4
4
|
|
|
@@ -14,11 +14,11 @@ HPKE (Hybrid Public Key Encryption) wrapper for SvelteKit applications with end-
|
|
|
14
14
|
## 📦 Installation
|
|
15
15
|
|
|
16
16
|
```bash
|
|
17
|
-
npm install @
|
|
17
|
+
npm install @ubay182/sveltekit-hpke-wrapper
|
|
18
18
|
# or
|
|
19
|
-
pnpm add @
|
|
19
|
+
pnpm add @ubay182/sveltekit-hpke-wrapper
|
|
20
20
|
# or
|
|
21
|
-
yarn add @
|
|
21
|
+
yarn add @ubay182/sveltekit-hpke-wrapper
|
|
22
22
|
```
|
|
23
23
|
|
|
24
24
|
## 🎯 Quick Start
|
|
@@ -26,13 +26,13 @@ yarn add @hpke/sveltekit-wrapper
|
|
|
26
26
|
### 1. Basic Usage (Client-Side)
|
|
27
27
|
|
|
28
28
|
```typescript
|
|
29
|
-
import {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
} from '@
|
|
29
|
+
import {
|
|
30
|
+
generateKeyPair,
|
|
31
|
+
hpkeEncrypt,
|
|
32
|
+
hpkeDecrypt,
|
|
33
|
+
exportKeyToBase64,
|
|
34
|
+
importKeyFromBase64
|
|
35
|
+
} from '@ubay182/sveltekit-hpke-wrapper';
|
|
36
36
|
|
|
37
37
|
// Generate key pair
|
|
38
38
|
const { publicKey, privateKey, publicKeyRaw } = await generateKeyPair();
|
|
@@ -54,19 +54,19 @@ const decrypted = await hpkeDecrypt(ciphertext, enc, privateKey);
|
|
|
54
54
|
|
|
55
55
|
```typescript
|
|
56
56
|
// src/routes/api/hpke/+server.ts
|
|
57
|
-
import { createHpkeEndpoint } from '@
|
|
57
|
+
import { createHpkeEndpoint } from '@ubay182/sveltekit-hpke-wrapper';
|
|
58
58
|
|
|
59
59
|
const { GET, POST } = createHpkeEndpoint({
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
60
|
+
onRequest: async (decryptedData, request) => {
|
|
61
|
+
// Process the decrypted request
|
|
62
|
+
const response = await fetch('https://api.example.com/data', {
|
|
63
|
+
method: 'POST',
|
|
64
|
+
headers: { 'Content-Type': 'application/json' },
|
|
65
|
+
body: JSON.stringify(decryptedData)
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return await response.json();
|
|
69
|
+
}
|
|
70
70
|
});
|
|
71
71
|
|
|
72
72
|
export { GET, POST };
|
|
@@ -75,7 +75,7 @@ export { GET, POST };
|
|
|
75
75
|
### 3. Manual Server Setup
|
|
76
76
|
|
|
77
77
|
```typescript
|
|
78
|
-
import { createHpkeServer } from '@
|
|
78
|
+
import { createHpkeServer } from '@ubay182/sveltekit-hpke-wrapper';
|
|
79
79
|
|
|
80
80
|
const server = createHpkeServer();
|
|
81
81
|
|
|
@@ -94,84 +94,94 @@ const encrypted = await server.encrypt(responseData, clientPublicKey);
|
|
|
94
94
|
### Core Functions
|
|
95
95
|
|
|
96
96
|
#### `generateKeyPair()`
|
|
97
|
+
|
|
97
98
|
Generate a new HPKE key pair.
|
|
98
99
|
|
|
99
100
|
```typescript
|
|
100
101
|
async function generateKeyPair(): Promise<{
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
102
|
+
publicKey: any; // XCryptoKey for HPKE operations
|
|
103
|
+
privateKey: any; // XCryptoKey for HPKE operations
|
|
104
|
+
publicKeyRaw: Uint8Array; // Raw bytes for transmission
|
|
105
|
+
}>;
|
|
105
106
|
```
|
|
106
107
|
|
|
107
108
|
#### `hpkeEncrypt(message, recipientPublicKey)`
|
|
109
|
+
|
|
108
110
|
Encrypt a message.
|
|
109
111
|
|
|
110
112
|
```typescript
|
|
111
113
|
async function hpkeEncrypt(
|
|
112
|
-
|
|
113
|
-
|
|
114
|
+
message: string,
|
|
115
|
+
recipientPublicKey: any
|
|
114
116
|
): Promise<{
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
117
|
+
ciphertext: ArrayBuffer;
|
|
118
|
+
enc: ArrayBuffer;
|
|
119
|
+
}>;
|
|
118
120
|
```
|
|
119
121
|
|
|
120
122
|
#### `hpkeDecrypt(ciphertext, enc, recipientPrivateKey)`
|
|
123
|
+
|
|
121
124
|
Decrypt a message.
|
|
122
125
|
|
|
123
126
|
```typescript
|
|
124
127
|
async function hpkeDecrypt(
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
): Promise<string
|
|
128
|
+
ciphertext: ArrayBuffer,
|
|
129
|
+
enc: Uint8Array | ArrayBuffer,
|
|
130
|
+
recipientPrivateKey: any
|
|
131
|
+
): Promise<string>;
|
|
129
132
|
```
|
|
130
133
|
|
|
131
134
|
#### `exportKeyToBase64(publicKey)`
|
|
135
|
+
|
|
132
136
|
Export public key to base64.
|
|
133
137
|
|
|
134
138
|
```typescript
|
|
135
|
-
function exportKeyToBase64(publicKey: any): string
|
|
139
|
+
function exportKeyToBase64(publicKey: any): string;
|
|
136
140
|
```
|
|
137
141
|
|
|
138
142
|
#### `importKeyFromBase64(base64)`
|
|
143
|
+
|
|
139
144
|
Import public key from base64.
|
|
140
145
|
|
|
141
146
|
```typescript
|
|
142
|
-
async function importKeyFromBase64(base64: string): Promise<any
|
|
147
|
+
async function importKeyFromBase64(base64: string): Promise<any>;
|
|
143
148
|
```
|
|
144
149
|
|
|
145
150
|
### Server Functions
|
|
146
151
|
|
|
147
152
|
#### `createHpkeServer(config?)`
|
|
153
|
+
|
|
148
154
|
Create HPKE server instance.
|
|
149
155
|
|
|
150
156
|
```typescript
|
|
151
157
|
interface HpkeServerConfig {
|
|
152
|
-
|
|
158
|
+
autoGenerateKeys?: boolean; // Default: true
|
|
153
159
|
}
|
|
154
160
|
|
|
155
161
|
interface HpkeServerInstance {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
+
getPublicKeyBase64(): string;
|
|
163
|
+
decrypt(ciphertext: string, enc: string, clientPublicKey: string): Promise<string>;
|
|
164
|
+
encrypt(
|
|
165
|
+
message: string,
|
|
166
|
+
clientPublicKey: string
|
|
167
|
+
): Promise<{
|
|
168
|
+
ciphertext: string;
|
|
169
|
+
enc: string;
|
|
170
|
+
}>;
|
|
162
171
|
}
|
|
163
172
|
```
|
|
164
173
|
|
|
165
174
|
### SvelteKit Integration
|
|
166
175
|
|
|
167
176
|
#### `createHpkeEndpoint(config?)`
|
|
177
|
+
|
|
168
178
|
Create complete API endpoints.
|
|
169
179
|
|
|
170
180
|
```typescript
|
|
171
181
|
interface HpkeEndpointConfig {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
182
|
+
autoGenerateKeys?: boolean;
|
|
183
|
+
onRequest?: (decrypted: any, request: Request) => Promise<any>;
|
|
184
|
+
onError?: (error: Error, request: Request) => Promise<Response>;
|
|
175
185
|
}
|
|
176
186
|
```
|
|
177
187
|
|
|
@@ -180,7 +190,7 @@ interface HpkeEndpointConfig {
|
|
|
180
190
|
### Custom Algorithm (ChaCha20-Poly1305)
|
|
181
191
|
|
|
182
192
|
```typescript
|
|
183
|
-
import { createHpkeSuiteChaCha20 } from '@
|
|
193
|
+
import { createHpkeSuiteChaCha20 } from '@ubay182/sveltekit-hpke-wrapper';
|
|
184
194
|
|
|
185
195
|
const suite = createHpkeSuiteChaCha20();
|
|
186
196
|
// Use suite for encryption/decryption
|
|
@@ -189,7 +199,7 @@ const suite = createHpkeSuiteChaCha20();
|
|
|
189
199
|
### Manual Key Management
|
|
190
200
|
|
|
191
201
|
```typescript
|
|
192
|
-
import { createHpkeServer } from '@
|
|
202
|
+
import { createHpkeServer } from '@ubay182/sveltekit-hpke-wrapper';
|
|
193
203
|
|
|
194
204
|
// Disable auto-generation
|
|
195
205
|
const server = createHpkeServer({ autoGenerateKeys: false });
|
|
@@ -202,17 +212,17 @@ const server = createHpkeServer({ autoGenerateKeys: false });
|
|
|
202
212
|
|
|
203
213
|
```typescript
|
|
204
214
|
const { GET, POST } = createHpkeEndpoint({
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
215
|
+
onError: async (error, request) => {
|
|
216
|
+
console.error('HPKE Error:', error);
|
|
217
|
+
|
|
218
|
+
return new Response(
|
|
219
|
+
JSON.stringify({
|
|
220
|
+
error: 'Encryption failed',
|
|
221
|
+
code: 'HPKE_ERROR'
|
|
222
|
+
}),
|
|
223
|
+
{ status: 500 }
|
|
224
|
+
);
|
|
225
|
+
}
|
|
216
226
|
});
|
|
217
227
|
```
|
|
218
228
|
|
package/package.json
CHANGED