Open erosman opened 2 years ago
The import feature is something that we're also interested in. Any chance someone from the CryptoJS team can help us how to do it?
@erosman can you please help out how to import crypto js in chrome extension. I have raised an issue #436 regarding it and got stuck at importing cryptojs. extension says unknown error without any information.
please refer to https://github.com/brix/crypto-js/issues/436#issuecomment-1298019073 where I mentioned what I have done. Any suggestions would be grateful.
Thank you very much.
@mai1x9 It is easy enough to import a complied version of CryptoJS but the issues are:
What I have done for my own use-case is:
export {CryptoJS};
and left a note explaining the reason in supporting txt e.g.
export {CryptoJS};
/*
CryptoJS v3.1.2
code.google.com/p/crypto-js
(c) 2009-2013 by Jeff Mott. All rights reserved.
code.google.com/p/crypto-js/wiki/License
*/
var CryptoJS=CryptoJS||function(u,p){var d={},l=d.lib={},s=function(){},t=l.Base={extend:.............
import {CryptoJS} from '../lib/aes.3.1.2.js';
If you want to see the actual code, here it is:
https://github.com/foxyproxy/browser-extension/tree/main/src/lib https://github.com/foxyproxy/browser-extension/blob/main/src/content/migrate.js
Just in case anyone is interested. I only needed CryptoJS for a SHA hash function to verify a signature. And since I couldn't make it with module import, I had to find another SHA library. In my case I found and used js-sha512
import "./vendors/jsencrypt.js?v=1.1.2.0";
import "./vendors/sha512.js?v=1.1.2.0";
export function verify(publicKey, content, signature) {
try {
const jsEncrypt = new JSEncrypt();
jsEncrypt.setPublicKey(publicKey);
const verified = jsEncrypt.verify(content, signature, sha512);
if (verified) {
return true;
}
} catch (error) {
console.error(error);
}
return false;
}
@mai1x9 It is easy enough to import a complied version of CryptoJS but the issues are:
- Using a pre-complied one
- The library is slightly altered which causes a checksum mismatch (as required for libraries in Firefox addons)
What I have done for my own use-case is:
Got a pre-complied library .e.g.
- Added
export {CryptoJS};
and left a note explaining the reason in supporting txt e.g.export {CryptoJS}; /* CryptoJS v3.1.2 code.google.com/p/crypto-js (c) 2009-2013 by Jeff Mott. All rights reserved. code.google.com/p/crypto-js/wiki/License */ var CryptoJS=CryptoJS||function(u,p){var d={},l=d.lib={},s=function(){},t=l.Base={extend:.............
- CryptoJS then can be imported as a module
import {CryptoJS} from '../lib/aes.3.1.2.js';
If you want to see the actual code, here it is:
https://github.com/foxyproxy/browser-extension/tree/main/src/lib https://github.com/foxyproxy/browser-extension/blob/main/src/content/migrate.js
@erosman thanks for the help, it has indeed work, got fair idea on how use crypto-js in browser extension. Would like to mention an alternative which is subtlecrypto which is inbuild in browsers and can be accessed via window.crypto...
@mai1x9 Indeed but in my situation, the data was encrypted with CryptoJS so it had to be decrypted with CryptoJS.
There are some data that have been encrypted using CryptoJS v3.1.2 in a Chrome extension, that need to be decrypted once (no encrypting) in an upgrade.
The decryption process is: