[solved] MD2 algorithm on Python: cannot spot a problem | Sololearn: Learn to code for FREE!


[solved] MD2 algorithm on Python: cannot spot a problem

I have tried implementing MD2 algorithm on Python lately (I know that there is a built-in module with hash algorithms, I wanted to try to implement it myself to understand hashes). I was following a reference that seems to be official: https://datatracker.ietf.org/doc/html/rfc1319 But I still get completely different answer. Can you help me spot what I did wrong while implementing it, please? My code: https://code.sololearn.com/c1fURe89V853 Note: byte permutation S is correct, steps 2 & 4 seem to be also.

10/17/2021 6:45:39 AM


2 Answers

New Answer


The padding should be 'pad' times the 'pad' (e.g. for 3 it's '3 3 3'). And when its already a multiple of 16 padding should still be appended. Also the original rfc proposal has a typo in the pseudocode that you followed, needs an extra xor (see link) Other than that your code is correct. You can also use the .hex() method of bytes object. https://www.rfc-editor.org/rfc/inline-errata/rfc1319.html This should help too: https://nickthecrypt.medium.com/cryptography-hash-method-md2-message-digest-2-step-by-step-explanation-made-easy-with-python-10faa2e35e85


Examples (no space left in question description, sorry for that): 1. Input: The quick brown fox jumps over the lazy dog Expected output: 03d85a0d629d2c442e987525319fc471 Little Endian output: 8a82ac5370508b205e7d87d5256cfed8 Big Endian output: d8fe6c25d5877d5e208b507053ac828a 2. Input: Expected output: 8350e5a3e24c153df2275c9f80692773 Little Endian output: ae805225ab4a3cd2c85aceacef534c81 Big Endian output: 814c53efacce5ac8d23c4aab255280ae Source for expected output: https://en.wikipedia.org/wiki/MD2_(hash_function)#MD2_hashes . Random online hash generator (https://emn178.github.io/online-tools/md2.html ) generates Expected output, so I guess that there is indeed a problem in my code.