Example Google Apps Script custom sheet function example. For more info see https://mashe.hawksey.info?p=19219
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Generates a SHA-256 hash of a text input. | |
* | |
* @param {String||Array} value to SHA-256 hash. | |
* @param {String} key to use to generate the hash with. | |
* @return the SHA-256 hash of the value | |
* @customfunction | |
*/ | |
function HASH(value, key) { | |
if (typeof key !== 'string'){ // test the key is a string | |
throw "Key needs to be a text value"; | |
} | |
if (value.map) { // Test whether input is an array. | |
return value.map(function(v){return HASH(v, key)}); // Recurse over array if so. | |
} else { // The bit that actual creates the hash | |
// based on https://stackoverflow.com/a/27933459 | |
return Utilities.computeHmacSha256Signature(value, key).reduce(function(str, chr) { | |
chr = (chr < 0 ? chr + 256 : chr).toString(16); | |
return str + (chr.length == 1 ? '0' : '') + chr; | |
}, ''); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment