0

I have multiple strings (containing alphanumeric characters) such as:

  • 523d33f0dc94a247536ea849b4834a77
  • 12hjh89nd9009idfn90kkui897oiuwer23
  • 3284uioen89ji89ji89ddf64f1f167678

And so on.

I want to convert these strings to a hash of less than or equal to 20 bytes. Is there a JS function which can do this? I am specifically looking for the output to be less than or equal to 20 bytes.

  • 1
    Does this answer your question? [Generate a Hash from string in Javascript](https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript) – FZs Mar 13 '20 at 13:04
  • No capital letters? Or case insensitive? Any length? – trincot Mar 13 '20 at 19:58

1 Answers1

1

Try using : First install bower (A package manager for web)

npm install -g bower

Go to the location where the project is located in the terminal and run following command:

bower install crypto-js

Add the following code in HTML file:

<script type="text/javascript" src="path-to/bower_components/crypto-js/crypto-js.js"></script>
<script type="text/javascript">
    var hash = CryptoJS.MD5("message");// returns 16 bytes hash code
    console.log(hash.toString());
</script>

Here path-to represents the relative path to bower_components folder that was created by the execution of bower install crypto-js

Hope this helps. For reference visit https://github.com/brix/crypto-js

AyushKatiyar
  • 1,000
  • 8
  • 15
  • MD5 has been broken for 15 years now. Cryptographers have been screaming at people to stop using it for anything for over a decade. Try https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest instead. – btilly Mar 13 '20 at 14:06
  • 2
    @btilly Depends on OP's use case. MD5 is [unsuitable for crypto](https://security.stackexchange.com/questions/15790/why-do-people-still-use-recommend-md5-if-it-has-been-cracked-since-1996) but at the same time [ideal for other purposes](https://idiallo.com/blog/when-to-use-md5), for one and the same reason: it's just so bloody fast. – Ruud Helderman Mar 13 '20 at 15:20
  • 1
    @btilly, the OP has not specified anything about crypto needs. Just compression. – trincot Mar 13 '20 at 19:51