2016-08-02 14:36:37 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var RSA = require('../').RSA;
|
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
|
|
|
|
var privkeyPemRef = fs.readFileSync(path.join(__dirname, 'privkey.pem'), 'ascii');
|
|
|
|
var privkeyJwkRef = JSON.parse(fs.readFileSync(path.join(__dirname, 'privkey.jwk'), 'ascii'));
|
|
|
|
|
|
|
|
var refs = {
|
|
|
|
privPem: RSA.exportPrivatePem({ privateKeyJwk: privkeyJwkRef })
|
|
|
|
, privJwk: RSA.exportPrivateJwk({ privateKeyPem: privkeyPemRef })
|
|
|
|
};
|
|
|
|
|
2016-08-02 19:29:11 +00:00
|
|
|
var hasUrsa;
|
|
|
|
var imported;
|
|
|
|
|
|
|
|
try {
|
|
|
|
hasUrsa = require('ursa') && true;
|
|
|
|
} catch(e) {
|
|
|
|
hasUrsa = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// PEM tests
|
|
|
|
//
|
|
|
|
//
|
|
|
|
console.log('JWK -> PEM ?', privkeyPemRef === refs.privPem);
|
2016-08-02 14:36:37 +00:00
|
|
|
if (privkeyPemRef !== refs.privPem) {
|
2016-08-02 19:29:11 +00:00
|
|
|
// Watch out for tricky whitespaces (\n instead of \r\n, trailing \r\n, etc)
|
|
|
|
console.log('REF:');
|
|
|
|
console.log(JSON.stringify(privkeyPemRef));
|
|
|
|
console.log('GEN:');
|
|
|
|
console.log(JSON.stringify(refs.privPem));
|
|
|
|
throw new Error("Failed to validate importedJwk against referencePem");
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('PEM -> _ -> PEM ?', privkeyPemRef === refs.privPem);
|
|
|
|
if (hasUrsa) {
|
|
|
|
imported = RSA.import({ privateKeyPem: privkeyPemRef });
|
|
|
|
refs.privPem2 = RSA.exportPrivatePem({ _ursa: imported._ursa });
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
imported = RSA.import({ privateKeyPem: privkeyPemRef });
|
|
|
|
refs.privPem2 = RSA.exportPrivatePem({ _forge: imported._forge });
|
2016-08-02 14:36:37 +00:00
|
|
|
}
|
2016-08-02 19:29:11 +00:00
|
|
|
if (privkeyPemRef !== refs.privPem2) {
|
|
|
|
console.log('REF:');
|
|
|
|
console.log(JSON.stringify(privkeyPemRef));
|
|
|
|
console.log('GEN:');
|
|
|
|
console.log(JSON.stringify(refs.privPem2));
|
|
|
|
throw new Error("Failed to validate importedPem against referencePem");
|
|
|
|
}
|
|
|
|
|
2016-08-02 14:36:37 +00:00
|
|
|
|
2016-08-02 19:29:11 +00:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// JWK tests
|
|
|
|
//
|
|
|
|
//
|
2016-08-02 20:42:44 +00:00
|
|
|
console.log('PEM -> JWK ?', privkeyJwkRef.n === refs.privJwk.n);
|
2016-08-02 22:03:03 +00:00
|
|
|
if (![ 'kty', 'n', 'e', 'p', 'q', 'dp', 'dq', 'qi', 'd' ].every(function (k) {
|
|
|
|
return privkeyJwkRef[k] === refs.privJwk[k];
|
|
|
|
})) {
|
2016-08-02 19:29:11 +00:00
|
|
|
console.log('REF:');
|
2016-08-02 14:36:37 +00:00
|
|
|
console.log(privkeyJwkRef);
|
2016-08-02 19:29:11 +00:00
|
|
|
console.log('GEN:');
|
2016-08-02 14:36:37 +00:00
|
|
|
console.log(refs.privJwk);
|
2016-08-02 19:29:11 +00:00
|
|
|
throw new Error("Failed to validate importedPem against referenceJwk");
|
|
|
|
}
|
|
|
|
|
|
|
|
imported = RSA.import({ privateKeyJwk: privkeyJwkRef });
|
|
|
|
refs.privJwk2 = RSA.exportPrivateJwk({ _forge: imported._forge });
|
2016-08-02 20:42:44 +00:00
|
|
|
console.log('JWK -> _ -> JWK ?', privkeyJwkRef.n === refs.privJwk2.n);
|
2016-08-02 19:29:11 +00:00
|
|
|
if (privkeyJwkRef.n !== refs.privJwk2.n) {
|
|
|
|
console.log('REF:');
|
|
|
|
console.log(privkeyJwkRef);
|
|
|
|
console.log('GEN:');
|
|
|
|
console.log(refs.privJwk2);
|
|
|
|
throw new Error("Failed to validate importedJwk against referenceJwk");
|
2016-08-02 14:36:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log('');
|