Tried using Crypto module and receiving the error ...
# suitescript
r
Tried using Crypto module and receiving the error as below. “An error occurred while decrypting a credential” Any one has any ideas to resolve this?
b
are you using secrets or guids?
r
using guids
b
and what does the secret key field creating code look like
r
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 }; });
here is sample code we have used
while digesting it getting an error
b
its likely both of your encodings are wrong
but lets start with the secret key field one first
your secret key is supposed to be represented by bytes
humans use strings to represent the byes, since thats convenient
most tools will output base64 encoded strings, mostly because utf-8 is not appropiate for representing bytes
are you sure your secret key is encoded in utf-8