Skip to content

Commit

Permalink
page breaking issue
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavanakarwade committed Dec 5, 2024
1 parent 952e0ab commit 8cc4aba
Showing 1 changed file with 70 additions and 18 deletions.
88 changes: 70 additions & 18 deletions src/api/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,34 +226,86 @@ export const passwordEncryption = (password: string): string => {
return encryptedPassword
}

export const encryptData = (value: any): string => {
// export const encryptData = (value: any): string => {

const CRYPTO_PRIVATE_KEY: string = `${envConfig.PUBLIC_CRYPTO_PRIVATE_KEY}`
// const CRYPTO_PRIVATE_KEY: string = `${envConfig.PUBLIC_CRYPTO_PRIVATE_KEY}`

// try {
// if (typeof (value) !== 'string') {
// value = JSON.stringify(value)
// }
// return CryptoJS.AES.encrypt(value, CRYPTO_PRIVATE_KEY).toString();
// } catch (error) {
// // Handle encryption error
// console.error('Encryption error:', error);
// return '';
// }
// }

// export const decryptData = (value: any): string => {
// const CRYPTO_PRIVATE_KEY: string = `${envConfig.PUBLIC_CRYPTO_PRIVATE_KEY}`

// try {
// let bytes = CryptoJS.AES.decrypt(value, CRYPTO_PRIVATE_KEY);
// return bytes.toString(CryptoJS.enc.Utf8);
// } catch (error) {
// // Handle decryption error or invalid input
// console.error('Decryption error:', error);
// return '';
// }
// }

export const decryptData = (value: string, isUserProfile?: boolean): string => {
const CRYPTO_PRIVATE_KEY: string = `${envConfig.PUBLIC_CRYPTO_PRIVATE_KEY}`;

try {
if (typeof (value) !== 'string') {
value = JSON.stringify(value)
}
return CryptoJS.AES.encrypt(value, CRYPTO_PRIVATE_KEY).toString();
// Parse the encrypted value to extract ciphertext and IV
const { ciphertext, iv } = JSON.parse(value);

// Create a CipherParams object
const cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Base64.parse(ciphertext),
});

const decrypted = CryptoJS.AES.decrypt(
cipherParams,
CryptoJS.enc.Utf8.parse(CRYPTO_PRIVATE_KEY),
{
iv: CryptoJS.enc.Base64.parse(iv),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
}
);

return decrypted.toString(CryptoJS.enc.Utf8); // Return the decrypted value
} catch (error) {
// Handle encryption error
console.error('Encryption error:', error);
return '';
console.error('Decryption error:', error); // Log the error for debugging
return ''; // Handle decryption error by returning an empty string
}
}
};

export const decryptData = (value: any): string => {
const CRYPTO_PRIVATE_KEY: string = `${envConfig.PUBLIC_CRYPTO_PRIVATE_KEY}`

export const encryptData = (value: any): string => {
const CRYPTO_PRIVATE_KEY: string = `${envConfig.PUBLIC_CRYPTO_PRIVATE_KEY}`;

try {
let bytes = CryptoJS.AES.decrypt(value, CRYPTO_PRIVATE_KEY);
return bytes.toString(CryptoJS.enc.Utf8);
value = JSON.stringify(value);
const iv = CryptoJS.lib.WordArray.random(16); // Generate a random IV
const encrypted = CryptoJS.AES.encrypt(value, CryptoJS.enc.Utf8.parse(CRYPTO_PRIVATE_KEY), {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
});

// Return the encrypted value along with the IV
return JSON.stringify({
ciphertext: encrypted.ciphertext.toString(CryptoJS.enc.Base64),
iv: iv.toString(CryptoJS.enc.Base64),
});
} catch (error) {
// Handle decryption error or invalid input
console.error('Decryption error:', error);
return '';
return ''; // Handle encryption error by returning an empty string
}
}
};

export const setToLocalStorage = async (key: string, value: any) =>{
// If passed value is object then checked empty object
Expand Down

0 comments on commit 8cc4aba

Please sign in to comment.