-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
37 lines (31 loc) · 958 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { createServer } from "http"
import { generateKeyPair, privateDecrypt } from "crypto"
import { promisify } from "util"
// Generate a one-run RSA key pair
const { publicKey, privateKey } = await promisify(generateKeyPair)('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem'
}
})
// Print the public key to the admin who just started this app
console.log(publicKey)
// Start a server to decrypt on demand
const requestListener = async (req, res) => {
const dataBuffers = []
// We might receive data in multiple chunks
for await (const chunk of req) {
dataBuffers.push(chunk)
}
const dataBuffer = Buffer.concat(dataBuffers)
// Send back decrypted data using the in-memory key
res.writeHead(200)
res.end(privateDecrypt(privateKey, dataBuffer).toString())
}
const server = createServer(requestListener)
server.listen(3000)