Here you go… put the Input in A2 the Secret Key in B2 and your Output will go in C2.
function computeHMACSHA1() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Get the input text from cell A2 and B2
var inputText = sheet.getRange("A2").getValue();
var secretKey = sheet.getRange("B2").getValue();
// Convert inputText and secretKey to Base64
var inputTextBase64 = Utilities.base64Encode(inputText);
var secretKeyBase64 = Utilities.base64Encode(secretKey);
// Convert Base64 strings back to binary
var inputBytes = Utilities.base64Decode(inputTextBase64);
var keyBytes = Utilities.base64Decode(secretKeyBase64);
// Implement HMAC-SHA1 manually
var blockSize = 64; // HMAC-SHA1 block size
if (keyBytes.length > blockSize) {
keyBytes = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1, keyBytes);
}
while (keyBytes.length < blockSize) {
keyBytes.push(0);
}
var oKeyPad = [];
var iKeyPad = [];
for (var i = 0; i < blockSize; i++) {
oKeyPad.push(keyBytes[i] ^ 0x5C);
iKeyPad.push(keyBytes[i] ^ 0x36);
}
var innerHash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1, iKeyPad.concat(inputBytes));
var finalHash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1, oKeyPad.concat(innerHash));
// Convert the binary hash to a hexadecimal string
var hmacSha1Hex = '';
for (var i = 0; i < finalHash.length; i++) {
var byte = finalHash[i];
hmacSha1Hex += (byte & 0xFF).toString(16).padStart(2, '0'); // Format as two-digit hex without leading 0's
}
// Put the result in cell C2
sheet.getRange("C2").setValue(hmacSha1Hex);
}