In order to create a unique hash from a specific string, it can be implemented using their own string to hash converting function. It will return the hash equivalent of a string. Also, a library named Crypto can be used to generate various types of hashes like SHA1, MD5, SHA256 and many more.
Note: The hash value of an empty string is always zero.
Example 1:
<!DOCTYPE html> <html> <head> <title> How to convert hash from string in JavaScript ? </title> <style> h1 { color: green; } </style> </head> <body> <center> <h1>GeeksforGeeks</h1> <h3>Creating hash from string in JavaScript?</h3> <p>The hash value of string "GeeksforGeeks" is .</p> <p id="geek"></p> <script> // Convert to 32bit integer function stringToHash(string) { var hash = 0; if (string.length == 0) return hash; for (i = 0; i < string.length; i++) { char = string.charCodeAt(i); hash = ((hash << 5) - hash) + char; hash = hash & hash; } return hash; } // String printing in hash var gfg = "GeeksforGeeks" document.getElementById("geek").innerHTML = stringToHash(gfg); </script> </center> </body> </html> |
Output:

Example 2:
<script> // Importing 'crypto' module const crypto = require('crypto'), // Returns the names of supported hash algorithms // such as SHA1,MD5 hash = crypto.getHashes(); // Create hash of SHA1 type x = "Geek" // 'digest' is the output of hash function containing // only hexadecimal digits hashPwd = crypto.createHash('sha1').update(x).digest('hex'); console.log(hash); </script> |
Output:
321cca8846c784b6f2d6ba628f8502a5fb0683ae
Recommended Posts:
- How to remove hash from window.location with JavaScript without page refresh ?
- Node | urlObject.hash API
- PHP | md5(), sha1(), hash() Functions
- HTML | DOM Location hash Property
- C# | How to get hash code for the specified key of a Hashtable
- Perl Hash
- hash command in Linux with examples
- HTML | DOM Anchor hash Property
- Perl | Hash Operations
- Perl | Hash in Scalar and List Context
- Perl | Use of Hash bang or Shebang line
- How to Secure hash and salt for PHP passwords ?
- How to read a hash with an “&” sign in the URL ?
- What is HMAC(Hash based Message Authentication Code)?
- Perl - Creating a Hash from an Array
- Create a string with multiple spaces in JavaScript
- How to create a function from a string in JavaScript ?
- Object.create( ) In JavaScript
- How to create a style tag using JavaScript?
- How to create two dimensional array in JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


