test PEM -> JWK, PEM -> PEM, JWK -> PEM, JWK -> JWK

This commit is contained in:
AJ ONeal 2016-08-02 15:29:11 -04:00
parent a7124fc656
commit 5de4362c6c
1 changed files with 66 additions and 4 deletions

View File

@ -12,18 +12,80 @@ var refs = {
, privJwk: RSA.exportPrivateJwk({ privateKeyPem: privkeyPemRef })
};
var hasUrsa;
var imported;
try {
hasUrsa = require('ursa') && true;
} catch(e) {
hasUrsa = false;
}
//
//
// PEM tests
//
//
console.log('');
console.log('valid pem', privkeyPemRef === refs.privPem);
console.log('JWK -> PEM ?', privkeyPemRef === refs.privPem);
if (privkeyPemRef !== refs.privPem) {
console.log(privkeyPemRef);
console.log(refs.privPem);
// 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('');
console.log('valid jwk', privkeyJwkRef.n === refs.privJwk.n);
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 });
}
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");
}
console.log('');
//
//
// JWK tests
//
//
console.log('');
console.log('PEM -> JWK', privkeyJwkRef.n === refs.privJwk.n);
if (privkeyJwkRef.n !== refs.privJwk.n) {
console.log('REF:');
console.log(privkeyJwkRef);
console.log('GEN:');
console.log(refs.privJwk);
throw new Error("Failed to validate importedPem against referenceJwk");
}
console.log('');
console.log('JWK -> _ -> JWK', privkeyJwkRef.n === refs.privJwk2.n);
imported = RSA.import({ privateKeyJwk: privkeyJwkRef });
refs.privJwk2 = RSA.exportPrivateJwk({ _forge: imported._forge });
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");
}
console.log('');