I'm currently trying to wrap my head around jwcrypto and json[steak] or whatever the cool kids call it there days so you can assume that I'm missing some major points here.
I'm trying to generate a DSA keypair, store it and at some point retrieve it and get back the secret and public keys. The serialization and deserialization methods seemed like a nice place to start so here goes.
var fs = require("fs");
var jwcrypto = require("jwcrypto");
var algs = require("jwcrypto/lib/algs");
require("jwcrypto/lib/algs/ds");
var keyPairObj = {
algorithm: "DSA",
keysize: 128
};
console.info("# Generating a new keypair.");
jwcrypto.generateKeypair(keyPairObj, function(err, keyPair) {
if (err) { return console.error(err); }
keyPair.secretKey.serializeToObject(keyPairObj);
keyPair.publicKey.serializeToObject(keyPairObj);
console.info("generated keyPair.secretKey.keysize is", keyPair.secretKey.keysize);
console.info("generated keyPair.secretKey.p.bitLength() is", keyPair.secretKey.p.bitLength());
console.info("generated keyPair.publicKey.keysize is", keyPair.publicKey.keysize);
console.info("generated keyPair.publicKey.y.bitLength() is", keyPair.publicKey.y.bitLength());
console.info("# Now using the serialized object.", JSON.stringify(keyPairObj));
// Comment this to see the first issue.
keyPairObj.algorithm = "DS";
keyPair = new algs.KeyPair();
keyPair.secretKey = jwcrypto.loadSecretKeyFromObject(keyPairObj);
keyPair.publicKey = jwcrypto.loadPublicKeyFromObject(keyPairObj);
console.info("loaded keyPair.secretKey.keysize is", keyPair.secretKey.keysize);
console.info("loaded keyPair.secretKey.p.bitLength() is", keyPair.secretKey.p.bitLength());
console.info("loaded keyPair.publicKey.keysize is", keyPair.publicKey.keysize);
console.info("loaded keyPair.publicKey.y.bitLength() is", keyPair.publicKey.y.bitLength());
});
# Generating a new keypair.
generated keyPair.secretKey.keysize is 128
generated keyPair.secretKey.p.bitLength() is 1024
generated keyPair.publicKey.keysize is 128
generated keyPair.publicKey.y.bitLength() is 1024
# Now using the serialized object. {"algorithm":"DSA","keysize":128,"x":"zZxiaf238dMD6LEgHlKDFpiySkY","p":"_2AEg9tqv8W0Xqt4WUs1M9VQ2fG_Kpkqeo2qbcNPgEWtTm4MQp0zTu6q79fiPUgQvgDkzBSSy6MluoH_LVpbMFqNF-s79KBqNJ05LgDTKXRKUXk4A0ToKhjEeTNDj4keIq7vgS1pyPdeMmy3DqAAw_d239vWBGOMLvcX_CbQLhc","q":"4h4E-RHR7XmRAI7Kqzv3dZhDCcM","g":"xSpKD_O35h_fGGfOhBODaaYVT0r6kpZuPIJ-Jc-mz1CLkOXeQZ4TN-B6Lp4qPNXepwTRdfjr9q85fWnhELlq-xfHoDJZMp5IKbDQO7x4lrFbSt5T4TCFjMNNliaaqJBB9AkTbHJCo4iVydW8ytTzia8dekvROYvQct_6iWIzOXo","y":"7veH-9ta3-bmxGrlhbiKKPYHu4pDy5O4Hff3iwNC-umWVvbIcxT7Df4wExaqUrrxnkoBDWoQNHWyLkHNZsyahLkAKDkghIa3_mgVaN-foIQdkEKcQDSechHCbhS-DsBwHiyyPEREZDhHlFOagBu5lUBel0MiKdgfkrsIwPHYaRE"}
loaded keyPair.secretKey.keysize is null
loaded keyPair.secretKey.p.bitLength() is 658
loaded keyPair.publicKey.keysize is null
loaded keyPair.publicKey.y.bitLength() is 659