create a hashing function in javascript

To create a simple hashing function in JavaScript, you can use the built-in crypto module. Here's an example of a SHA-256 hashing function:

index.tsx
const crypto = require('crypto');

function hash(input) {
  const hash = crypto.createHash('sha256');
  hash.update(input);
  return hash.digest('hex');
}

const hashedData = hash('example data');
console.log(hashedData); // prints "7c860fbd72937f347216a74c283761dde7ab710350f45a2f88c83e8d0196f9ea"
299 chars
11 lines

This function uses the SHA-256 algorithm to compute a hash of the input data, then returns the hash as a hex string. You can call this function on any data you want to hash, such as passwords or sensitive information, to hide the actual values from attackers. By using a hashing function, you can keep important data stored securely and can also verify the integrity of the data during transmission.

gistlibby LogSnag