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



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

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

所以在Google Apps Script中,我使用以下代码:

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 Script中进行php sha1和base64encode吗?谢谢

您可以将从Utilities::computeDigest返回的Byte[]数组直接传递给Utilities::base64EncodeWebSafe调用。因此,您可以执行以下操作:

function getLoginHash(loginData) {
return Utilities.base64EncodeWebSafe(Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1, loginData));
}

最新更新