如何从SSJS访问CSJS函数?

时间:2018-09-27 20:05:59

标签: javascript server-side salesforce-marketing-cloud

我正在使用SSJS生成HMAC SHA1密钥,并使用base64编码整个内容。问题是我不能包括两个库,

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/sha1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha1.js"></script>

仅仅是因为就页面而言,由于尚未加载SSJS而无法访问它们。


因此,在客户端,它可以正常工作。这是工作代码(不包括base64编码):

<!DOCTYPE html>
<html>

<head>
    <title>HMAC SHA1 Demo</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/sha1.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha1.js"></script>
    <script>
        $(window).on("load", function () {
            var _msg = 'Foo';
            var _secret = 'Bar';
            var _hashed = CryptoJS.HmacSHA1(_msg, _secret);
            $('#_msg').text(_msg);
            $('#_msgLength').text(_msg.length);
            $('#_hashed').text(_hashed);
        });
    </script>
</head>

<body>
    <h1>Message: "
        <span id="_msg"></span>" (length:
        <span id="_msgLength"></span>)</h1>
    <h3>Hashed: "
        <span id="_hashed"></span>"</h3>
</body>

</html>

但是,同样,我的问题是我需要在服务器端进行操作。我甚至尝试将全部代码都从这两种资源中转储到页面中,并向它们添加runat=server属性。但是现在我遇到了似乎是依赖错误的不同错误。具体来说,其中一个错误是Unknown type: l.HMAC.init Line: 2 Char: 3085 return new l.HMAC.init(t,i).finalize(n,另一个错误是Unknown type: u.HMAC.init Line: 8 Char: 480 return(new u.HMAC.init(a, d)).finalize(b

我要在下面粘贴我的代码,非常感谢任何人对此进行查看。也许用SSJS不可能做到这一点。

%%[ set @condition = 1 ]%%
<!DOCTYPE html>
<html>

<head>
    <title>HMAC SHA1 Demo</title>
    <script runat=server>
            Platform.Load("Core", "1");
    </script>
</head>

<body>
    %%[
        if @condition == 1 then 
            set @message = "Foo"
            set @secret = "Bar"
            set @msgLength = Length(@message)
            set @secretLength = Length(@secret)
    ]%%
        <script runat=server>
            //hardcoding all of the Crypto functions and logic
            var CryptoJS=(CryptoJS=CryptoJS||function(t,n){var i={},e=i.lib={},r=function(){},s=e.Base={extend:function(t){r.prototype=this;var n=new r;return t&&n.mixIn(t),n.hasOwnProperty("init")||(n.init=function(){n.$super.init.apply(this,arguments)}),n.init.prototype=n,n.$super=this,n},create:function(){var t=this.extend();return t.init.apply(t,arguments),t},init:function(){},mixIn:function(t){for(var n in t)t.hasOwnProperty(n)&&(this[n]=t[n]);t.hasOwnProperty("toString")&&(this.toString=t.toString)},clone:function(){return this.init.prototype.extend(this)}},o=e.WordArray=s.extend({init:function(t,n){t=this.words=t||[],this.sigBytes=void 0!=n?n:4*t.length},toString:function(t){return(t||c).stringify(this)},concat:function(t){var n=this.words,i=t.words,e=this.sigBytes;if(t=t.sigBytes,this.clamp(),e%4)for(var r=0;r<t;r++)n[e+r>>>2]|=(i[r>>>2]>>>24-r%4*8&255)<<24-(e+r)%4*8;else if(65535<i.length)for(r=0;r<t;r+=4)n[e+r>>>2]=i[r>>>2];else n.push.apply(n,i);return this.sigBytes+=t,this},clamp:function(){var n=this.words,i=this.sigBytes;n[i>>>2]&=4294967295<<32-i%4*8,n.length=t.ceil(i/4)},clone:function(){var t=s.clone.call(this);return t.words=this.words.slice(0),t},random:function(n){for(var i=[],e=0;e<n;e+=4)i.push(4294967296*t.random()|0);return new o.init(i,n)}}),a=i.enc={},c=a.Hex={stringify:function(t){var n=t.words;t=t.sigBytes;for(var i=[],e=0;e<t;e++){var r=n[e>>>2]>>>24-e%4*8&255;i.push((r>>>4).toString(16)),i.push((15&r).toString(16))}return i.join("")},parse:function(t){for(var n=t.length,i=[],e=0;e<n;e+=2)i[e>>>3]|=parseInt(t.substr(e,2),16)<<24-e%8*4;return new o.init(i,n/2)}},h=a.Latin1={stringify:function(t){var n=t.words;t=t.sigBytes;for(var i=[],e=0;e<t;e++)i.push(String.fromCharCode(n[e>>>2]>>>24-e%4*8&255));return i.join("")},parse:function(t){for(var n=t.length,i=[],e=0;e<n;e++)i[e>>>2]|=(255&t.charCodeAt(e))<<24-e%4*8;return new o.init(i,n)}},f=a.Utf8={stringify:function(t){try{return decodeURIComponent(escape(h.stringify(t)))}catch(t){throw Error("Malformed UTF-8 data")}},parse:function(t){return h.parse(unescape(encodeURIComponent(t)))}},u=e.BufferedBlockAlgorithm=s.extend({reset:function(){this._data=new o.init,this._nDataBytes=0},_append:function(t){"string"==typeof t&&(t=f.parse(t)),this._data.concat(t),this._nDataBytes+=t.sigBytes},_process:function(n){var i=this._data,e=i.words,r=i.sigBytes,s=this.blockSize,a=r/(4*s);if(n=(a=n?t.ceil(a):t.max((0|a)-this._minBufferSize,0))*s,r=t.min(4*n,r),n){for(var c=0;c<n;c+=s)this._doProcessBlock(e,c);c=e.splice(0,n),i.sigBytes-=r}return new o.init(c,r)},clone:function(){var t=s.clone.call(this);return t._data=this._data.clone(),t},_minBufferSize:0});e.Hasher=u.extend({cfg:s.extend(),init:function(t){this.cfg=this.cfg.extend(t),this.reset()},reset:function(){u.reset.call(this),this._doReset()},update:function(t){return this._append(t),this._process(),this},finalize:function(t){return t&&this._append(t),this._doFinalize()},blockSize:16,_createHelper:function(t){return function(n,i){return new t.init(i).finalize(n)}},_createHmacHelper:function(t){return function(n,i){return new l.HMAC.init(t,i).finalize(n)}}});var l=i.algo={};return i}(Math))||function(t,n){var i={},e=i.lib={},r=function(){},s=e.Base={extend:function(t){r.prototype=this;var n=new r;return t&&n.mixIn(t),n.hasOwnProperty("init")||(n.init=function(){n.$super.init.apply(this,arguments)}),n.init.prototype=n,n.$super=this,n},create:function(){var t=this.extend();return t.init.apply(t,arguments),t},init:function(){},mixIn:function(t){for(var n in t)t.hasOwnProperty(n)&&(this[n]=t[n]);t.hasOwnProperty("toString")&&(this.toString=t.toString)},clone:function(){return this.init.prototype.extend(this)}},o=e.WordArray=s.extend({init:function(t,n){t=this.words=t||[],this.sigBytes=void 0!=n?n:4*t.length},toString:function(t){return(t||c).stringify(this)},concat:function(t){var n=this.words,i=t.words,e=this.sigBytes;if(t=t.sigBytes,this.clamp(),e%4)for(var r=0;r<t;r++)n[e+r>>>2]|=(i[r>>>2]>>>24-r%4*8&255)<<24-(e+r)%4*8;else if(65535<i.length)for(r=0;r<t;r+=4)n[e+r>>>2]=i[r>>>2];else n.push.apply(n,i);return this.sigBytes+=t,this},clamp:function(){var n=this.words,i=this.sigBytes;n[i>>>2]&=4294967295<<32-i%4*8,n.length=t.ceil(i/4)},clone:function(){var t=s.clone.call(this);return t.words=this.words.slice(0),t},random:function(n){for(var i=[],e=0;e<n;e+=4)i.push(4294967296*t.random()|0);return new o.init(i,n)}}),a=i.enc={},c=a.Hex={stringify:function(t){var n=t.words;t=t.sigBytes;for(var i=[],e=0;e<t;e++){var r=n[e>>>2]>>>24-e%4*8&255;i.push((r>>>4).toString(16)),i.push((15&r).toString(16))}return i.join("")},parse:function(t){for(var n=t.length,i=[],e=0;e<n;e+=2)i[e>>>3]|=parseInt(t.substr(e,2),16)<<24-e%8*4;return new o.init(i,n/2)}},h=a.Latin1={stringify:function(t){var n=t.words;t=t.sigBytes;for(var i=[],e=0;e<t;e++)i.push(String.fromCharCode(n[e>>>2]>>>24-e%4*8&255));return i.join("")},parse:function(t){for(var n=t.length,i=[],e=0;e<n;e++)i[e>>>2]|=(255&t.charCodeAt(e))<<24-e%4*8;return new o.init(i,n)}},f=a.Utf8={stringify:function(t){try{return decodeURIComponent(escape(h.stringify(t)))}catch(t){throw Error("Malformed UTF-8 data")}},parse:function(t){return h.parse(unescape(encodeURIComponent(t)))}},u=e.BufferedBlockAlgorithm=s.extend({reset:function(){this._data=new o.init,this._nDataBytes=0},_append:function(t){"string"==typeof t&&(t=f.parse(t)),this._data.concat(t),this._nDataBytes+=t.sigBytes},_process:function(n){var i=this._data,e=i.words,r=i.sigBytes,s=this.blockSize,a=r/(4*s);if(n=(a=n?t.ceil(a):t.max((0|a)-this._minBufferSize,0))*s,r=t.min(4*n,r),n){for(var c=0;c<n;c+=s)this._doProcessBlock(e,c);c=e.splice(0,n),i.sigBytes-=r}return new o.init(c,r)},clone:function(){var t=s.clone.call(this);return t._data=this._data.clone(),t},_minBufferSize:0});e.Hasher=u.extend({cfg:s.extend(),init:function(t){this.cfg=this.cfg.extend(t),this.reset()},reset:function(){u.reset.call(this),this._doReset()},update:function(t){return this._append(t),this._process(),this},finalize:function(t){return t&&this._append(t),this._doFinalize()},blockSize:16,_createHelper:function(t){return function(n,i){return new t.init(i).finalize(n)}},_createHmacHelper:function(t){return function(n,i){return new l.HMAC.init(t,i).finalize(n)}}});var l=i.algo={};return i}(Math);!function(){var t=CryptoJS,n=(r=t.lib).WordArray,i=r.Hasher,e=[],r=t.algo.SHA1=i.extend({_doReset:function(){this._hash=new n.init([1732584193,4023233417,2562383102,271733878,3285377520])},_doProcessBlock:function(t,n){for(var i=this._hash.words,r=i[0],s=i[1],o=i[2],a=i[3],c=i[4],h=0;80>h;h++){if(16>h)e[h]=0|t[n+h];else{var f=e[h-3]^e[h-8]^e[h-14]^e[h-16];e[h]=f<<1|f>>>31}f=(r<<5|r>>>27)+c+e[h],f=20>h?f+(1518500249+(s&o|~s&a)):40>h?f+(1859775393+(s^o^a)):60>h?f+((s&o|s&a|o&a)-1894007588):f+((s^o^a)-899497514),c=a,a=o,o=s<<30|s>>>2,s=r,r=f}i[0]=i[0]+r|0,i[1]=i[1]+s|0,i[2]=i[2]+o|0,i[3]=i[3]+a|0,i[4]=i[4]+c|0},_doFinalize:function(){var t=this._data,n=t.words,i=8*this._nDataBytes,e=8*t.sigBytes;return n[e>>>5]|=128<<24-e%32,n[14+(e+64>>>9<<4)]=Math.floor(i/4294967296),n[15+(e+64>>>9<<4)]=i,t.sigBytes=4*n.length,this._process(),this._hash},clone:function(){var t=i.clone.call(this);return t._hash=this._hash.clone(),t}});t.SHA1=i._createHelper(r),t.HmacSHA1=i._createHmacHelper(r)}(),function(){var t=CryptoJS,n=t.enc.Utf8;t.algo.HMAC=t.lib.Base.extend({init:function(t,i){t=this._hasher=new t.init,"string"==typeof i&&(i=n.parse(i));var e=t.blockSize,r=4*e;i.sigBytes>r&&(i=t.finalize(i)),i.clamp();for(var s=this._oKey=i.clone(),o=this._iKey=i.clone(),a=s.words,c=o.words,h=0;h<e;h++)a[h]^=1549556828,c[h]^=909522486;s.sigBytes=o.sigBytes=r,this.reset()},reset:function(){var t=this._hasher;t.reset(),t.update(this._iKey)},update:function(t){return this._hasher.update(t),this},finalize:function(t){var n=this._hasher;return t=n.finalize(t),n.reset(),n.finalize(this._oKey.clone().concat(t))}})}(),function(){var t=CryptoJS,n=t.lib,i=n.WordArray,e=n.Hasher,r=[],s=t.algo.SHA1=e.extend({_doReset:function(){this._hash=new i.init([1732584193,4023233417,2562383102,271733878,3285377520])},_doProcessBlock:function(t,n){for(var i=this._hash.words,e=i[0],s=i[1],o=i[2],a=i[3],c=i[4],h=0;h<80;h++){if(h<16)r[h]=0|t[n+h];else{var f=r[h-3]^r[h-8]^r[h-14]^r[h-16];r[h]=f<<1|f>>>31}var u=(e<<5|e>>>27)+c+r[h];u+=h<20?1518500249+(s&o|~s&a):h<40?1859775393+(s^o^a):h<60?(s&o|s&a|o&a)-1894007588:(s^o^a)-899497514,c=a,a=o,o=s<<30|s>>>2,s=e,e=u}i[0]=i[0]+e|0,i[1]=i[1]+s|0,i[2]=i[2]+o|0,i[3]=i[3]+a|0,i[4]=i[4]+c|0},_doFinalize:function(){var t=this._data,n=t.words,i=8*this._nDataBytes,e=8*t.sigBytes;return n[e>>>5]|=128<<24-e%32,n[14+(e+64>>>9<<4)]=Math.floor(i/4294967296),n[15+(e+64>>>9<<4)]=i,t.sigBytes=4*n.length,this._process(),this._hash},clone:function(){var t=e.clone.call(this);return t._hash=this._hash.clone(),t}});t.SHA1=e._createHelper(s),t.HmacSHA1=e._createHmacHelper(s)}();
            var _msg = Variable.GetValue("@message");
            var _secret = Variable.GetValue("@secret");
            var _hashed = CryptoJS.HmacSHA1(_msg, _secret);
            Variable.SetValue("@hashed", _hashed);
        </script>
    %%[
        set @base64 = Base64Encode(@hashed)
        set @finalData = Replace(@base64, '=', '_')
    ]%%
        <h3>Message: %%=v(@message)=%%, (length: %%=v(@msgLength)=%%)</h3>
        <h3>Secret: %%=v(@secret)=%%, (length: %%=v(@secretLength)=%%)</h3>
        <h3>Hashed Key (HMAC SHA1): %%=v(@hashed)=%%</h3>
        <h3>Base64 Hashed Key: %%=v(@finalData)=%%</h3>

    %%[ elseif @condition == 0 then
            set @message = "Bar"
            set @secret = "Foo"
            set @msgLength = Length(@message)
            set @secretLength = Length(@secret)
    ]%%
        <script runat=server>
                var _msg = Variable.GetValue("@message");
                var _secret = Variable.GetValue("@secret");
                var _hashed = CryptoJS.HmacSHA1(_msg, _secret);
                Variable.SetValue("@hashed", _hashed);
        </script>
    %%[
        set @base64 = Base64Encode(@hashed)
        set @finalData = Replace(@base64, '=', '_')
    ]%%
        <h3>Message: %%=v(@message)=%%, (length: %%=v(@msgLength)=%%)</h3>
        <h3>Secret: %%=v(@secret)=%%, (length: %%=v(@secretLength)=%%)</h3>
        <h3>Hashed Key (HMAC SHA1): %%=v(@hashed)=%%</h3>
        <h3>Base64 Hashed Key: %%=v(@finalData)=%%</h3>

    %%[ else ]%%

        <h1>Pass</h1>

    %%[ endif ]%%
</body>

</html>

在这种情况下,@condition == 1var _hashed...行崩溃。

0 个答案:

没有答案