-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidentity.js
More file actions
39 lines (33 loc) · 907 Bytes
/
identity.js
File metadata and controls
39 lines (33 loc) · 907 Bytes
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
38
39
const crypto = require('crypto')
const LocalDatabase = require('./local-database')
const db = LocalDatabase('identity')
const init = async () => {
const identities = await db.find({})
if (!identities.length) {
const { publicKey, privateKey } = generate()
const publicKeyHash = getKeyHash(publicKey)
const privateKeyHash = getKeyHash(privateKey)
await db.insert({
publicKey,
publicKeyHash,
privateKey,
privateKeyHash,
})
}
}
const getKeyHash = key => {
const hash = crypto.createHash('sha256')
hash.update(key)
return hash.digest().toString('hex')
}
const generate = () => {
return crypto.generateKeyPairSync('rsa', {
modulusLength: 4096,
publicKeyEncoding: { type: 'spki', format: 'pem' },
privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
})
}
module.exports = async () => {
await init()
return (await db.find({}))[0]
}