0

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

3
  • What do you mean by 'authentication is unsuccessful'? Commented Dec 4, 2017 at 9:34
  • 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) Commented Dec 4, 2017 at 9:38
  • Now my problem is to find a google apps script that is identical to the one of php Commented Dec 4, 2017 at 9:38

1 Answer 1

5

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

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.