ReferenceError:在rails javascript中未定义cryptoJS

时间:2018-04-03 13:29:59

标签: javascript ruby-on-rails cryptojs

我正在尝试实现此代码以使我的付款方式之一工作,但每次都返回错误"异常发生encryptionString:ReferenceError:cryptoJS未定义"

错误是由于代码" var key = cryptoJS.enc.Hex.parse(keyValue);"

我在rails中运行它,所以请帮助告知代码是否有任何问题。

的Gemfile

gem 'cryptojs-rails', '~> 2.5', '>= 2.5.3'

应用/资产/ Javascript角/ application.js中

//= require crypto

应用/资产/ Javascript角/ maybank.js

var getEncryptionString = function (arrayM2U, envType) {    
try {
    var ITERATIONS = 2;
    var salt='Maybank2u simple encryption';
    var keyValue='000102030405060708090a0b0c0d0e0f';

    var amount = arrayM2U.amount;
    var accountNumber =arrayM2U.accountNumber;
    var payeeCode = arrayM2U.payeeCode;
    var refNum = arrayM2U.refNumber;
    var redirectionurl = arrayM2U.callbackUrl;
    var m_sReqSendString = "";

    if( (accountNumber == undefined || accountNumber == "") && (refNum != undefined &&  refNum != ""))
     m_sReqSendString = 'Login$' +payeeCode+ '$1$' + amount + '$1$' + refNum + '$$$' + redirectionurl;
    else if((accountNumber != undefined && accountNumber != "" ) && (refNum == undefined ||  refNum == ""))
     m_sReqSendString = 'Login$' + payeeCode + '$1$' + amount + '$$$1$' + accountNumber + '$' + redirectionurl;
    else
    m_sReqSendString = 'Login$' + payeeCode + '$1$' + amount + '$1$'+refNum+'$1$' + accountNumber + '$' +redirectionurl ;

    console.log(keyValue);
    var key = cryptoJS.enc.Hex.parse(keyValue);
    console.log(keyValue);
    var valueToEnc = null;
    var eValue = m_sReqSendString;
    for (var i = 0; i < ITERATIONS; i++) {
        valueToEnc = salt + eValue;
        var encrypted = cryptoJS.AES.encrypt(valueToEnc, key, {
            mode: cryptoJS.mode.ECB,
            padding: cryptoJS.pad.Pkcs7
        });
        eValue = encrypted;
    }

    var fullEncryptedString = encodeURIComponent(eValue);

    switch (envType) {
      case 1:
          actionUrl = "https://m2upayuat.maybank2u.com.my/testM2uPayment";
          break;
      case 2:
          actionUrl = "https://www.maybank2u.com.my/mbb/m2u/m9006_enc/m2uMerchantLogin.do";
          break;
      default:
          actionUrl = "https://api.maybanksandbox.com/v1.0/testM2uPayment";
    }

    var json = {};

    json.encryptedString = fullEncryptedString;

    json.actionUrl = actionUrl;

        return json;

} catch (err) {
    console.log("Exception occured encryptionString:"+err)
      return "FAIL"

}

}

1 个答案:

答案 0 :(得分:0)

好吧,我刚看了一下gem源代码,它很快。你没有机会让这个宝石工作。我想这是作者的测试,但他一定不能快速放弃它(这很可能,鉴于您已经删除了github存储库,并且只有该gem的一个版本)。

2.5.3 cryptojs库应包含所有这些文件:

⇒  tree tags/2.5.3/src

├── AES.js
├── BlockModes.js
├── Crypto.js
├── CryptoMath.js
├── DES.js
├── HMAC.js
├── MARC4.js
├── MD5.js
├── PBKDF2.js
├── PBKDF2Async.js
├── Rabbit.js
├── SHA1.js
└── SHA256.js

0 directories, 13 files

你使用的gem只包含gem和Crypto.js所需的基本文件,此时大部分都是空shell,这就是你的代码无效的原因。

⇒  tree gems/ruby-2.3.3/gems/cryptojs-rails-2.5.3/

├── LICENSE
├── README.md
├── app
│   └── assets
│       └── javascripts
│           └── crypto.js
└── lib
    ├── cryptojs-rails
    │   └── version.rb
    └── cryptojs-rails.rb

5 directories, 5 files

简而言之,我认为你必须通过另一个渠道(rails-assets,npm / yarn,bower,手动进入供应商或任何其他我无法想到的方式)在你的项目中导入cryptojs。 / p>