2

Ive been trying to write the md5-digest algorithm in erlang and have no clue how to implement this step,

1. creating 16 octet MD5 hash of X where X is a string.

Can someone help ?

Does this mean this:

Create a 16 byte(32-hex digits) of base - 8(octet) which is md5 of X. ?

Thank you!

sad
  • 820
  • 1
  • 9
  • 16
  • The answer is yes. Note that a string could also mean an octet string or bit string (although most libraries only handle bytes, but in principle bit strings are also possible). – Maarten Bodewes Sep 20 '16 at 20:11

2 Answers2

9

Using crypto module and hash function, you can calculate the MD5 which is a 16 byte digest algorithm.

crypto:hash(Type, Data) -> Digest

Type = md5

Data = iodata()

Digest = binary()

It gets a md5 atom as Type and an iodata() as Data, and returns a binary() as Digest. Following code snippet is a simple example:

crypto:hash(md5, "put-your-string-here").

Check crypto documentation for more information.

Also for converting the returned binary value to hex string, there is no function in standard library, but it is as simple as few lines of code which is well explained in this thread.

Community
  • 1
  • 1
Hamidreza Soleimani
  • 2,504
  • 16
  • 19
  • Thanks for the answer. So I know that it returns a binary format so I created a function that gives the equivalent string representation like this: md5:hexstring(crypto:hash(md5, "hello")). "5d41402abc4b2a76b9719d911017c592" However, Im not sure what 16 octet md5 hash of X. Thanks – sad Sep 19 '16 at 18:39
  • @Atomic_alarm I got that. since erlang returns binary, we should convert to hex-string. I was able to do that using the io_lib:format to convert to hex. but my concern is this: creating 16 octet MD5 hash of X where X is a string. Can you explain what that means ? thanks – sad Sep 19 '16 at 18:57
  • @sad There is no function for converting binary data type to Hex string, but it is as simple as a few lines of code which is well explained [here](http://stackoverflow.com/questions/3768197/erlang-ioformatting-a-binary-to-hex). – Hamidreza Soleimani Sep 19 '16 at 19:02
  • `hexstring(<>) -> lists:flatten(io_lib:format("~32.16.0b", [X])).` – sad Sep 19 '16 at 19:06
  • 1
    Consider [octet](https://en.wikipedia.org/wiki/Octet_(computing)) as a synonym of byte. 16 octet == 16 bytes [length]. I.e hash of 16 byte length. I.e. string of 32 chars (as every byte is represented with 2 hex digits) – Lol4t0 Sep 20 '16 at 19:56
0

This md5 module from the epop package calculates the md5 and returns it as a hex string.

epop_md5:string("put-your-string-here").
N. Hoogervorst
  • 146
  • 1
  • 5