Skip to content

Commit

Permalink
fix: page breaking issue
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavanakarwade committed Dec 5, 2024
1 parent 8cc4aba commit aaa15c3
Showing 1 changed file with 18 additions and 70 deletions.
88 changes: 18 additions & 70 deletions src/api/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,86 +226,34 @@ 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}`

// 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}`;
const CRYPTO_PRIVATE_KEY: string = `${envConfig.PUBLIC_CRYPTO_PRIVATE_KEY}`

try {
// 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
if (typeof (value) !== 'string') {
value = JSON.stringify(value)
}
return CryptoJS.AES.encrypt(value, CRYPTO_PRIVATE_KEY).toString();
} catch (error) {
console.error('Decryption error:', error); // Log the error for debugging
return ''; // Handle decryption error by returning an empty string
// Handle encryption error
console.error('Encryption error:', error);
return '';
}
};
}


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

try {
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),
});
let bytes = CryptoJS.AES.decrypt(value, CRYPTO_PRIVATE_KEY);
return bytes.toString(CryptoJS.enc.Utf8);
} catch (error) {
return ''; // Handle encryption error by returning an empty string
// Handle decryption error or invalid input
console.error('Decryption error:', error);
return '';
}
};
}

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

0 comments on commit aaa15c3

Please sign in to comment.