SuiteScript hmac sha256

Viewed 2527

I'm actually working on a new project based on netsuite product. I'm trying to encrypt a message using hmac sha256.

What's is the simple way to do it considering that I have the stringToEncrypt and a key.

I've read the documentation in Netsuite but I'm still stucked...

There is my function

function toHmacSHA256Base64(toCrypt, key) {
        var inputString = toCrypt;
        var myGuid = key;
        var sKey = crypto.createSecretKey({
            guid: myGuid,
            encoding: encode.Encoding.UTF_8
        });
        var hmacSHA256 = crypto.createHmac({
            algorithm: 'SHA256',
            key: sKey
        });
        hmacSHA256.update({
            input: inputString,
            inputEncoding: encode.Encoding.BASE_64
        });
        var digestSHA256 = hmacSHA256.digest({
            outputEncoding: encode.Encoding.HEX
        });
        return digestSHA256;
};

of course behind the word crypto I use the module 'N/crypto' and encode 'N/encode'. Thx a lot.

1 Answers

That's roughly correct and looks exactly like the sample from the NS help. If you have a string then you probably want inputEncoding:encode.Encoding.UTF_8 for the update call.

What's missing is how to generate the guid of the secret key. For that you use a suitelet. Note the addSecretKeyField not the addCredentialField of the NS help:

/**
 *@NApiVersion 2.x
 *@NScriptType Suitelet
 */
define(['N/ui/serverWidget', './config.js'],
    function(serverWidget, config) {
        function onRequest(context) {
            if (context.request.method === 'GET') {
                var form = serverWidget.createForm({
                    title: 'SFTP Password'
                });

                form.addSecretKeyField({
                    id : 'username',
                    label : 'Pwd',
                    restrictToScriptIds : config.targetScript,
                    restrictToCurrentUser : false
                });
                form.addSubmitButton({
                    label: 'Submit Button'
                });

                context.response.writePage(form);
            } else {
                var textField = context.request.parameters.username;
                context.response.write('You have entered: ' + textField);
            }
        }

        return {
            onRequest: onRequest
        };
    });

FWIW encrypt is the wrong term here. You are creating a hash of the data which will be used for ensuring data integrity. You cannot decrypt a hash.

Once a GUID for a key has been generated I just store it in a config file (same one as is used for the script list above.

in TypeScript it looks like:

/**
 * config.js
 * @NApiVersion 2.x
 */

export var config =  {

    'host': '162.242.144.xxx',
    'userName': 'unsername',
    'targetScript': ['customscript_transmit_dsv_943', 'customscript_transmit_dsv_940', 'customscript_retrieve_dsv_944'],
    'hostKey': 'AAAAB3Nza...Wz'
};

Then everything except the config.xs files can be stored in version control. Audience needs to be set appropriately on the files used in the script.

Related