Modules

filter

 

Objects/Functions

module (N/crypto)

SuiteScript 2.0

Main Examples

N/crypto Module
Member Type: Name
Search NetSuite - https://system.netsuite.com/app/help/helpcenter.nl?search=N/crypto Module

// Code Example 1
/**
* @NApiVersion 2.x
*/

require(['N/crypto', 'N/encode', 'N/runtime'], function(crypto, encode, runtime) {
function createSecureKeyWithHash() {
var inputString = 'YWJjZGVmZwo=';
var myGuid = '{284CFB2D225B1D76FB94D150207E49DF}';

var sKey = crypto.createSecretKey({
guid: myGuid,
encoding: encode.Encoding.UTF_8
});

var hmacSHA512 = crypto.createHmac({
algorithm: crypto.HashAlg.SHA512,
key: sKey
});
hmacSHA512.update({
input: inputString,
inputEncoding: encode.Encoding.BASE_64
});
var digestSHA512 = hmacSHA512.digest({
outputEncoding: encode.Encoding.HEX
});
}

createSecureKeyWithHash();
});

// Code Example 2
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
*/
define(['N/ui/serverWidget', 'N/runtime', 'N/crypto', 'N/encode'], function(ui, runtime, crypto, encode) {
function onRequest(option) {
if (option.request.method === 'GET') {
var form = ui.createForm({
title: 'My Credential Form'
});

var skField = form.addSecretKeyField({
id: 'mycredential',
label: 'Credential',
restrictToScriptIds: [runtime.getCurrentScript().id],
restrictToCurrentUser: false
})
skField.maxLength = 200;

form.addSubmitButton();
option.response.writePage(form);
} else {
var form = ui.createForm({
title: 'My Credential Form'
});

var inputString = "YWJjZGVmZwo=";
var myGuid = option.request.parameters.mycredential;

// 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.HEX
});
} 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
};
});

//SOURCE: https://system.netsuite.com/app/help/helpcenter.nl?fid=section_4358549582.html