forked from ibmruntimes/node-zcrypto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
65 lines (51 loc) · 2.06 KB
/
example.js
File metadata and controls
65 lines (51 loc) · 2.06 KB
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
57
58
59
60
61
62
63
64
65
/*
* Licensed Materials - Property of IBM
* (C) Copyright IBM Corp. 2020. All Rights Reserved.
* US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*/
const zcrypto = require('./');
const fs = require('fs')
if (fs.existsSync("my.kdb")) {
fs.unlinkSync("my.kdb");
}
if (fs.existsSync("Cert.p12.nodedup")) {
fs.unlinkSync("Cert.p12.nodedup");
}
var crypt = new zcrypto.ZCrypto();
// Create a KDB file with a password, length and expiry time
var rc =crypt.createKDB("/tmp/mysdfasdfsdfas.kdb", "password", 10024, 0);
console.log(rc)
console.log(crypt.getErrorString(rc));
// Open kdb file if it exists
crypt.openKDB("my.kdb", "password");
// Racf Keyring
var rc = crypt.openKeyRing("ITODOR");
console.log(crypt.getErrorString(rc));
// Import a P12 file as Cert.p12 using password and label
// Update only allowed for KDB
rc = crypt.importKey("Cert.p12", "root", "MYCERT3");
console.log(crypt.getErrorString(rc));
// Export to P12 using password and label
crypt.exportKeyToFile("Cert.p12.nodedup", "root", "MYCERT3");
// Convert from KDB/RACF Keyring to a PEM file
var pem = zcrypto.ConvertP12LabelToPEM(crypt, "MYCERT3", "root");
console.log(pem.key);
console.log(pem.cert);
// Convert from P12 File to PEM
pem = zcrypto.ConvertP12FileToPEM("Cert.p12.nodedup", "root");
console.log(pem.key);
console.log(pem.cert);
// Load modules that we'll use.
const https = require('https'); // HTTPS web server.
// Configuration for the HTTPS web server.
const options = {
key: pem.key,
cert: pem.cert,
};
// Create the https server and begin listening for requests.
https.createServer(options, (req, res) => {
// This callback function will be called when someone makes a request to the https server.
// req will contain the request data, while res will allow a response to be sent back.
res.writeHead(200); // Set the HTTP response status codes to 200 OK.
res.end('hello world\n'); // Write the string 'hello world\n' to the body, and send the response back.
}).listen(3000); // Listen for requests on port 3000.