简体   繁体   English

在流星中使用Crypto.js

[英]Using Crypto.js in meteor

I am trying to include some crypto.js libraries in a meteor js app (meteor version 0.6.4.1). 我正在尝试在流星js应用程序(流星版本0.6.4.1)中包含一些crypto.js库。

When I copy and paste the contents into the server/main.js file it works fine, but this makes it very un-readable. 当我将内容复制并粘贴到server / main.js文件中时,它可以很好地工作,但这使它非常不可读。

When I put the libraries in separate files in the server directory (I also tried placing them in the lib directory) I get the error ReferenceError: CryptoJS is not defined . 当我将库放在服务器目录中的单独文件中(我也尝试将它们放在lib目录中)时,出现错误ReferenceError: CryptoJS is not defined

gist of server/main.js: server / main.js的要点:

Meteor.methods({
    encrypt:function(bundleID){
        return CryptoJS.HmacSHA256(string, 'something');
    }
});

I also tried changing the first line of the hmac-sha256.js file from 我还尝试过将hmac-sha256.js文件的第一行从

var CryptoJS=CryptoJS||function(h,s){...

to: 至:

CryptoJS=CryptoJS||function(h,s){...

to make it global, but this also did not work. 使其全球化,但这也行不通。 How do I include this library properly? 如何正确包含此库?

In your application dir create folder 'packages/cryptojs' and put there files: 在您的应用程序目录中,创建文件夹“ packages / cryptojs”,然后在其中放置文件:

  • hmac-sha256.js HMAC-sha256.js
  • enc-base64-min.js ENC-的base64 min.js
  • package.js package.js

packages / cryptojs / package.js : 包/ cryptojs / package.js

Package.describe({
  summary: "CryptoJS"
});

Package.on_use(function (api, where) {
  api.add_files(['hmac-sha256.js'], ['client','server']);
  api.add_files(['enc-base64-min.js'], ['client','server']);
});

You need to modify hmac-sha256.js by changing beginning of line 7 from: 您需要通过将第7行的开头更改为来修改hmac-sha256.js

var CryptoJS=CryptoJS||function(h,s){

to: 至:

CryptoJS=function(h,s){

After that, you can use it: 之后,您可以使用它:

var hash = CryptoJS.HmacSHA256("Message", "secret");
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);    
console.log(hashInBase64)

Example source 来源示例

I followed as parhelium's guide, but still not work, then I found new way to fix this issue: Just replace line 7 in hmac-sha512.js to this one to globalize CryptoJS object: 我按照parhelium的指南进行操作,但仍然无法正常工作,然后我找到了解决此问题的新方法:只需将hmac-sha512.js中的第7行替换为该行即可全球化CryptoJS对象:

this.CryptoJS=this.CryptoJS

Fix the same for other cryptojs. 修复其他cryptojs的相同问题。 To use with Base64, you must make sure the base64.js is loaded after other libraries. 要与Base64一起使用,必须确保在其他库之后加载了base64.js。

The reason this is happening is due to the variable scoping in meteor. 发生这种情况的原因是流星的作用域可变。 Try putting the cryptojs library files in /server/compatibility . 尝试将cryptojs库文件放在/server/compatibility This way the cryptojs library can be accessed in other files. 这样,可以在其他文件中访问cryptojs库。

You could also get it working without putting it in /compatibility by removing the var used in the cryptojs source files. 您也可以通过删除cryptojs源文件中使用的var来使其工作而无需将其放在/compatibility中。 The thing is if you do this its harder to keep the files up to date. 问题是,如果执行此操作,则很难使文件保持最新状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM