This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeytool.js
56 lines (42 loc) · 1.56 KB
/
keytool.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const crypto = require('crypto')
const jose = require('jose')
async function validate_keys(baseSK, basePK) {
const test_msg = 'this is only a test'
const buffSK = Buffer.from(baseSK, 'base64')
const jsonSK = JSON.parse(buffSK)
const skey = await jose.importJWK(jsonSK, 'ES256')
const sign = crypto.createSign('SHA256')
sign.write(test_msg)
sign.end()
const sig = sign.sign(skey, 'base64')
const buffPK = Buffer.from(basePK, 'base64')
const jsonPK = JSON.parse(buffPK)
const pkey = await jose.importJWK(jsonPK, 'ES256')
const verify = crypto.createVerify('SHA256')
verify.write(test_msg)
verify.end()
return verify.verify(pkey, sig, 'base64')
}
async function start() {
const ALGO = 'ES256'
const EXTR = true
const { publicKey, privateKey } = await jose.generateKeyPair(ALGO, { extractable: EXTR })
const exportPK = await jose.exportJWK(publicKey)
const exportSK = await jose.exportJWK(privateKey)
const jsonPK = JSON.stringify(exportPK)
const jsonSK = JSON.stringify(exportSK)
const buffPK = Buffer.from(jsonPK)
const buffSK = Buffer.from(jsonSK)
const basePK = buffPK.toString('base64')
const baseSK = buffSK.toString('base64')
// Test key integrity:
if (await validate_keys(baseSK, basePK)) {
console.log('SECRET KEY (NOT FOR SHARING)\t', baseSK)
console.log('PUBLIC KEY (SHARE WITH KUBELT)\t', basePK)
process.exit(0)
} else {
console.error('Public key cannot validate private key signatures!')
process.exit(1)
}
}
start()