N/crypto Module Script Samples

The following script samples demonstrate how to use the features of the N/crypto module.

Create a Secure Key Using SHA512

Important:

This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.

The following sample demonstrates the APIs needed to generate a secure key using the SHA512 hashing algorithm. The GUID in this sample is a placeholder. You must replace it with a valid value from your NetSuite account. To create a real password GUID, obtain a password value from a credential field on a form. For more information, see Form.addCredentialField(options). Also see the Create a Suitelet to Request User Credentials, Create a Secret Key, and Encode a Sample String that shows how to create a form field that generates a GUID.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.1
 */
/**This sample demonstrates the APIs needed to generate a secure key using the SHA512 hashing algorithm.
*  The mySecret variable is a placeholder that must be replaced with a valid secret from your NetSuite account. 
*  For information about  secrets management page, see Account Administration > Authentication > Secrets Management
 * in the Help Center.
**/

require(['N/crypto', 'N/encode', 'N/runtime'], (crypto, encode, runtime) => {
    function createSecureKeyWithHash() {
        let mySecret = 'custsecret_my_secret';    //secret id take from secrets management page

        let sKey = crypto.createSecretKey({
            secret: mySecret,
            encoding: encode.Encoding.UTF_8
        });

        let hmacSHA512 = crypto.createHmac({
            algorithm: crypto.HashAlg.SHA512,
            key: sKey
        });

        hmacSHA512.update({
            input: inputString,
            inputEncoding: encode.Encoding.BASE_64
        });

        let digestSHA512 = hmacSHA512.digest({
            outputEncoding: encode.Encoding.HEX
        });
    }
    createSecureKeyWithHash();
}); 

          

Create a Suitelet to Request User Credentials, Create a Secret Key, and Encode a Sample String

Important:

This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.

The following sample creates a simple Suitelet that requests user credentials, creates a secret key, and encodes a sample string.

Note:

This script sample uses the define function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript 2.x Global Objects.

Note:

The default maximum length for a secret key field is 32 characters. If needed, use the Field.maxLength property to change this value.

            /**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */
define(['N/ui/serverWidget', 'N/runtime', 'N/crypto', 'N/encode'], (serverWidget, runtime, crypto, encode) => {
    function onRequest(option) {
        if (option.request.method === 'GET') {
            let form = serverWidget.createForm({
                title: 'My Credential Form'
            });
            let skField = form.addSecretKeyField({
                id: 'mycredential',
                label: 'Credential',
                restrictToScriptIds: [runtime.getCurrentScript().id],
                restrictToCurrentUser: false
            })
            skField.maxLength = 200;

            form.addSubmitButton();

            option.response.writePage(form);
        } else {
            let form = serverWidget.createForm({
                title: 'My Credential Form'
            });

            const inputString = "YWJjZGVmZwo=";
            let myGuid = option.request.parameters.mycredential;

            // Create the key
            let sKey = crypto.createSecretKey({
                guid: myGuid,
                encoding: encode.Encoding.UTF_8
            });

            try {
                let hmacSha512 = crypto.createHmac({
                    algorithm: 'SHA512',
                    key: sKey
                });
                hmacSha512.update({
                    input: inputString,
                    inputEncoding: encode.Encoding.BASE_64
                });
                let 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
    };
}); 

          

General Notices