javascript

jamesBB

·

Easy JS hashing

·

JavaScript

·

Total Size: 1.91 KB

·

·

Created: 4 years ago

·

Edited: 4 years ago

<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>
<!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> 

1 bit

1170 views

Are you sure you want to delete?