0

I have the following Oracle function that is used for checking user passwords, but I want to switch to MySQL. I'm looking for a set of MySQL functions that will produce the equivalent result.

FUNCTION encpasswd (ID IN VARCHAR, passwd IN VARCHAR)

   RETURN VARCHAR

IS

   v_result   VARCHAR2 (4000);

BEGIN

   v_result := DBMS_OBFUSCATION_TOOLKIT.md5 (input_string => ID || passwd);

   --RETURN RAWTOHEX (v_result);

   RETURN v_result;

END encpasswd;
2

2 Answers 2

0

MySQL's MD5 function returns a string of 32 hexadecimal characters. It's marked as a binary string but it isn't the 16 byte binary data one would expect.

So to fix it, this string must be converted back to the binary data for example:

SELECT hex(aes_encrypt('test-data', unhex(MD5('test_key'))));

The result is:

8FCA326C25C8908446D28884394F2E22

SQLFiddle for MySQL

It's again a string of 32 hexadecimal characters. But otherwise it's the same result as with Oracle.

And BTW:

  • MySQL uses PKCS7 padding.
  • PKCS5 padding and PKCS7 padding are one and the same. So the Oracle padding option is correct.
  • MySQL uses ECB block cipher mode. So you'll have to adapt the code accordingly. (It doesn't make any difference for the first 16 bytes.)
  • MySQL uses no initialization vector (the same as your Oracle code).
  • MySQL uses a non-standard folding a keys. So to achieve the same result in MySQL and Oracle (or .NET or Java), only use keys that are 16 byte long.
2
  • So in this case, if id=1 and passwrd=foo, what would the statement look like? SELECT hex(aes_encrypt('1', unhex(MD5('foo')))); Oct 8, 2013 at 4:14
  • Yes. More likely. You can test it in your oracle.
    – Mark
    Oct 8, 2013 at 5:09
0

Mysql has a native "md5" function.

SELECT md5(concat(id, passwd)) FROM yourtable

As a function:

CREATE FUNCTION `encpasswd`(`ID` VARCHAR(250), `passwd` VARCHAR(250)) RETURNS VARCHAR(250)
BEGIN
    RETURN md5(concat(ID,passwd))
END

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.