ecency-mobile/src/utils/crypto.js

65 lines
1.9 KiB
JavaScript
Raw Normal View History

import CryptoJS from 'crypto-js';
2021-03-20 15:53:42 +03:00
const STAMP = '995a06d5-ee54-407f-bb8e-e4af2ab2fe01';
export const encryptKey = (data, key) => {
console.log('encrypting: ', data, key);
2021-03-20 15:53:42 +03:00
const stampedData = getStampedData(data);
const encJson = CryptoJS.AES.encrypt(JSON.stringify(stampedData), key).toString();
let encData = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(encJson));
console.log('returning: ', encData);
return encData;
};
export const decryptKey = (data, key, onError) => {
const legacyDecrypt = () => decryptKeyLegacy(data, key, onError);
try {
const response = decryptKeyNew(data, key);
2021-03-19 23:51:36 +03:00
return response;
} catch (err) {
console.warn('decryption with new method failed, trying legacy', err);
return legacyDecrypt();
}
};
const decryptKeyNew = (data, key) => {
console.log('decrypting new: ', data, key);
let decData = CryptoJS.enc.Base64.parse(data).toString(CryptoJS.enc.Utf8);
let bytes = CryptoJS.AES.decrypt(decData, key).toString(CryptoJS.enc.Utf8);
2021-03-20 15:53:42 +03:00
const stampedData = JSON.parse(bytes);
const ret = processStampedData(stampedData);
console.log('returning: ', ret);
return ret;
};
const decryptKeyLegacy = (data, key, onError) => {
try {
console.log('decrypting legacy ', data, key);
const ret = CryptoJS.AES.decrypt(data, key).toString(CryptoJS.enc.Utf8);
console.log('returning: ', ret);
return ret;
} catch (err) {
console.warn('decryption with legacy failed as well');
if (onError) {
onError(err);
}
}
2020-02-20 06:31:13 +03:00
};
2021-03-20 15:53:42 +03:00
// stamping mechanism will help distinguish old legacy data and new encrypted data
// second purpose is to avoid necrypting empty strings
const getStampedData = (data) => {
return {
data,
stamp: STAMP,
};
};
const processStampedData = (stampedData) => {
if (stampedData.hasOwnProperty('stamp') && stampedData.stamp == STAMP) {
return stampedData.data;
}
throw new Error('Possibly un-stamped legacy data');
};