chanced to useFormerKey name for boolean
MODIFY
src/utils/Cypher.ts
src/utils/Cypher.ts
@@ -14,24 +14,24 @@ const MODULE_NAME = 'Cypher';
export default class Cypher {
- public static async encrypt(tenantID: string, data: string, former = false): Promise<string> {
+ public static async encrypt(tenantID: string, data: string, useFormerKey = false): Promise<string> {
const iv = crypto.randomBytes(IV_LENGTH);
const cryptoSetting = await Cypher.getCryptoSetting(tenantID);
- const algo = former ? Utils.buildAlgorithm(cryptoSetting.formerKeyProperties) : Utils.buildAlgorithm(cryptoSetting.keyProperties);
- const key = former ? Buffer.from(cryptoSetting.formerKey) : Buffer.from(cryptoSetting.key);
+ const algo = useFormerKey ? Utils.buildAlgorithm(cryptoSetting.formerKeyProperties) : Utils.buildAlgorithm(cryptoSetting.keyProperties);
+ const key = useFormerKey ? Buffer.from(cryptoSetting.formerKey) : Buffer.from(cryptoSetting.key);
const cipher = crypto.createCipheriv(algo, key, iv);
let encryptedData = cipher.update(data);
encryptedData = Buffer.concat([encryptedData, cipher.final()]);
return iv.toString('hex') + ':' + encryptedData.toString('hex');
}
- public static async decrypt(tenantID: string, data: string, former = false): Promise<string> {
+ public static async decrypt(tenantID: string, data: string, useFormerKey = false): Promise<string> {
const dataParts = data.split(':');
const iv = Buffer.from(dataParts.shift(), 'hex');
const encryptedData = Buffer.from(dataParts.join(':'), 'hex');
const cryptoSetting = await Cypher.getCryptoSetting(tenantID);
- const algo = former ? Utils.buildAlgorithm(cryptoSetting.formerKeyProperties) : Utils.buildAlgorithm(cryptoSetting.keyProperties);
- const key = former ? Buffer.from(cryptoSetting.formerKey) : Buffer.from(cryptoSetting.key);
+ const algo = useFormerKey ? Utils.buildAlgorithm(cryptoSetting.formerKeyProperties) : Utils.buildAlgorithm(cryptoSetting.keyProperties);
+ const key = useFormerKey ? Buffer.from(cryptoSetting.formerKey) : Buffer.from(cryptoSetting.key);
const decipher = crypto.createDecipheriv(algo, key , iv);
let decrypted = decipher.update(encryptedData);
decrypted = Buffer.concat([decrypted, decipher.final()]);
@@ -42,7 +42,7 @@ export default class Cypher {
return crypto.createHash('sha256').update(data).digest('hex');
}
- public static async encryptSensitiveDataInJSON(tenantID: string, obj: Record<string, any>, former = false): Promise<void> {
+ public static async encryptSensitiveDataInJSON(tenantID: string, obj: Record<string, any>, useFormerKey = false): Promise<void> {
if (typeof obj !== 'object') {
throw new BackendError({
source: Constants.CENTRAL_SERVER,
@@ -67,7 +67,7 @@ export default class Cypher {
const value = _.get(obj, property);
// If the value is undefined, null or empty then do nothing and skip to the next property
if (value && value.length > 0) {
- _.set(obj, property, await Cypher.encrypt(tenantID, value, former));
+ _.set(obj, property, await Cypher.encrypt(tenantID, value, useFormerKey));
}
}
}
@@ -76,38 +76,37 @@ export default class Cypher {
}
}
- // Not used!!!
- // public static async decryptSensitiveDataInJSON(tenantID: string, obj: Record<string, any>): Promise<void> {
- // if (typeof obj !== 'object') {
- // throw new BackendError({
- // source: Constants.CENTRAL_SERVER,
- // module: MODULE_NAME,
- // method: 'decryptSensitiveDataInJSON',
- // message: `The parameter ${obj} is not an object`
- // });
- // }
- // if ('sensitiveData' in obj) {
- // // Check that sensitive data is an array
- // if (!Array.isArray(obj.sensitiveData)) {
- // throw new BackendError({
- // source: Constants.CENTRAL_SERVER,
- // module: MODULE_NAME,
- // method: 'decryptSensitiveDataInJSON',
- // message: 'The property \'sensitiveData\' is not an array'
- // });
- // }
- // for (const property of obj.sensitiveData as string[]) {
- // // Check that the property does exist otherwise skip to the next property
- // if (_.has(obj, property)) {
- // const value = _.get(obj, property);
- // // If the value is undefined, null or empty then do nothing and skip to the next property
- // if (value && value.length > 0) {
- // _.set(obj, property, await Cypher.decrypt(tenantID, value));
- // }
- // }
- // }
- // }
- // }
+ public static async decryptSensitiveDataInJSON(tenantID: string, obj: Record<string, any>, useFormerKey = false): Promise<void> {
+ if (typeof obj !== 'object') {
+ throw new BackendError({
+ source: Constants.CENTRAL_SERVER,
+ module: MODULE_NAME,
+ method: 'decryptSensitiveDataInJSON',
+ message: `The parameter ${obj} is not an object`
+ });
+ }
+ if ('sensitiveData' in obj) {
+ // Check that sensitive data is an array
+ if (!Array.isArray(obj.sensitiveData)) {
+ throw new BackendError({
+ source: Constants.CENTRAL_SERVER,
+ module: MODULE_NAME,
+ method: 'decryptSensitiveDataInJSON',
+ message: 'The property \'sensitiveData\' is not an array'
+ });
+ }
+ for (const property of obj.sensitiveData as string[]) {
+ // Check that the property does exist otherwise skip to the next property
+ if (_.has(obj, property)) {
+ const value = _.get(obj, property);
+ // If the value is undefined, null or empty then do nothing and skip to the next property
+ if (value && value.length > 0) {
+ _.set(obj, property, await Cypher.decrypt(tenantID, value, useFormerKey));
+ }
+ }
+ }
+ }
+ }
public static hashSensitiveDataInJSON(obj: Record<string, any>): void {
if (typeof obj !== 'object') {
@@ -150,7 +149,7 @@ export default class Cypher {
await Cypher.cleanupFormerSensitiveData(tenantID);
const keySettings = await SettingStorage.getCryptoSettings(tenantID);
keySettings.crypto.migrationToBeDone = false;
- await SettingStorage.saveCryptoSettings(tenantID, keySettings);
+ await Cypher.saveCryptoSetting(tenantID, keySettings);
} catch (err) {
throw new BackendError({
source: Constants.CENTRAL_SERVER,
@@ -195,9 +194,9 @@ export default class Cypher {
formerSensitiveData.push(Cypher.hash(cryptoSetting.formerKey));
setting.formerSensitiveData = formerSensitiveData;
// Decrypt sensitive data with former key and key properties
- await Cypher.decryptSensitiveDataInJSON(setting, tenantID, true);
+ await Cypher.decryptSensitiveDataInJSON(tenantID,setting, true);
// Encrypt sensitive data with new key and key properties
- await Cypher.encryptSensitiveDataInJSON(setting, tenantID);
+ await Cypher.encryptSensitiveDataInJSON(tenantID, setting);
// Save setting with sensitive data encrypted with new key
await SettingStorage.saveSettings(tenantID, setting);
}
@@ -219,19 +218,6 @@ export default class Cypher {
}
}
- private static async getCryptoSetting(tenantID: string): Promise<CryptoSetting> {
- const cryptoSettings = await SettingStorage.getCryptoSettings(tenantID);
- if (!cryptoSettings || !cryptoSettings.crypto) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- module: MODULE_NAME,
- method: 'getCryptoSetting',
- message: `Tenant ID '${tenantID}' does not have crypto settings.`
- });
- }
- return cryptoSettings.crypto;
- }
-
public static async saveCryptoSetting(tenantID: string, cryptoSettingToSave: CryptoKeySetting): Promise<void> {
// Build internal structure
const settingsToSave = {
@@ -246,6 +232,19 @@ export default class Cypher {
await SettingStorage.saveSettings(tenantID, settingsToSave);
}
+ private static async getCryptoSetting(tenantID: string): Promise<CryptoSetting> {
+ const cryptoSettings = await SettingStorage.getCryptoSettings(tenantID);
+ if (!cryptoSettings || !cryptoSettings.crypto) {
+ throw new BackendError({
+ source: Constants.CENTRAL_SERVER,
+ module: MODULE_NAME,
+ method: 'getCryptoSetting',
+ message: `Tenant ID '${tenantID}' does not have crypto settings.`
+ });
+ }
+ return cryptoSettings.crypto;
+ }
+
private static prepareFormerSenitiveData(setting: SettingDB): string[] {
const formerSensitiveData: string[] = [];
for (const property of setting.sensitiveData) {

chanced to useFormerKey name for boolean