how to get cryptojs to work with out “rollup” files

匆匆过客 提交于 2019-12-11 06:48:34

问题


I'm trying to upgrade the old version of cryptoJS that can be found here to a newer version on github that can be found here because, there are some functions on the old version that is going to be deprecated. Unfortunately, the newer don't have rollup files, so im trying to get it to work using the core files but, I keep getting the error(s) that something is "undefined" like "cfg.hasher is undefined".

<script type="text/javascript" src="crypto-js3.1.9-1/core.js"></script>
<script type="text/javascript" src="crypto-js3.1.9-1/evpkdf.js"></script>
<script type="text/javascript" src="crypto-js3.1.9-1/x64-core.js"></script>
<script type="text/javascript" src="crypto-js3.1.9-1/cipher-core.js"></script>
<script type="text/javascript" src="crypto-js3.1.9-1/aes.js"></script>
<script type="text/javascript" src="crypto-js3.1.9-1/pbkdf2.js"></script>
<script type="text/javascript" src="crypto-js3.1.9-1/hmac.js"></script>
<script type="text/javascript" src="crypto-js3.1.9-1/lib-typedarrays.js"></script>
<script type="text/javascript" src="crypto-js3.1.9-1/format-hex.js"></script>

I tried adding all the core files and even changing the orders of them, but I think I'm still missing something.

If I use the rollup files, this simple code below works just fine on it, but it fail on the newer version. Hopefully someone with more experience with cryptoJS can help me out.

<script type="text/javascript">
    var string = "asdfasdfsadfdsa";
    var key = "asdfasfasfs";
    var encrypted = CryptoJS.AES.encrypt(string, key);
    var decrypted = CryptoJS.AES.decrypt(encrypted, key);
    alert(decrypted);
</script>

UPDATE!! I can get SHA256 to work just fine with out uses of require.js so I don't thing the problem is Modular include and the lib should be able to run "without RequireJS", but I'm still running into encryption and decryption error: "cfg.hasher is undefined".

<script type="text/javascript" src="CryptoJS/components/core.js"></script>
<script type="text/javascript" src="CryptoJS/components/sha256.js"></script>
<script type="text/javascript">
    console.log(CryptoJS.SHA256("Message"));
</script>

回答1:


You would have to do modular includes

Modular include using require js:

require.config({
    packages: [
        {
            name: 'crypto-js',
            location: 'path-to/bower_components/crypto-js',
            main: 'index'
        }
    ]
});

require(["crypto-js/aes", "crypto-js/sha256"], function (AES, SHA256) {
    console.log(SHA256("Message"));
});

Here is a full gist



来源:https://stackoverflow.com/questions/42523928/how-to-get-cryptojs-to-work-with-out-rollup-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!