File
Blob: src/workerd/server/tests/inspector/index.mjs
| 1 | // Copyright (c) 2024 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | // index.mjs |
| 5 | import { Buffer } from 'node:buffer'; |
| 6 | |
| 7 | const encoder = new TextEncoder(); |
| 8 | |
| 9 | async function pbkdf2Derive(password) { |
| 10 | const passwordArray = encoder.encode(password); |
| 11 | const passwordKey = await crypto.subtle.importKey( |
| 12 | 'raw', |
| 13 | passwordArray, |
| 14 | 'PBKDF2', |
| 15 | false, |
| 16 | ['deriveBits'] |
| 17 | ); |
| 18 | const saltArray = crypto.getRandomValues(new Uint8Array(16)); |
| 19 | const keyBuffer = await crypto.subtle.deriveBits( |
| 20 | { name: 'PBKDF2', hash: 'SHA-256', salt: saltArray, iterations: 1_000_000 }, |
| 21 | passwordKey, |
| 22 | 256 |
| 23 | ); |
| 24 | return Buffer.from(keyBuffer).toString('base64'); |
| 25 | } |
| 26 | |
| 27 | export default { |
| 28 | async fetch(request, env, ctx) { |
| 29 | return new Response(await pbkdf2Derive('hello!')); |
| 30 | }, |
| 31 | }; |