0
votes

I have to make a url request with Google Apps Script and for that, I need to enncode my login and password using a sha 1 algorithm and a base64 encoding. In the documantation for the request, I have the following php code :

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

So in Google Apps Script, i use the following code :

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; }

and then the function

Utilities.base64Encode

Finally I make the url request but authentication is unsuccessful. Does someone know how to do a php sha1 and base64encode in Google Apps Script ? Thanks

1
What do you mean by 'authentication is unsuccessful'?Kos
It is a url request to connect to a webservice I actually found the problem, the different sha1 I tested on google apps script differ from the one of php (I tested the encryption in php and tested that in google apps script and it worked)BrtD
Now my problem is to find a google apps script that is identical to the one of phpBrtD

1 Answers

5
votes

You can pass the Byte[] array returned from the Utilities::computeDigest directly to a Utilities::base64EncodeWebSafe invocation. So you could do something like the following:

function getLoginHash(loginData) {

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

}