brix / crypto-js

JavaScript library of crypto standards.
Other
15.88k stars 2.39k forks source link

Importing CryptoJS as JavaScript module #434

Open erosman opened 2 years ago

erosman commented 2 years ago

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:

CryptoJS.AES.decrypt(item, key).toString(CryptoJS.enc.Utf8)
stsrki commented 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?

mai1x9 commented 2 years ago

@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.

erosman commented 2 years ago

@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:

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

stsrki commented 2 years ago

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 commented 2 years ago

@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:

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...

erosman commented 2 years ago

@mai1x9 Indeed but in my situation, the data was encrypted with CryptoJS so it had to be decrypted with CryptoJS.

entronad commented 1 year ago

Maybe you could try CryptoES, it is originally written in ES6 module, and with same code with CryptoJS inside.