Does anyone know how I can generate a PBKDF2 key in Google Apps Script? (including generating the "salt" value needed)
I'm looking for something like this from Node.js:
const key = crypto.pbkdf2Sync('secret', 'salt', 100000, 64, 'sha512');
Or something like this from the Go pbkdf2 function:
pbkdf2.Key([]byte(plaintext), salt, iterations, macKeyLen, sha512.New)
I'm trying to satisfy the standard from Apple's MDM Protocol for AccountConfiguration, as on this page:
Here is the snippet I'm trying to fulfill:
The passwordHash data objects should be created on the server using the CommonCrypto libraries or equivalent as a salted SHA512 PBKDF2 dictionary containing three items: entropy is the derived key from the password hash (an example is from CCKeyDerivationPBKDF()), salt is the 32 byte randomized salt (from CCRandomCopyBytes()), and iterations contains the number of iterations (from CCCalibratePBKDF()) using a minimum hash time of 100 milliseconds (or if not known, a number in the range 20,000 to 40,000 iterations). This dictionary of the three keys should be placed into an outer dictionary under the key SALTED-SHA512-PBKDF2 and converted to binary data before being set into the configuration dictionary passwordHash key value.
I need the PBKDF2 key for the "entropy" dictionary item.
The script needs to run as a Google Apps Script, as it will be part of a Sheets add-on that I am developing.
UPDATE: I thought it might help to work backwards a bit.
Here is a password hash like I am looking for. It was generated using the Go pbkdf2 function, and the password "password".
PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPCFET0NUWVBFIHBsaXN0IFBVQkxJQyAiLS8vQXBwbGUvL0RURCBQTElTVCAxLjAvL0VOIiAiaHR0cDovL3d3dy5hcHBsZS5jb20vRFREcy9Qcm9wZXJ0eUxpc3QtMS4wLmR0ZCI+CjxwbGlzdCB2ZXJzaW9uPSIxLjAiPjxkaWN0PjxrZXk+U0FMVEVELVNIQTUxMi1QQktERjI8L2tleT48ZGljdD48a2V5PmVudHJvcHk8L2tleT48ZGF0YT4ycnFxTGVHQVY0OTdFemROUm5pcHNPcUFGZHhHSjZzZWV5d2U3UURyQlVxNzE5ZitUNTNkaG0rQkJTeUZ2WlovM25TUVZyeHhUTGk1a1FhQWF5Nm5Kc0tjTGllRFBwR0E2dWFp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D4K";
var decoded = Utilities.base64Decode(passwordHash);
var plist = Utilities.newBlob(decoded).getDataAsString();
I get the following plist output:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SALTED-SHA512-PBKDF2</key>
<dict>
<key>entropy</key>
<data>g6uFvivFJpQ1nJ1BnZwbM0uQK5VvIj+Sk8wJbMTEv8YtEYKVTXZHeIndpt9fY3Hfi7MCuBoZQ8xN09E5bSg4ZoAZ7qR9fMjptkg3dMinx2ipCvx4hIvDrYKE3jaxkEZHYIioSXvXIh5Mw34xlwRnDwsTfzKxtbjTCohU/eJ7NXY=</data>
<key>iterations</key>
<integer>39766</integer>
<key>salt</key>
<data>QpV97+e+W8/tpwgC5uw+OEAc0qKgIgVXM4Ww4vGcWtw=</data>
</dict>
</dict>
</plist>
Does anyone know how (in Google Apps Script) to generate "entropy", "iterations", and "salt" values like this data from an original password value of "password"?
I think it would require using the methods of this Class:
https://developers.google.com/apps-script/reference/utilities/utilities