define([‘N/ui/serverWidget’, ‘N/runtime’, ‘N/crypto’, ‘N/encode’], function (ui, runtime, crypto, encode) {
function onRequest(option) {
var form = ui.createForm({
title: ‘My Credential Form’
});
if (option.request.method === ‘GET’) {
var skField = form.addSecretKeyField({
id: ‘mycredential’,
label: ‘Credential’,
restrictToScriptIds: ‘customscript_my_script’,
restrictToCurrentUser: false
});
skField.maxLength = 200;
form.addSubmitButton();
option.response.writePage(form);
} else {
var inputString = “India123”;
var myGuid = option.request.parameters.mycredential;
log.debug(“myGuid”,myGuid);
// Create the key
var sKey = crypto.createSecretKey({
guid: myGuid,
encoding: encode.Encoding.UTF_8
});
try {
var hmacSha512 = crypto.createHmac({
algorithm: ‘SHA512’,
key: sKey
});
hmacSha512.update({
input: inputString,
inputEncoding: encode.Encoding.BASE_64
});
var digestSha512 = hmacSha512.digest({
outputEncoding: encode.Encoding.BASE_64
});
} catch (e) {
log.error({
title: ‘Failed to hash input’,
details: e
});
}
form.addField({
id: ‘result’,
label: ‘Your digested hash value’,
type: ‘textarea’
}).defaultValue = digestSha512;
option.response.writePage(form);
}
}
return {
onRequest: onRequest
};
});