Skip to content

Commit

Permalink
Merge pull request #33 from simomat/custom-iv-size added iv size par…
Browse files Browse the repository at this point in the history
…ameter # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Wed Sep 16 12:21:50 2020 +0300 # # On branch master # Your branch is up to date with 'origin/master'. # # Changes to be committed: # modified: .gitignore # modified: lib/constants.js # modified: lib/crypt.js # modified: src/constants.js # modified: src/crypt.js # modified: test/test.js # modified: web/hybrid-crypto.js # modified: web/hybrid-crypto.min.js # # Changes not staged for commit: # modified: package.json #
  • Loading branch information
juhoen committed Sep 16, 2020
2 parents c1c916b + 92f62f9 commit 3511f82
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ typings/
# dotenv environment variables file
.env

# IDE files
.idea
*.iml

3 changes: 2 additions & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ module.exports = {
AES_STANDARD: 'AES-CBC',
RSA_STANDARD: 'RSA-OAEP',
DEFAULT_MESSAGE_DIGEST: 'sha256',
DEFAULT_AES_KEY_SIZE: 256
DEFAULT_AES_KEY_SIZE: 256,
DEFAULT_AES_IV_SIZE: 32
};
4 changes: 3 additions & 1 deletion lib/crypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var pki = forge.pki,
var _require = require('./constants'),
DEFAULT_MESSAGE_DIGEST = _require.DEFAULT_MESSAGE_DIGEST,
DEFAULT_AES_KEY_SIZE = _require.DEFAULT_AES_KEY_SIZE,
DEFAULT_AES_IV_SIZE = _require.DEFAULT_AES_IV_SIZE,
AES_STANDARD = _require.AES_STANDARD,
RSA_STANDARD = _require.RSA_STANDARD;

Expand All @@ -36,6 +37,7 @@ function () {
this.options = _objectSpread({
md: DEFAULT_MESSAGE_DIGEST,
aesKeySize: DEFAULT_AES_KEY_SIZE,
aesIvSize: DEFAULT_AES_IV_SIZE,
aesStandard: AES_STANDARD,
rsaStandard: RSA_STANDARD,
entropy: undefined
Expand Down Expand Up @@ -206,7 +208,7 @@ function () {
return typeof key === 'string' ? pki.publicKeyFromPem(key) : key;
}); // Generate random keys

var iv = forge.random.getBytesSync(32);
var iv = forge.random.getBytesSync(this.options.aesIvSize);
var key = forge.random.getBytesSync(this.options.aesKeySize / 8); // Encrypt random key with all of the public keys

var encryptedKeys = {};
Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ module.exports = {
RSA_STANDARD: 'RSA-OAEP',
DEFAULT_MESSAGE_DIGEST: 'sha256',
DEFAULT_AES_KEY_SIZE: 256,
DEFAULT_AES_IV_SIZE: 32
};
5 changes: 4 additions & 1 deletion src/crypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ const { pki, rsa } = forge;
const {
DEFAULT_MESSAGE_DIGEST,
DEFAULT_AES_KEY_SIZE,
DEFAULT_AES_IV_SIZE,
AES_STANDARD,
RSA_STANDARD,
} = require('./constants');

type CryptProps = {
md: string,
aesKeySize: number,
aesIvSize: number,
entropy?: string | number,
aesStandard: string,
rsaStandard: string,
Expand All @@ -24,6 +26,7 @@ class Crypt {
this.options = {
md: DEFAULT_MESSAGE_DIGEST,
aesKeySize: DEFAULT_AES_KEY_SIZE,
aesIvSize: DEFAULT_AES_IV_SIZE,
aesStandard: AES_STANDARD,
rsaStandard: RSA_STANDARD,
entropy: undefined,
Expand Down Expand Up @@ -198,7 +201,7 @@ class Crypt {
): Array<Object>);

// Generate random keys
const iv = forge.random.getBytesSync(32);
const iv = forge.random.getBytesSync(this.options.aesIvSize);
const key = forge.random.getBytesSync(this.options.aesKeySize / 8);

// Encrypt random key with all of the public keys
Expand Down
25 changes: 25 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,31 @@ describe('Crypt', function() {
});
});
});

it('should encrypt with a default IV size of 32', function() {
var message = 'Hello world!';

var encrypted = crypt.encrypt(publicKey, message);
var iv = Buffer.from(JSON.parse(encrypted).iv, 'base64');

assert.equal(iv.length, 32);
})

it('should encrypt with custom IV size', function () {
var crypt = new Crypt({
aesIvSize: 16
});

var message = 'Hello world!';

var encrypted = crypt.encrypt(publicKey, message);
var decrypted = crypt.decrypt(privateKey, encrypted).message;
var iv = Buffer.from(JSON.parse(encrypted).iv, 'base64');

assert.equal(iv.length, 16);
assert.notEqual(encrypted, message);
assert.equal(decrypted, message);
});
});

describe('Helpers', function() {
Expand Down
7 changes: 5 additions & 2 deletions web/hybrid-crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module.exports = {
AES_STANDARD: 'AES-CBC',
RSA_STANDARD: 'RSA-OAEP',
DEFAULT_MESSAGE_DIGEST: 'sha256',
DEFAULT_AES_KEY_SIZE: 256
DEFAULT_AES_KEY_SIZE: 256,
DEFAULT_AES_IV_SIZE: 32
};
},{}],2:[function(require,module,exports){
"use strict";
Expand All @@ -32,6 +33,7 @@ var pki = forge.pki,
var _require = require('./constants'),
DEFAULT_MESSAGE_DIGEST = _require.DEFAULT_MESSAGE_DIGEST,
DEFAULT_AES_KEY_SIZE = _require.DEFAULT_AES_KEY_SIZE,
DEFAULT_AES_IV_SIZE = _require.DEFAULT_AES_IV_SIZE,
AES_STANDARD = _require.AES_STANDARD,
RSA_STANDARD = _require.RSA_STANDARD;

Expand All @@ -46,6 +48,7 @@ function () {
this.options = _objectSpread({
md: DEFAULT_MESSAGE_DIGEST,
aesKeySize: DEFAULT_AES_KEY_SIZE,
aesIvSize: DEFAULT_AES_IV_SIZE,
aesStandard: AES_STANDARD,
rsaStandard: RSA_STANDARD,
entropy: undefined
Expand Down Expand Up @@ -216,7 +219,7 @@ function () {
return typeof key === 'string' ? pki.publicKeyFromPem(key) : key;
}); // Generate random keys

var iv = forge.random.getBytesSync(32);
var iv = forge.random.getBytesSync(this.options.aesIvSize);
var key = forge.random.getBytesSync(this.options.aesKeySize / 8); // Encrypt random key with all of the public keys

var encryptedKeys = {};
Expand Down
2 changes: 1 addition & 1 deletion web/hybrid-crypto.min.js

Large diffs are not rendered by default.

0 comments on commit 3511f82

Please sign in to comment.