How to Hash Data in NetSuite Using the N/crypto Module

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */
define(['N/crypto', 'N/log'], function (crypto, log) {

  function onRequest(context) {
    let textToHash = 'Sample Text';
     
    let hash = crypto.hash({
      algorithm: crypto.HashAlg.SHA256,
      input: textToHash
    });

    log.debug('Hashed Value:', hash);
    context.response.write('SHA-256 Hashed Value: ' + hash);
  }

  return {
    onRequest: onRequest
  };
});

How It Works

  • The crypto.hash() function takes an input string and applies the SHA-256 algorithm.
  • The result is a unique, irreversible hash value.
  • This is useful for password storage or data integrity checks.

Other Supported Hash Algorithms

The N/crypto module supports multiple hashing algorithms:

  • crypto.HashAlg.SHA1
  • crypto.HashAlg.SHA256
  • crypto.HashAlg.SHA512
  • crypto.HashAlg.MD5

Leave a comment

Your email address will not be published. Required fields are marked *