I need to encrypt strings with TEXT input, 1 round, HEX output, SHA-256 encryption. Which should be a string of characters of length 64.
Every SHA-256 encryption module I've tried in Google Apps Script docs returns a set of numbers. For example.
function SHA256() {
var signature = Utilities.computeHmacSha256Signature("this is my input",
"my key - use a stronger one",
Utilities.Charset.US_ASCII);
Logger.log(signature);
}
Outputs
[53, -75, -52, -25, -47, 86, -21, 14, -2, -57, 5, -13, 24, 105, -2, -84, 127, 115, -40, -75, -93, -27, -21, 34, -55, -117, -36, -103, -47, 116, -55, -61]
I haven't seen anything in the docs or elsewhere that specifies every parameter I'm going for outlined above for GAS. I wouldn't mind a deeper explanation of putting it together from scratch if that is what is required. I'm encrypting info to send to Facebook for Offline Conversions for ads. How does Facebook decrypt the encrypted strings?
Google Apps Script docs
https://developers.google.com/apps-script/reference/utilities/utilities#computeHmacSha256Signature(String,String,Charset)
̶U̶t̶i̶l̶i̶t̶i̶e̶s̶.̶c̶o̶m̶p̶u̶t̶e̶H̶m̶a̶c̶S̶h̶a̶2̶5̶6̶S̶i̶g̶n̶a̶t̶u̶r̶e̶
Utilities.computeDigest()
returns an array of bytes (8-bit integers). If you want to convert that array to a string composed of hexadecimal characters you'll have to do it manually as follows:
/** @type Byte[] */
var signature = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, value);
/** @type String */
var hexString = signature
.map(function(byte) {
// Convert from 2's compliment
var v = (byte < 0) ? 256 + byte : byte;
// Convert byte to hexadecimal
return ("0" + v.toString(16)).slice(-2);
})
.join("");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With