renaming, some corrections
MODIFY
src/migration/tasks/MigrateCryptoSettingsFromConfigToDBTask.ts
src/migration/tasks/MigrateCryptoSettingsFromConfigToDBTask.ts
@@ -22,7 +22,7 @@ export default class MigrateCryptoSettingsFromConfigToDBTask extends MigrationTa
// Crypto Key from config file
const configCryptoKey: string = Configuration.getCryptoConfig().key;
// Crypto Key Properties from config file
- const confiCryptoKeyProperties: CryptoKeyProperties = Utils.separateCryptoAlgo(Configuration.getCryptoConfig().algorithm);
+ const configCryptoKeyProperties: CryptoKeyProperties = Utils.parseConfigCryptoAlgorithm(Configuration.getCryptoConfig().algorithm);
// Crypto Key Setting from db
const keySettings = await SettingStorage.getCryptoSettings(tenant.id);
// If no Crypto Key Setting exist, initialize them with Crypto Key from config file
@@ -33,7 +33,7 @@ export default class MigrateCryptoSettingsFromConfigToDBTask extends MigrationTa
type: CryptoSettingsType.CRYPTO,
crypto: {
key: configCryptoKey,
- keyProperties: confiCryptoKeyProperties,
+ keyProperties: configCryptoKeyProperties,
}
} as KeySetting;
await SettingStorage.saveCryptoSettings(tenant.id, keySettingToSave);

MODIFY
src/storage/mongodb/MongoDBStorage.ts
src/storage/mongodb/MongoDBStorage.ts
@@ -179,7 +179,7 @@ export default class MongoDBStorage {
type: CryptoSettingsType.CRYPTO,
crypto: {
key: Utils.generateKey(),
- keyProperties: Utils.getKeyProperties()
+ keyProperties: Utils.getDefaultKeyProperties()
}
} as KeySetting;
// Save Crypto Key Settings

MODIFY
src/utils/Cypher.ts
src/utils/Cypher.ts
import BackendError from '../exception/BackendError';
-import Configuration from './Configuration';
import Constants from './Constants';
-import CryptoConfiguration from '../types/configuration/CryptoConfiguration';
import { CryptoSetting } from '../types/Setting';
import SettingStorage from '../storage/mongodb/SettingStorage';
import Utils from './Utils';
@@ -14,7 +12,16 @@ const MODULE_NAME = 'Cypher';
export default class Cypher {
public static async getCryptoSetting(tenantID: string): Promise<CryptoSetting> {
- return (await SettingStorage.getCryptoSettings(tenantID)).crypto;
+ const cryptoSettings = (await SettingStorage.getCryptoSettings(tenantID)).crypto;
+ if (!cryptoSettings) {
+ throw new BackendError({
+ source: Constants.CENTRAL_SERVER,
+ module: MODULE_NAME,
+ method: 'getCryptoSetting',
+ message: 'No Crypto Settings found in the database'
+ });
+ }
+ return cryptoSettings;
}
public static encrypt(data: string, cryptoSetting: CryptoSetting): string {

MODIFY
src/utils/Utils.ts
src/utils/Utils.ts
@@ -1341,12 +1341,12 @@ export default class Utils {
return /^[A-Z0-9- ]*$/.test(plateID);
}
- public static separateCryptoAlgo(algo: string): CryptoKeyProperties {
- const regExp = /(.*?)-(.*?)-(.*)/.exec(algo);
+ public static parseConfigCryptoAlgorithm(algo: string): CryptoKeyProperties {
+ const [blockCypher, blockSize, operationMode] = algo.split('-');
return {
- blockCypher: regExp[1].toUpperCase(),
- blockSize: parseInt(regExp[2]),
- operationMode: regExp[3].toUpperCase()
+ blockCypher: blockCypher,
+ blockSize: parseInt(blockSize),
+ operationMode: operationMode
};
}
@@ -1359,7 +1359,7 @@ export default class Utils {
return crypto.randomBytes(16).toString('hex');
}
- public static getKeyProperties(): CryptoKeyProperties {
+ public static getDefaultKeyProperties(): CryptoKeyProperties {
return {
blockCypher: 'AES',
blockSize: 256,

renaming, some corrections