Sampath Sampath - 3 months ago 20
AngularJS Question

Generate HASHBYTES inside the JavaScript file

I have to generate a secure token.This token will be consumed by WebApi. I'm requesting that Webapi through JavaScript file. It is a SPA app. So my question is, can I generate below mentioned token without using the Sql server response ?

The SQL syntax in Microsoft SQL Server to generate such a token is:

SELECT HASHBYTES('MD5', convert(varchar,getdate(),112) + Mysc@re4+)

Answer

You can generate an MD5 hash in JavaScript using the Crypto-JS library:

<html>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/core.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/md5.js"></script>
  <body>
    <script>
      var test = CryptoJS.MD5("string to hash");
      alert(test);
    </script>
  </body>
</html>
Comments