NextGen Knowledge Center

Generating a Hash with JavaScript

Use HashUtil.generate() to create a hex hash of a message passing through a channel in JavaScript contexts: var hash = HashUtil.generate(message);

We provide 3 overloaded versions:

  • HashUtil.generate(Object)
    • Takes in any object and returns a hex hash of the object as a String. This defaults to using the platform's default character encoding and the SHA-256 hashing algorithm.
    • Example
      • HashUtil.generate("Hello, World!")
  • HashUtil.generate(String, String, String)
    • Takes in data as a string, a character encoding as a string, and a hashing algorithm as a string and returns a hex hash of the data as a string.
    • Example
      • HashUtil.generate("Hello, World!", "UTF-8", "SHA-256")
  • HashUtil.generate(byte[], String)
    • Takes in data as a byte[] and a hashing algorithm as a string and returns a hex hash of the data as a string.
    • Example
      • HashUtil.generate("Hello, World!".getBytes(), "SHA-256")

Supported Character Encodings (for more information, please see documentation on Charset):

  • US-ASCII
  • ISO-8859-1
  • UTF-8
  • UTF-16BE
  • UTF-16LE
  • UTF-16

Supported Algorithms (for more information, please see documentation on MessageDigest and MessageDigest Algorithms):

  • MD2
  • MD5
  • SHA-1
  • SHA-224
  • SHA-256
  • SHA-384
  • SHA-512
  • SHA-512/224
  • SHA-512/256