Google Directory API setting user's password with hash invalid password

65 Views Asked by At

I am trying to create an Apps Script function that creates a SHA-256 hash of a password which is put in request of Directory API.

The code right now looks like this:

function testUpdatePasswordWithHash () {
  let pass = "SomePassword";
  let salt = "";
  let possible = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

  for(var i=1;i<16;i++){
    salt += possible.charAt(Math.floor(Math.random() * possible.length));
  }

  let passHashInBytes = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, (pass + salt));
  let passHashInHex = Utilities.base64Encode(passHashInBytes);

  let passHashInHexWithPrefix = "$5$" + salt + "$" + passHashInHex;

  let passChangeEvent = AdminDirectory.Users.update({
    password: passHashInHexWithPrefix,
    hashFunction: "crypt"
  }, "[email protected]");
}

I'm using salt as pointed out in this thread.

Right now I'm testing it using users.update on a test user but when I set it up like that I receive this response in Apps Script console:

GoogleJsonResponseException: API call to directory.users.update failed with error: Invalid Password

I have tried changing the salt length but other than that I'm clueless as to how to make this work.

0

There are 0 best solutions below