Skip to content

Commit

Permalink
Remove encryption migration
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 16, 2023
1 parent cea3d68 commit 80e1ff0
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 27 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"@types/node": "^18.11.18",
"@types/semver": "^7.3.13",
"ava": "^5.1.0",
"clear-module": "^4.1.2",
"del": "^7.0.0",
"del-cli": "^5.0.0",
"delay": "^5.0.0",
Expand Down
20 changes: 5 additions & 15 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,22 +357,12 @@ export default class Conf<T extends Record<string, any> = Record<string, unknown
return data.toString();
}

// Check if an initialization vector has been used to encrypt the data.
try {
// Check if an initialization vector has been used to encrypt the data
if (this.#encryptionKey) {
try {
if (data.slice(16, 17).toString() === ':') {
const initializationVector = data.slice(0, 16);
const password = crypto.pbkdf2Sync(this.#encryptionKey, initializationVector.toString(), 10_000, 32, 'sha512');
const decipher = crypto.createDecipheriv(encryptionAlgorithm, password, initializationVector);
data = Buffer.concat([decipher.update(Buffer.from(data.slice(17))), decipher.final()]).toString('utf8');
} else {
// TODO: Remove this in the next major version.
const decipher = crypto.createDecipher(encryptionAlgorithm, this.#encryptionKey);
data = Buffer.concat([decipher.update(Buffer.from(data)), decipher.final()]).toString('utf8');
}
} catch {}
}
const initializationVector = data.slice(0, 16);
const password = crypto.pbkdf2Sync(this.#encryptionKey, initializationVector.toString(), 10_000, 32, 'sha512');
const decipher = crypto.createDecipheriv(encryptionAlgorithm, password, initializationVector);
return Buffer.concat([decipher.update(Buffer.from(data.slice(17))), decipher.final()]).toString('utf8');
} catch {}

return data.toString();
Expand Down
11 changes: 0 additions & 11 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,17 +475,6 @@ test('encryption - corrupt file', t => {
t.is(after.get('foo'), undefined);
});

test('decryption - migration to initialization vector', t => {
// The `test/config-encrypted-with-conf-4-1-0.json` file contains `{"unicorn": "🦄"}` JSON data which is encrypted with [email protected] and password `abcd1234`
const config = new Conf({
cwd: 'test',
encryptionKey: 'abcd1234',
configName: 'config-encrypted-with-conf-4-1-0',
});

t.deepEqual(config.store, {unicorn: '🦄'});
});

test('onDidChange()', t => {
const {config} = t.context;

Expand Down

0 comments on commit 80e1ff0

Please sign in to comment.