UNPKG

rsa-key-es5

Version:

[ES5 version] Converts between RSA key formats, detecting input format (PEM, DER, PKCS1, PKCS8). No OpenSSL needed.

362 lines (286 loc) 11.6 kB
'use strict'; var _require = require('chai'), assert = _require.assert; var RSAKey = require('../src'); var NodeRSA = require('node-rsa'); describe('Module entry point', function () { it('should return RSAKey class', function () { assert.equal(RSAKey.name, 'RSAKey'); }); }); describe('RSAKey', function () { var testKey = void 0, testEncodedPrivatePKCS8PEM = void 0, testEncodedPublicPKCS8PEM = void 0, pubKey = void 0, privKey = void 0; before(function () { this.timeout(10000); testKey = new NodeRSA({ b: 256 }); testEncodedPrivatePKCS8PEM = testKey.exportKey('pkcs8-private-pem'); testEncodedPublicPKCS8PEM = testKey.exportKey('pkcs8-public-pem'); pubKey = new RSAKey(testEncodedPublicPKCS8PEM); privKey = new RSAKey(testEncodedPrivatePKCS8PEM); }); describe('Creation and importing', function () { var key = void 0; it('should create an empty object', function () { key = new RSAKey(); assert.instanceOf(key, RSAKey); }); it('should have empty components property', function () { assert.isObject(key.components); assert.deepEqual(key.components, {}); }); it('hasKey should be false', function () { assert.isFalse(key.hasKey()); }); it('isEmpty should be true', function () { assert.isTrue(key.isEmpty()); }); it('getType should return null', function () { assert.isNull(key.getType()); }); it('should fail trying to export key', function () { assert.throws(function () { key.exportKey(); }, 'key is empty'); }); it('should fail trying to generate fingerprint', function () { assert.throws(function () { key.getFingerprint(); }, 'key is empty'); }); it('_requireKey should throw', function () { assert.throws(function () { key._requireKey(); }, 'key is empty'); }); it('should import a key', function () { assert.doesNotThrow(function () { key.importKey(testEncodedPrivatePKCS8PEM); assert.isObject(key.components); }); }); it('should import a key while creating RSAKey object', function () { assert.doesNotThrow(function () { var keyImported = new RSAKey(testEncodedPublicPKCS8PEM); assert.isObject(keyImported); }); }); it('should import from another RSAKey instance', function () { var newKey = new RSAKey(); newKey.importKey(key); assert.deepEqual(newKey.components, key.components); }); }); describe('Handling public keys', function () { it('hasKey should be true', function () { assert.isTrue(pubKey.hasKey()); }); it('isEmpty should be false', function () { assert.isFalse(pubKey.isEmpty()); }); it('isPublic should be true', function () { assert.isTrue(pubKey.isPublic()); }); it('isPublic(true) should be true', function () { assert.isTrue(pubKey.isPublic(true)); }); it('isPrivate should be false', function () { assert.isFalse(pubKey.isPrivate()); }); it('getType should return public', function () { assert.equal(pubKey.getType(), 'public'); }); it('_requireKey should not throw', function () { assert.doesNotThrow(function () { pubKey._requireKey(); }); }); it('_requireKey should throw if private key is required', function () { assert.throws(function () { pubKey._requireKey('test', true); }, 'this is a public key'); }); describe('exporting key', function () { it('should fail trying to export private PKCS8 PEM key', function () { assert.throws(function () { pubKey.exportKey('pkcs8', 'private', 'pem'); }, 'components are missing'); }); it('should fail trying to export private PKCS1 PEM key', function () { assert.throws(function () { pubKey.exportKey('pkcs1', 'private', 'pem'); }, 'components are missing'); }); it('should export public PKCS8 PEM key', function () { var exported = pubKey.exportKey('pkcs8', 'public', 'pem'); var correct = testKey.exportKey('pkcs8-public-pem'); assert.equal(exported, correct); }); it('should export public PKCS1 PEM key', function () { var exported = pubKey.exportKey('pkcs1', 'public', 'pem'); var correct = testKey.exportKey('pkcs1-public-pem'); assert.equal(exported, correct); }); it('should fail trying to export private PKCS8 DER key', function () { assert.throws(function () { pubKey.exportKey('pkcs8', 'private', 'der').toString('base64'); }, 'components are missing'); }); it('should fail trying to export private PKCS1 DER key', function () { assert.throws(function () { pubKey.exportKey('pkcs1', 'private', 'der').toString('base64'); }, 'components are missing'); }); it('should export public PKCS8 DER key', function () { var exported = pubKey.exportKey('pkcs8', 'public', 'der').toString('base64'); var correct = testKey.exportKey('pkcs8-public-der').toString('base64'); assert.equal(exported, correct); }); it('should export public PKCS1 DER key', function () { var exported = pubKey.exportKey('pkcs1', 'public', 'der').toString('base64'); var correct = testKey.exportKey('pkcs1-public-der').toString('base64'); assert.equal(exported, correct); }); it('should export public PKCS8 PEM key by default', function () { var exported = pubKey.exportKey(); var correct = testKey.exportKey('pkcs8-public-pem'); assert.equal(exported, correct); }); }); }); describe('Handling private keys', function () { it('hasKey should be true', function () { assert.isTrue(privKey.hasKey()); }); it('isEmpty should be false', function () { assert.isFalse(privKey.isEmpty()); }); it('isPublic should be true', function () { assert.isTrue(privKey.isPublic()); }); it('isPublic(true) should be false', function () { assert.isFalse(privKey.isPublic(true)); }); it('isPrivate should be true', function () { assert.isTrue(privKey.isPrivate()); }); it('getType should return private', function () { assert.equal(privKey.getType(), 'private'); }); it('_requireKey should not throw', function () { assert.doesNotThrow(function () { privKey._requireKey(); }); }); it('_requireKey should not throw if private key is required', function () { assert.doesNotThrow(function () { privKey._requireKey('test', true); }); }); describe('exporting key', function () { it('should export private PKCS8 PEM key', function () { var exported = privKey.exportKey('pkcs8', 'private', 'pem'); var correct = testKey.exportKey('pkcs8-private-pem'); assert.equal(exported, correct); }); it('should export private PKCS1 PEM key', function () { var exported = privKey.exportKey('pkcs1', 'private', 'pem'); var correct = testKey.exportKey('pkcs1-private-pem'); assert.equal(exported, correct); }); it('should export public PKCS8 PEM key', function () { var exported = privKey.exportKey('pkcs8', 'public', 'pem'); var correct = testKey.exportKey('pkcs8-public-pem'); assert.equal(exported, correct); }); it('should export public PKCS1 PEM key', function () { var exported = privKey.exportKey('pkcs1', 'public', 'pem'); var correct = testKey.exportKey('pkcs1-public-pem'); assert.equal(exported, correct); }); it('should export private PKCS8 DER key', function () { var exported = privKey.exportKey('pkcs8', 'private', 'der').toString('base64'); var correct = testKey.exportKey('pkcs8-private-der').toString('base64'); assert.equal(exported, correct); }); it('should export private PKCS1 DER key', function () { var exported = privKey.exportKey('pkcs1', 'private', 'der').toString('base64'); var correct = testKey.exportKey('pkcs1-private-der').toString('base64'); assert.equal(exported, correct); }); it('should export public PKCS8 DER key', function () { var exported = privKey.exportKey('pkcs8', 'public', 'der').toString('base64'); var correct = testKey.exportKey('pkcs8-public-der').toString('base64'); assert.equal(exported, correct); }); it('should export public PKCS1 DER key', function () { var exported = privKey.exportKey('pkcs1', 'public', 'der').toString('base64'); var correct = testKey.exportKey('pkcs1-public-der').toString('base64'); assert.equal(exported, correct); }); it('should export private PKCS8 PEM key by default', function () { var exported = privKey.exportKey(); var correct = testKey.exportKey('pkcs8-private-pem'); assert.equal(exported, correct); }); it('should fail trying to export key with invalid export params', function () { assert.throws(function () { privKey.exportKey('clearly-invalid-param'); }, 'Unknown key export parameter'); }); }); }); describe('Caching time-consuming operation results', function () { var key = void 0, exported = void 0; var specs = ['public', 'pkcs1', 'pem']; it('should export key', function () { var _key; key = new RSAKey(privKey); exported = (_key = key).exportKey.apply(_key, specs); assert.isString(exported); }); it('should export previously cached key', function () { var _key2; key.components.n = 'different value'; var newExport = (_key2 = key).exportKey.apply(_key2, specs); assert.equal(newExport, exported); }); it('should export a different key for different specs', function () { var _key3; key.components.n = privKey.components.n; var newExport = (_key3 = key).exportKey.apply(_key3, specs.concat(['pkcs8'])); assert.notEqual(newExport, exported); }); it('should clear cache when importing key', function () { var _key4; key.importKey(pubKey); key.components.n = 'different value'; var newExport = (_key4 = key).exportKey.apply(_key4, specs); assert.notEqual(newExport, exported); }); }); describe('Other methods', function () { var key = void 0; before(function () { key = new RSAKey(testEncodedPrivatePKCS8PEM); }); it('getKeySize should return key size in bits', function () { var size = key.getKeySize(); assert.equal(size, testKey.getKeySize()); }); it('getKeySize should return 0 for empty key', function () { var size = new RSAKey().getKeySize(); assert.equal(size, 0); }); it('getFingerprint should return key fingerprint', function () { var keyString = '-----BEGIN PUBLIC KEY-----\n' + 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8Z9kXlY0FtPi25ZLwQnI7OamZ\n' + 'VjWSIkNRcDrzN971MNKu0Q6LjDicCYH77iZKla6OgNxAB3uBHoZ27Wz1mMlqddbg\n' + 'HXe6ToEBQCW8K0gP4zGF+osV5B8iLoqCPiqCiCGcuX6ePs01AmFLkSzsqGdVu+tX\n' + 'L/+q3MOEIa7XYlO/lwIDAQAB\n' + '-----END PUBLIC KEY-----'; var correct = 'df:bc:31:2b:9e:10:6a:b9:2e:5f:27:5a:14:41:72:6f'; var fingerprint = new RSAKey(keyString).getFingerprint(); assert.equal(fingerprint, correct); }); }); });