Comparing version 0.2.13 to 0.2.20
{ | ||
"name": "node-rsa", | ||
"version": "0.2.13", | ||
"version": "0.2.20", | ||
"description": "Node.js RSA library", | ||
@@ -33,12 +33,12 @@ "main": "src/NodeRSA.js", | ||
"devDependencies": { | ||
"grunt": "^0.4.4", | ||
"grunt-simple-mocha": "^0.4.0", | ||
"jit-grunt": "^0.3.2", | ||
"chai": "^1.9.1", | ||
"grunt-contrib-jshint": "^0.9.2" | ||
"grunt": "0.4.5", | ||
"grunt-simple-mocha": "0.4.0", | ||
"jit-grunt": "0.9.1", | ||
"chai": "2.0.0", | ||
"grunt-contrib-jshint": "0.11.0" | ||
}, | ||
"dependencies": { | ||
"lodash": "^2.4.1", | ||
"asn1": "^0.2.0" | ||
"lodash": "3.3.0", | ||
"asn1": "0.2.2" | ||
} | ||
} |
@@ -32,2 +32,3 @@ # Node-RSA | ||
``` | ||
*Requires nodejs >= 0.10.x or io.js >= 1.x* | ||
@@ -60,24 +61,11 @@ ### Testing | ||
* environment — working environment, `'browser'` or `'node'`. Default autodetect. | ||
* environment — working environment (default autodetect): | ||
* `'browser'` — will run pure js implementation of RSA algorithms. | ||
* `'node'` for `nodejs >= 0.10.x or io.js >= 1.x` — provide some native methods like sign/verify and encrypt/decrypt. | ||
* encryptionScheme — padding scheme for encrypt/decrypt. Can be `'pkcs1_oaep'` or `'pkcs1'`. Default `'pkcs1_oaep'`. | ||
* signingScheme — scheme used for signing and verifying. Can be `'pkcs1'` or `'pss'` or 'scheme-hash' format string (eg `'pss-sha1'`). Default `'pkcs1-sha256'`, or, if chosen pss: `'pss-sha1'`. | ||
**Advanced options:**<br/> | ||
You also can specify advanced options for some schemes like this: | ||
```javascript | ||
options = { | ||
encryptionScheme: { | ||
scheme: 'pkcs1_oaep', //scheme | ||
hash: 'md5', //hash using for scheme | ||
mgf: function(...) {...} //mask generation function | ||
}, | ||
signingScheme: { | ||
scheme: 'pss', //scheme | ||
hash: 'sha1', //hash using for scheme | ||
saltLength: 20 //salt length for pss sign | ||
} | ||
} | ||
``` | ||
> *Notice:* This lib supporting next hash algorithms: `'md5'`, `'ripemd160'`, `'sha1'`, `'sha256'`, `'sha512'` in browser and node environment and additional `'md4'`, `'sha'`, `'sha224'`, `'sha384'` in node only. | ||
This lib supporting next hash algorithms: `'md5'`, `'ripemd160'`, `'sha1'`, `'sha256'`, `'sha512'` in browser and node environment and additional `'md4'`, `'sha'`, `'sha224'`, `'sha384'` in node only. | ||
<sub>Some [advanced options info](https://github.com/rzcoder/node-rsa/wiki/Advanced-options)</sub> | ||
@@ -89,3 +77,3 @@ #### Creating "empty" key | ||
#### Generate new key 512bit-length and with public exponent 65537 | ||
#### Generate new 512bit-length key | ||
```javascript | ||
@@ -141,3 +129,3 @@ var key = new NodeRSA({b: 512}); | ||
**Notice:** For import, if *keyData* is PEM string or buffer containing string, you can do not specify format, but if you provide *keyData* as DER you must specify it in format string. | ||
> *Notice:* For import, if *keyData* is PEM string or buffer containing string, you can do not specify format, but if you provide *keyData* as DER you must specify it in format string. | ||
@@ -189,2 +177,3 @@ **Shortcuts and examples** | ||
key.encrypt(buffer, [encoding], [source_encoding]); | ||
key.encryptPrivate(buffer, [encoding], [source_encoding]); // use private key for encryption | ||
``` | ||
@@ -199,2 +188,3 @@ Return encrypted data.<br/> | ||
key.decrypt(buffer, [encoding]); | ||
key.decryptPublic(buffer, [encoding]); // use public key for decryption | ||
``` | ||
@@ -206,2 +196,4 @@ Return decrypted data.<br/> | ||
> *Notice:* usage `encryptPrivate` and `decryptPublic` with `pkcs1_oaep` padding not described in the RSA [specification](http://www.emc.com/collateral/white-papers/h11300-pkcs-1v2-2-rsa-cryptography-standard-wp.pdf) | ||
### Signing/Verifying | ||
@@ -229,2 +221,8 @@ ```javascript | ||
### 0.2.20 | ||
* Added `.encryptPrivate()` and `.decryptPublic()` methods. | ||
* Encrypt/decrypt methods in nodejs 0.12.x and io.js using native implementation (> 40x speed boost). | ||
* Fixed some regex issue causing catastrophic backtracking. | ||
* *KNOWN ISSUE*:`encryptPrivate` and `decryptPublic` don't have native implementation in nodejs and can't be use in native implementation with pkcs1_oaep padding in io.js. | ||
### 0.2.10 | ||
@@ -239,9 +237,9 @@ * **Methods `.exportPrivate()` and `.exportPublic()` was replaced by `.exportKey([format])`.** | ||
* **`.loadFromPEM()` method was renamed to `.importKey()`** | ||
* Added PKCS1_OAEP encrypting/decrypting support | ||
* **PKCS1_OAEP now default scheme, you need to specify 'encryptingScheme' option to 'pkcs1' for compatibility with 0.1.x version of NodeRSA** | ||
* Added PSS signing/verifying support | ||
* Added PKCS1_OAEP encrypting/decrypting support. | ||
* **PKCS1_OAEP now default scheme, you need to specify 'encryptingScheme' option to 'pkcs1' for compatibility with 0.1.x version of NodeRSA.** | ||
* Added PSS signing/verifying support. | ||
* Signing now supports `'md5'`, `'ripemd160'`, `'sha1'`, `'sha256'`, `'sha512'` hash algorithms in both environments | ||
and additional `'md4'`, `'sha'`, `'sha224'`, `'sha384'` for nodejs env. | ||
* **`options.signingAlgorithm` was renamed to `options.signingScheme`** | ||
* Added `encryptingScheme` option | ||
* Added `encryptingScheme` option. | ||
* Property `key.options` now mark as private. Added `key.setOptions(options)` method. | ||
@@ -251,19 +249,19 @@ | ||
### 0.1.54 | ||
* Added support for loading PEM key from Buffer (`fs.readFileSync()` output) | ||
* Added `isEmpty()` method | ||
* Added support for loading PEM key from Buffer (`fs.readFileSync()` output). | ||
* Added `isEmpty()` method. | ||
### 0.1.52 | ||
* Improve work with not properly trimming PEM strings | ||
* Improve work with not properly trimming PEM strings. | ||
### 0.1.50 | ||
* Implemented native js signing and verifying for browsers | ||
* `options.signingAlgorithm` now takes only hash-algorithm name | ||
* Added `.getKeySize()` and `.getMaxMessageSize()` methods | ||
* `.loadFromPublicPEM` and `.loadFromPrivatePEM` methods marked as private | ||
* Implemented native js signing and verifying for browsers. | ||
* `options.signingAlgorithm` now takes only hash-algorithm name. | ||
* Added `.getKeySize()` and `.getMaxMessageSize()` methods. | ||
* `.loadFromPublicPEM` and `.loadFromPrivatePEM` methods marked as private. | ||
### 0.1.40 | ||
* Added signing/verifying | ||
* Added signing/verifying. | ||
### 0.1.30 | ||
* Added long message support | ||
* Added long message support. | ||
@@ -270,0 +268,0 @@ |
@@ -131,3 +131,3 @@ var ber = require('asn1').Ber; | ||
autoImport: function (key, data) { | ||
if (/^\s*-----BEGIN RSA PRIVATE KEY-----\s*([A-Za-z0-9+/=]+\s*)+-----END RSA PRIVATE KEY-----\s*$/g.test(data)) { | ||
if (/^\s*-----BEGIN RSA PRIVATE KEY-----\s*(?=(([A-Za-z0-9+/=]+\s*)+))\1-----END RSA PRIVATE KEY-----\s*$/g.test(data)) { | ||
module.exports.privateImport(key, data); | ||
@@ -137,3 +137,3 @@ return true; | ||
if (/^\s*-----BEGIN RSA PUBLIC KEY-----\s*([A-Za-z0-9+/=]+\s*)+-----END RSA PUBLIC KEY-----\s*$/g.test(data)) { | ||
if (/^\s*-----BEGIN RSA PUBLIC KEY-----\s*(?=(([A-Za-z0-9+/=]+\s*)+))\1-----END RSA PUBLIC KEY-----\s*$/g.test(data)) { | ||
module.exports.publicImport(key, data); | ||
@@ -140,0 +140,0 @@ return true; |
@@ -170,3 +170,3 @@ var ber = require('asn1').Ber; | ||
autoImport: function (key, data) { | ||
if (/^\s*-----BEGIN PRIVATE KEY-----\s*([A-Za-z0-9+/=]+\s*)+-----END PRIVATE KEY-----\s*$/g.test(data)) { | ||
if (/^\s*-----BEGIN PRIVATE KEY-----\s*(?=(([A-Za-z0-9+/=]+\s*)+))\1-----END PRIVATE KEY-----\s*$/g.test(data)) { | ||
module.exports.privateImport(key, data); | ||
@@ -176,3 +176,3 @@ return true; | ||
if (/^\s*-----BEGIN PUBLIC KEY-----\s*([A-Za-z0-9+/=]+\s*)+-----END PUBLIC KEY-----\s*$/g.test(data)) { | ||
if (/^\s*-----BEGIN PUBLIC KEY-----\s*(?=(([A-Za-z0-9+/=]+\s*)+))\1-----END PUBLIC KEY-----\s*$/g.test(data)) { | ||
module.exports.publicImport(key, data); | ||
@@ -179,0 +179,0 @@ return true; |
@@ -185,9 +185,21 @@ /* | ||
var k; | ||
switch(radix) { | ||
case 2: k=1; break; | ||
case 4: k=2; break; | ||
case 8: k=3; break; | ||
case 16: k=4; break; | ||
case 32: k=5; break; | ||
case 256: k=8; break; | ||
switch (radix) { | ||
case 2: | ||
k = 1; | ||
break; | ||
case 4: | ||
k = 2; | ||
break; | ||
case 8: | ||
k = 3; | ||
break; | ||
case 16: | ||
k = 4; | ||
break; | ||
case 32: | ||
k = 5; | ||
break; | ||
case 256: | ||
k = 8; | ||
break; | ||
default: | ||
@@ -843,9 +855,9 @@ this.fromRadix(data, radix); | ||
function bnEquals(a) { | ||
return(this.compareTo(a) == 0); | ||
return (this.compareTo(a) == 0); | ||
} | ||
function bnMin(a) { | ||
return(this.compareTo(a) < 0) ? this : a; | ||
return (this.compareTo(a) < 0) ? this : a; | ||
} | ||
function bnMax(a) { | ||
return(this.compareTo(a) > 0) ? this : a; | ||
return (this.compareTo(a) > 0) ? this : a; | ||
} | ||
@@ -986,4 +998,4 @@ | ||
var j = Math.floor(n / this.DB); | ||
if (j >= this.t) return(this.s != 0); | ||
return((this[j] & (1 << (n % this.DB))) != 0); | ||
if (j >= this.t) return (this.s != 0); | ||
return ((this[j] & (1 << (n % this.DB))) != 0); | ||
} | ||
@@ -990,0 +1002,0 @@ |
@@ -47,5 +47,6 @@ /* | ||
var schemes = require('../schemes/schemes.js'); | ||
var encryptEngines = require('../encryptEngines/encryptEngines.js'); | ||
exports.BigInteger = BigInteger; | ||
module.exports.Key = (function() { | ||
module.exports.Key = (function () { | ||
/** | ||
@@ -84,2 +85,4 @@ * RSA key constructor | ||
} | ||
this.encryptEngine = encryptEngines.getEngine(this, options); | ||
}; | ||
@@ -96,4 +99,4 @@ | ||
var ee = new BigInteger(E, 16); | ||
for (; ;) { | ||
for (; ;) { | ||
while (true) { | ||
while (true) { | ||
this.p = new BigInteger(B - qs, 1); | ||
@@ -103,3 +106,3 @@ if (this.p.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) === 0 && this.p.isProbablePrime(10)) | ||
} | ||
for (; ;) { | ||
while (true) { | ||
this.q = new BigInteger(qs, 1); | ||
@@ -213,3 +216,3 @@ if (this.q.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) === 0 && this.q.isProbablePrime(10)) | ||
*/ | ||
RSAKey.prototype.encrypt = function (buffer) { | ||
RSAKey.prototype.encrypt = function (buffer, usePrivate) { | ||
var buffers = []; | ||
@@ -221,22 +224,12 @@ var results = []; | ||
if ( buffersCount == 1) { | ||
if (buffersCount == 1) { | ||
buffers.push(buffer); | ||
} else { | ||
for (var bufNum = 0; bufNum < buffersCount; bufNum++) { | ||
buffers.push(buffer.slice(bufNum * dividedSize, (bufNum+1) * dividedSize)); | ||
buffers.push(buffer.slice(bufNum * dividedSize, (bufNum + 1) * dividedSize)); | ||
} | ||
} | ||
for(var i = 0; i < buffers.length; i++) { | ||
var buf = buffers[i]; | ||
var m = new BigInteger(this.encryptionScheme.encPad(buf)); | ||
var c = this.$doPublic(m); | ||
if (c === null) { | ||
return null; | ||
} | ||
var encryptedBuffer = c.toBuffer(this.encryptedDataLength); | ||
results.push(encryptedBuffer); | ||
for (var i = 0; i < buffers.length; i++) { | ||
results.push(this.encryptEngine.encrypt(buffers[i], usePrivate)); | ||
} | ||
@@ -252,3 +245,3 @@ | ||
*/ | ||
RSAKey.prototype.decrypt = function (buffer) { | ||
RSAKey.prototype.decrypt = function (buffer, usePublic) { | ||
if (buffer.length % this.encryptedDataLength > 0) { | ||
@@ -266,6 +259,3 @@ throw Error('Incorrect data or key'); | ||
length = offset + this.encryptedDataLength; | ||
var c = new BigInteger(buffer.slice(offset, Math.min(length, buffer.length))); | ||
var m = this.$doPrivate(c); | ||
result.push(this.encryptionScheme.encUnPad(m.toBuffer(this.encryptedDataLength))); | ||
result.push(this.encryptEngine.decrypt(buffer.slice(offset, Math.min(length, buffer.length)), usePublic)); | ||
} | ||
@@ -300,11 +290,17 @@ | ||
Object.defineProperty(RSAKey.prototype, 'keySize', { | ||
get: function() { return this.cache.keyBitLength; } | ||
get: function () { | ||
return this.cache.keyBitLength; | ||
} | ||
}); | ||
Object.defineProperty(RSAKey.prototype, 'encryptedDataLength', { | ||
get: function() { return this.cache.keyByteLength; } | ||
get: function () { | ||
return this.cache.keyByteLength; | ||
} | ||
}); | ||
Object.defineProperty(RSAKey.prototype, 'maxMessageLength', { | ||
get: function() { return this.encryptionScheme.maxMessageLength(); } | ||
get: function () { | ||
return this.encryptionScheme.maxMessageLength(); | ||
} | ||
}); | ||
@@ -311,0 +307,0 @@ |
@@ -20,3 +20,5 @@ /*! | ||
var SUPPORTED_HASH_ALGORITHMS = { | ||
node10: ['md4', 'md5', 'ripemd160', 'sha', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'], | ||
node: ['md4', 'md5', 'ripemd160', 'sha', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'], | ||
iojs: ['md4', 'md5', 'ripemd160', 'sha', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'], | ||
browser: ['md5', 'ripemd160', 'sha1', 'sha256', 'sha512'] | ||
@@ -223,3 +225,3 @@ }; | ||
/** | ||
* Encrypting data method | ||
* Encrypting data method with public key | ||
* | ||
@@ -232,4 +234,40 @@ * @param buffer {string|number|object|array|Buffer} - data for encrypting. Object and array will convert to JSON string. | ||
NodeRSA.prototype.encrypt = function (buffer, encoding, source_encoding) { | ||
return this.$$encryptKey(false, buffer, encoding, source_encoding); | ||
}; | ||
/** | ||
* Decrypting data method with private key | ||
* | ||
* @param buffer {Buffer} - buffer for decrypting | ||
* @param encoding - encoding for result string, can also take 'json' or 'buffer' for the automatic conversion of this type | ||
* @returns {Buffer|object|string} | ||
*/ | ||
NodeRSA.prototype.decrypt = function (buffer, encoding) { | ||
return this.$$decryptKey(false, buffer, encoding); | ||
}; | ||
/** | ||
* Encrypting data method with private key | ||
* | ||
* Parameters same as `encrypt` method | ||
*/ | ||
NodeRSA.prototype.encryptPrivate = function (buffer, encoding, source_encoding) { | ||
return this.$$encryptKey(true, buffer, encoding, source_encoding); | ||
}; | ||
/** | ||
* Decrypting data method with public key | ||
* | ||
* Parameters same as `decrypt` method | ||
*/ | ||
NodeRSA.prototype.decryptPublic = function (buffer, encoding) { | ||
return this.$$decryptKey(true, buffer, encoding); | ||
}; | ||
/** | ||
* Encrypting data method with custom key | ||
*/ | ||
NodeRSA.prototype.$$encryptKey = function (usePrivate, buffer, encoding, source_encoding) { | ||
try { | ||
var res = this.keyPair.encrypt(this.$getDataForEncrypt(buffer, source_encoding)); | ||
var res = this.keyPair.encrypt(this.$getDataForEncrypt(buffer, source_encoding), usePrivate); | ||
@@ -247,12 +285,8 @@ if (encoding == 'buffer' || !encoding) { | ||
/** | ||
* Decrypting data method | ||
* | ||
* @param buffer {Buffer} - buffer for decrypting | ||
* @param encoding - encoding for result string, can also take 'json' or 'buffer' for the automatic conversion of this type | ||
* @returns {Buffer|object|string} | ||
* Decrypting data method with custom key | ||
*/ | ||
NodeRSA.prototype.decrypt = function (buffer, encoding) { | ||
NodeRSA.prototype.$$decryptKey = function (usePublic, buffer, encoding) { | ||
try { | ||
buffer = _.isString(buffer) ? new Buffer(buffer, 'base64') : buffer; | ||
var res = this.keyPair.decrypt(buffer); | ||
var res = this.keyPair.decrypt(buffer, usePublic); | ||
@@ -259,0 +293,0 @@ if (res === null) { |
@@ -79,3 +79,3 @@ /** | ||
var label = this.options.encryptionSchemeOptions.label || new Buffer(0); | ||
var emLen = this.key.encryptedDataLength; | ||
var emLen = this.key.encryptedDataLength; | ||
@@ -82,0 +82,0 @@ var hLen = module.exports.digestLength[hash]; |
@@ -6,2 +6,4 @@ /* | ||
var crypt = require('crypto'); | ||
/** | ||
@@ -29,3 +31,2 @@ * Break string str each maxLen symbols | ||
} | ||
return 'node'; | ||
@@ -32,0 +33,0 @@ }; |
@@ -64,2 +64,10 @@ /** | ||
describe("Setup options", function(){ | ||
it("should use browser environment", function () { | ||
assert.equal((new NodeRSA(null, {environment: 'browser'})).$options.environment, 'browser'); | ||
}); | ||
it("should use io.js environment", function () { | ||
assert.equal((new NodeRSA(null, {environment: 'iojs'})).$options.environment, 'iojs'); | ||
}); | ||
it("should make empty key pair with default options", function () { | ||
@@ -108,3 +116,2 @@ var key = new NodeRSA(null); | ||
it("advanced options change", function () { | ||
@@ -344,48 +351,210 @@ var key = new NodeRSA(null); | ||
describe("Encrypting & decrypting", function () { | ||
for (var scheme_i in encryptSchemes) { | ||
(function (scheme) { | ||
describe("Encryption scheme: " + scheme, function () { | ||
describe("Good cases", function () { | ||
var encrypted = {}; | ||
var decrypted = {}; | ||
for (var i in dataBundle) { | ||
(function (i) { | ||
var key = null; | ||
var suit = dataBundle[i]; | ||
for (var env in environments) { | ||
(function (env) { | ||
for (var scheme_i in encryptSchemes) { | ||
(function (scheme) { | ||
describe("Environment: " + env + ". Encryption scheme: " + scheme, function () { | ||
describe("Good cases", function () { | ||
var encrypted = {}; | ||
var decrypted = {}; | ||
for (var i in dataBundle) { | ||
(function (i) { | ||
var key = null; | ||
var suit = dataBundle[i]; | ||
it("should encrypt " + i, function () { | ||
key = generatedKeys[Math.round(Math.random() * 1000) % generatedKeys.length]; | ||
key.setOptions({encryptionScheme: scheme}); | ||
encrypted[i] = key.encrypt(suit.data); | ||
assert(Buffer.isBuffer(encrypted[i])); | ||
assert(encrypted[i].length > 0); | ||
it("`encrypt()` should encrypt " + i, function () { | ||
key = new NodeRSA(generatedKeys[Math.round(Math.random() * 1000) % generatedKeys.length].exportKey(), { | ||
environment: env, | ||
encryptionScheme: scheme | ||
}); | ||
encrypted[i] = key.encrypt(suit.data); | ||
assert(Buffer.isBuffer(encrypted[i])); | ||
assert(encrypted[i].length > 0); | ||
}); | ||
it("`decrypt()` should decrypt " + i, function () { | ||
decrypted[i] = key.decrypt(encrypted[i], _.isArray(suit.encoding) ? suit.encoding[0] : suit.encoding); | ||
if (Buffer.isBuffer(decrypted[i])) { | ||
assert.equal(suit.data.toString('hex'), decrypted[i].toString('hex')); | ||
} else { | ||
assert(_.isEqual(suit.data, decrypted[i])); | ||
} | ||
}); | ||
})(i); | ||
} | ||
for (var i in dataBundle) { | ||
(function (i) { | ||
var key = null; | ||
var suit = dataBundle[i]; | ||
it("`encryptPrivate()` should encrypt " + i, function () { | ||
key = new NodeRSA(generatedKeys[Math.round(Math.random() * 1000) % generatedKeys.length].exportKey(), { | ||
environment: env, | ||
encryptionScheme: scheme | ||
}); | ||
encrypted[i] = key.encryptPrivate(suit.data); | ||
assert(Buffer.isBuffer(encrypted[i])); | ||
assert(encrypted[i].length > 0); | ||
}); | ||
it("`decryptPublic()` should decrypt " + i, function () { | ||
decrypted[i] = key.decryptPublic(encrypted[i], _.isArray(suit.encoding) ? suit.encoding[0] : suit.encoding); | ||
if (Buffer.isBuffer(decrypted[i])) { | ||
assert.equal(suit.data.toString('hex'), decrypted[i].toString('hex')); | ||
} else { | ||
assert(_.isEqual(suit.data, decrypted[i])); | ||
} | ||
}); | ||
})(i); | ||
} | ||
}); | ||
describe("Bad cases", function () { | ||
it("unsupported data types", function () { | ||
assert.throw(function () { | ||
generatedKeys[0].encrypt(null); | ||
}, Error, "Unexpected data type"); | ||
assert.throw(function () { | ||
generatedKeys[0].encrypt(undefined); | ||
}, Error, "Unexpected data type"); | ||
assert.throw(function () { | ||
generatedKeys[0].encrypt(true); | ||
}, Error, "Unexpected data type"); | ||
}); | ||
it("should decrypt " + i, function () { | ||
decrypted[i] = key.decrypt(encrypted[i], _.isArray(suit.encoding) ? suit.encoding[0] : suit.encoding); | ||
if (Buffer.isBuffer(decrypted[i])) { | ||
assert.equal(suit.data.toString('hex'), decrypted[i].toString('hex')); | ||
} else { | ||
assert(_.isEqual(suit.data, decrypted[i])); | ||
} | ||
it("incorrect key for decrypting", function () { | ||
var encrypted = generatedKeys[0].encrypt('data'); | ||
assert.throw(function () { | ||
generatedKeys[1].decrypt(encrypted); | ||
}, Error, "Error during decryption"); | ||
}); | ||
})(i); | ||
}); | ||
}); | ||
})(encryptSchemes[scheme_i]); | ||
} | ||
})(environments[env]); | ||
} | ||
describe("Compatibility of different environments", function () { | ||
var encrypted = {}; | ||
var decrypted = {}; | ||
for (var i in dataBundle) { | ||
(function (i) { | ||
var key1 = null; | ||
var key2 = null; | ||
var suit = dataBundle[i]; | ||
it("`encrypt()` by browser" + i, function () { | ||
var key = generatedKeys[Math.round(Math.random() * 1000) % generatedKeys.length].exportKey(); | ||
key1 = new NodeRSA(key, {environment: 'browser'}); | ||
key2 = new NodeRSA(key, {environment: 'node'}); | ||
encrypted[i] = key1.encrypt(suit.data); | ||
assert(Buffer.isBuffer(encrypted[i])); | ||
assert(encrypted[i].length > 0); | ||
}); | ||
it("`decrypt()` by node" + i, function () { | ||
decrypted[i] = key2.decrypt(encrypted[i], _.isArray(suit.encoding) ? suit.encoding[0] : suit.encoding); | ||
if (Buffer.isBuffer(decrypted[i])) { | ||
assert.equal(suit.data.toString('hex'), decrypted[i].toString('hex')); | ||
} else { | ||
assert(_.isEqual(suit.data, decrypted[i])); | ||
} | ||
}); | ||
})(i); | ||
} | ||
describe("Bad cases", function () { | ||
it("unsupported data types", function(){ | ||
assert.throw(function(){ generatedKeys[0].encrypt(null); }, Error, "Unexpected data type"); | ||
assert.throw(function(){ generatedKeys[0].encrypt(undefined); }, Error, "Unexpected data type"); | ||
assert.throw(function(){ generatedKeys[0].encrypt(true); }, Error, "Unexpected data type"); | ||
for (var i in dataBundle) { | ||
(function (i) { | ||
var key1 = null; | ||
var key2 = null; | ||
var suit = dataBundle[i]; | ||
it("`encryptPrivate()` by browser " + i, function () { | ||
var key = generatedKeys[Math.round(Math.random() * 1000) % generatedKeys.length].exportKey(); | ||
key1 = new NodeRSA(key, { | ||
environment: 'browser', | ||
encryptionScheme: 'pkcs1' | ||
}); | ||
key2 = new NodeRSA(key, { | ||
environment: 'node', | ||
encryptionScheme: 'pkcs1' | ||
}); | ||
encrypted[i] = key1.encryptPrivate(suit.data); | ||
assert(Buffer.isBuffer(encrypted[i])); | ||
assert(encrypted[i].length > 0); | ||
}); | ||
it("incorrect key for decrypting", function(){ | ||
var encrypted = generatedKeys[0].encrypt('data'); | ||
assert.throw(function(){ generatedKeys[1].decrypt(encrypted); }, Error, "Error during decryption"); | ||
it("`decryptPublic()` by node " + i, function () { | ||
decrypted[i] = key2.decryptPublic(encrypted[i], _.isArray(suit.encoding) ? suit.encoding[0] : suit.encoding); | ||
if (Buffer.isBuffer(decrypted[i])) { | ||
assert.equal(suit.data.toString('hex'), decrypted[i].toString('hex')); | ||
} else { | ||
assert(_.isEqual(suit.data, decrypted[i])); | ||
} | ||
}); | ||
})(i); | ||
} | ||
var encrypted = {}; | ||
var decrypted = {}; | ||
for (var i in dataBundle) { | ||
(function (i) { | ||
var key1 = null; | ||
var key2 = null; | ||
var suit = dataBundle[i]; | ||
it("`encrypt()` by node" + i, function () { | ||
var key = generatedKeys[Math.round(Math.random() * 1000) % generatedKeys.length].exportKey(); | ||
key1 = new NodeRSA(key, {environment: 'node'}); | ||
key2 = new NodeRSA(key, {environment: 'browser'}); | ||
encrypted[i] = key1.encrypt(suit.data); | ||
assert(Buffer.isBuffer(encrypted[i])); | ||
assert(encrypted[i].length > 0); | ||
}); | ||
it("`decrypt()` by browser" + i, function () { | ||
decrypted[i] = key2.decrypt(encrypted[i], _.isArray(suit.encoding) ? suit.encoding[0] : suit.encoding); | ||
if (Buffer.isBuffer(decrypted[i])) { | ||
assert.equal(suit.data.toString('hex'), decrypted[i].toString('hex')); | ||
} else { | ||
assert(_.isEqual(suit.data, decrypted[i])); | ||
} | ||
}); | ||
})(i); | ||
} | ||
for (var i in dataBundle) { | ||
(function (i) { | ||
var key1 = null; | ||
var key2 = null; | ||
var suit = dataBundle[i]; | ||
it("`encryptPrivate()` by node " + i, function () { | ||
var key = generatedKeys[Math.round(Math.random() * 1000) % generatedKeys.length].exportKey(); | ||
key1 = new NodeRSA(key, { | ||
environment: 'browser', | ||
encryptionScheme: 'pkcs1' | ||
}); | ||
key2 = new NodeRSA(key, { | ||
environment: 'node', | ||
encryptionScheme: 'pkcs1' | ||
}); | ||
encrypted[i] = key1.encryptPrivate(suit.data); | ||
assert(Buffer.isBuffer(encrypted[i])); | ||
assert(encrypted[i].length > 0); | ||
}); | ||
}); | ||
})(encryptSchemes[scheme_i]); | ||
} | ||
it("`decryptPublic()` by browser " + i, function () { | ||
decrypted[i] = key2.decryptPublic(encrypted[i], _.isArray(suit.encoding) ? suit.encoding[0] : suit.encoding); | ||
if (Buffer.isBuffer(decrypted[i])) { | ||
assert.equal(suit.data.toString('hex'), decrypted[i].toString('hex')); | ||
} else { | ||
assert(_.isEqual(suit.data, decrypted[i])); | ||
} | ||
}); | ||
})(i); | ||
} | ||
}); | ||
}); | ||
@@ -392,0 +561,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
166153
29
3695
295
+ Addedasn1@0.2.2(transitive)
+ Addedlodash@3.3.0(transitive)
- Removedasn1@0.2.6(transitive)
- Removedlodash@2.4.2(transitive)
- Removedsafer-buffer@2.1.2(transitive)
Updatedasn1@0.2.2
Updatedlodash@3.3.0