javascript - Crypto-Js 库的 hmac-256 脚本返回函数结构而不是 Google Apps 脚本中的值,在外部工作正常吗?

标签 javascript google-apps-script google-sheets cryptojs hmacsha1

我正在设置一个谷歌电子表格项目以连接到我的 CryptoExchange 应用程序接口(interface)。 但是当谈到这个简单的 CryptoJs Hmac-sha256 脚本时,它不起作用:它返回函数结构而不是值,而在外部它工作正常(see my jsfiddle)。

现在,我明白了from this Stack answer Cameron Roberts 指出,Apps 脚本在某些 POV 下的行为不同,但我不明白这之间有何关系。

此外,如果我只是切换脚本并使用 Stanford Javascript Crypto 库,代码执行完美完全没有问题,在 Google 中都是如此 Apps Script 和它之外的当然。

这是我的代码:

eval(UrlFetchApp.fetch('https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha256.js').getContentText());


function write() {
var hash = CryptoJS.HmacSHA256("message", "secret");
return hash;
}
Logger.log(write());

以及来自 Google Apps 脚本的控制台日志

[19-06-07 00:53:32:859 PDT] {mixIn=
function (a) {
    for (var c in a) {
        a.hasOwnProperty(c) && (this[c] = a[c]);
    }
    a.hasOwnProperty("toString") && (this.toString = a.toString);
}
, extend=
function (a) {
    q.prototype = this;
    var c = new q();
    a && c.mixIn(a);
    c.hasOwnProperty("init") || (c.init = function () {
        c.$super.init.apply(this, arguments);
    });
    c.init.prototype = c;
    c.$super = this;
    return c;
}
, init=
function (a, c) {
    a = this.words = a || [];
    this.sigBytes = c != s ? c : 4 * a.length;
}
, random=
function (a) {
    for (var c = [], d = 0; d < a; d += 4) {
        c.push(4294967296 * h.random() | 0);
    }
    return new r.init(c, a);
}
, words=[-1.956689808E9, 6.97680217E8, -1.940439631E9, -5.01717335E8, -

1.205480281E9, -1.798215209E9, 1.0131952E8, 1.469462027E9], clone=
function () {
    var a = m.clone.call(this);
    a.words = this.words.slice(0);
    return a;
}
, sigBytes=32.0, create=
function () {
    var a = this.extend();
    a.init.apply(a, arguments);
    return a;
}
, toString=
function (a) {
    return (a || k).stringify(this);
}
, concat=
function (a) {
    var c = this.words, d = a.words, b = this.sigBytes;
    a = a.sigBytes;
    this.clamp();
    if (b % 4) {
        for (var e = 0; e < a; e++) {
            c[b + e >>> 2] |= (d[e >>> 2] >>> 24 - 8 * (e % 4) & 255) << 24 - 

8 * ((b + e) % 4);
        }
    } else {
        if (65535 < d.length) {
            for (e = 0; e < a; e += 4) {
                c[b + e >>> 2] = d[e >>> 2];
            }
        } else {
            c.push.apply(c, d);
        }
    }
    this.sigBytes += a;
    return this;
}
, clamp=
function () {
    var a = this.words, c = this.sigBytes;
    a[c >>> 2] &= 4294967295 << 32 - 8 * (c % 4);
    a.length = h.ceil(c / 4);
}
, $super={extend=
function (a) {
    q.prototype = this;
    var c = new q();
    a && c.mixIn(a);
    c.hasOwnProperty("init") || (c.init = function () {
        c.$super.init.apply(this, arguments);
    });
    c.init.prototype = c;
    c.$super = this;
    return c;
}
, mixIn=
function (a) {
    for (var c in a) {
        a.hasOwnProperty(c) && (this[c] = a[c]);
    }
    a.hasOwnProperty("toString") && (this.toString = a.toString);
}
, init=
function () {
}
, clone=
function () {
    return this.init.prototype.extend(this);
}
, create=
function () {
    var a = this.extend();
    a.init.apply(a, arguments);
    return a;
}
}}

虽然相同的代码within jsfiddle工作正常

编辑: 虽然我的问题仍然是我的好奇心,但我刚刚在堆栈上找到了整个回复分支,其中涉及在 Google Apps 脚本中我不知道的特定方法:用于创建 HMAC Sha256 签名的内置类实用程序

这可能不是我在理论知识上的问题的答案,但从实践的 Angular 可能会解决我的问题;所以我现在会调查一下。 谢谢

Generate a keyed hash value using the HMAC method with Google Apps Script

How to get Hex value from computeHmacSha256Signature method of Google Apps Script?

get back a string representation from computeDigest(algorithm, value) byte[]

最佳答案

  • 您想使用 Google Apps 脚本从 CryptoJS.HmacSHA256("message", "secret") 中检索 8b5f48702995c1598c573db1e21866a9b825d4a794d169d7060a03605796360b 的值。

如果我的理解是正确的,直接使用Google Apps Script的方法计算值怎么样?在这种情况下,不使用 CryptoJS。请将此视为几个答案之一。

示例脚本:

var res = Utilities.computeHmacSha256Signature("message", "secret")
  .map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2)}).join("");
Logger.log(res)

结果:

8b5f48702995c1598c573db1e21866a9b825d4a794d169d7060a03605796360b

备注:

上面脚本的要点如下。

  • 在 Google Apps Script 中,通过 Utilities.computeHmacSha256Signature() 加密的数据是带符号的十六进制字节数组。
  • 在您的例子中,字节数组被转换为无符号的十六进制数。

引用资料:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

关于javascript - Crypto-Js 库的 hmac-256 脚本返回函数结构而不是 Google Apps 脚本中的值,在外部工作正常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56490535/

相关文章:

javascript - 失败的 prop 类型 : Invalid prop `rowSelection` of type `function` supplied to `Table` , 预期 `object`

search - 如何搜索 Google 电子表格?

用于从 Google 工作表 URL 中提取电子表格 ID 和工作表 ID 的 Javascript 正则表达式

google-apps-script - 过滤 View 的按钮

Javascript/Jquery 树遍历问题

javascript - Angularjs ng-repeat 在另一个 ng-repeat 中

javascript - Laravel Ajax Post 返回 500

javascript - 有什么方法可以从 url 获取文件大小而不使用谷歌应用程序脚本下载它。?

google-apps-script - 使用Google幻灯片API更新Google幻灯片中对象的文本

javascript - 如何将 google 工作表中的单元格值设置为 HTML 侧边栏文本区域的默认值?