Created
May 18, 2019 22:26
-
-
Save zackbloom/96e946e0c616e3c0bf160d5b93cf8e31 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const crypto = require('crypto'); | |
exports.handler = async (event) => { | |
const key = crypto.pbkdf2Sync(crypto.randomBytes(16), crypto.randomBytes(16), 35000, 32, 'sha512'); | |
return { | |
"isBase64Encoded": false, | |
"statusCode": 200, | |
"headers": { }, | |
"body": key.toString('hex') | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addEventListener('fetch', event => { | |
event.respondWith(handleRequest(event.request)) | |
}) | |
function buf2hex(buffer) { | |
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join(''); | |
} | |
async function handleRequest(request) { | |
const key = await crypto.subtle.importKey( | |
"raw", | |
crypto.getRandomValues(new Uint8Array(16)), | |
{ | |
name: "PBKDF2", | |
}, | |
false, | |
["deriveKey", "deriveBits"] | |
) | |
var resp = await crypto.subtle.deriveBits({ | |
name: "PBKDF2", | |
salt: crypto.getRandomValues(new Uint8Array(16)), | |
iterations: 35000, | |
hash: {name: "SHA-512"}}, | |
key, | |
256 | |
) | |
return new Response(buf2hex(resp)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment