1

I want to hash a string, but I don't want to implement the hashing algorithm. Is there a built-in JavaScript function or a dependable npm package to hash strings? If so, how do I use it to hash a string?

For example, if I have

const string = "password";

I want to run it through some kind of ready-made function or method, like this

const hash = hashFunction( string );

then get a hash from it, like this

console.log( hash ); //5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8

NOTE: I'm aware that this question is similar to this question, but the chosen answer there implements hashing. I'm specifically looking for ready-built dependable functions or methods that hash.

Rolazar
  • 120
  • 1
  • 8
  • You want a cryptographic hash? There are libraries like [CryptoJS](https://cryptojs.gitbook.io/docs/) that can do that. – Spectric Dec 16 '22 at 20:55
  • https://openbase.com/categories/js/best-javascript-hashing-libraries?vs=argon2%2Cobject-hash%2Cmd5 – Bench Vue Dec 16 '22 at 20:57

1 Answers1

2

I believe can simply use the built-in object.

It provides cryptographic functionality that includes the ability to hash strings.

const crypto = require('crypto');
const string = "password";
const hash = crypto.createHash('sha256').update(string).digest('hex'); 

console.log(hash);
Output: 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8

Or as what @Spectric has mentioned, you can use the CryptoJS library.

const CryptoJS = require("crypto-js");

const string = "password";
const hash = CryptoJS.SHA256(string).toString();

console.log(hash);
Output: 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8

I hope this helps! :)

Vense
  • 53
  • 5