Google Apps脚本 - 使用sha1和base64编码发出网址请求

时间:2017-12-04 08:32:36

标签: google-apps-script

我必须使用Google Apps脚本提出网址请求,为此,我需要使用sha 1算法和base64编码对我的登录名和密码进行编码。在请求的文档中,我有以下php代码:

$string = "loginpassword2017-15-04T09:25:00"; $hash = sha1($string, true); $hash = base64_encode($hash);

因此,在Google Apps脚本中,我使用以下代码:

function GetSHA1(input) { var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1, input); var txtHash = ''; for (j = 0; j <rawHash.length; j++) { var hashVal = rawHash[j]; if (hashVal < 0) hashVal += 256; if (hashVal.toString(16).length == 1) txtHash += "0"; txtHash += hashVal.toString(16); } return txtHash; }

然后是函数

Utilities.base64Encode

最后我发出了url请求,但身份验证失败了。有人知道如何在Google Apps脚本中执行php sha1和base64encode吗?感谢

1 个答案:

答案 0 :(得分:4)

您可以将 Byte[] 返回的 Utilities::computeDigest 数组直接传递给 Utilities::base64EncodeWebSafe 调用。所以你可以做类似以下的事情:

function getLoginHash(loginData) {

    return Utilities.base64EncodeWebSafe(Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1, loginData));

}