Use cases

  • Usable on server side
  • Usable on Client side, if certain node-forge files are built and served to the Browser. See node-forge
  • Verifying if a string has been changed

node version

  • 8.11.2

JavaScript Version

  • ECMAScript 6 and higher

Installation

Example Code for JavaScript String Hashing using SHA-512, BASE64 and UTF-8 encoding

/**
 * An example for hashing of a String featuring:
 * - An out of the box working Example
 * - sha-512 digest
 * - Utf8 Encoding of Strings
 * - Base64 String encoding of digest
 * - Logging of exceptions
 */

var forge = require("node-forge"),
  winston = require("winston");

const logger = winston.createLogger({
  format: winston.format.combine(
    winston.format.splat(),
    winston.format.simple()
  ),
  transports: [
    new winston.transports.Console({
      format: winston.format.simple(),
      handleExceptions: true
    })
  ]
});

const demonstrateHash = () => {
  try {
    // replace with your actual Strings
    let exampleString =
      "Text that should be authenticated by comparing the hash of it!";
    let exampleString2 =
      "Text that should be authenticated by comparing the hash of it! - 2";

    let hashObject = forge.md.sha512.create();
    //update the hash object with data as often as required
    hashObject.update(exampleString);
    hashObject.update(exampleString2);

    let digest = forge.util.encode64(hashObject.digest().data);

    logger.info("Digest of the Strings: %s", digest);
  } catch (error) {
    logger.error(error.message);
  }
};

demonstrateHash();

// for unit testing purposes
module.exports = { demonstrateHash, logger };

References

Authors

Tobias Hirzel

Reviews

Tags: