-
Notifications
You must be signed in to change notification settings - Fork 3
/
gen-selfsign.ts
24 lines (22 loc) · 1001 Bytes
/
gen-selfsign.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { bytesToBase64 } from '@ucla-irl/ndnts-aux/utils';
import { Encoder } from '@ndn/tlv';
import { Name } from '@ndn/packet';
import { Certificate, CertNaming, createSigner, createVerifier, ECDSA } from '@ndn/keychain';
if (import.meta.main) {
if (Deno.args.length < 1 || !Deno.args[0]) {
console.error('Please input the identity name');
Deno.exit(1);
}
// Generate key pair
const idName = new Name(Deno.args[0]);
const keyName = CertNaming.makeKeyName(idName);
const algo = ECDSA;
const gen = await ECDSA.cryptoGenerate({}, true);
const privateKey = createSigner(keyName, algo, gen);
const publicKey = createVerifier(keyName, algo, gen);
const prvKeyBits = await crypto.subtle.exportKey('pkcs8', gen.privateKey);
const cert = await Certificate.selfSign({ privateKey, publicKey });
const certBits = Encoder.encode(cert.data);
console.log('CERTIFICATE: ', bytesToBase64(certBits));
console.log('PRIVATE KEY: ', bytesToBase64(new Uint8Array(prvKeyBits)));
}