Drop 0 from md5 () PHP if byte representation is less than 0x10

Using the md5 () function in PHP directly gives me a string. Before storing a row in the database, I want to remove the zeros 0 if they are in the byte representation of this hexadecimal element, and this byte representation is <0x10, and then save the row in the database.

How can I do this in PHP?

MD5 - PHP - Raw Value - catch12 - 214423105677f2375487b4c6880c12ae - This is what I get now. Below is the value that I want to save PHP in the database.

MD5 - Original value - catch12 - 214423105677f2375487b4c688c12ae

I wonder why? The MD5 code that I have in the Android application for login and registration. I did not add zeros for the condition if ((b & 0xFF) < 0x10) hex.append("0");. Works great. But the "Forgot Password" function on the site is PHP when a discrepancy occurs if the user resets the password. JAVA below.

byte raw[] = md.digest();  
StringBuffer hexString = new StringBuffer();
for (int i=0; i<raw.length; i++)
hexString.append(Integer.toHexString(0xFF & raw[i]));
v_password = hexString.toString();

Any help on the PHP side, so the mismatch does not happen, would be very helpful. I cannot change the application code because this will create problems for existing users.

Thank.

+5
source share
3 answers

Pass this β€œnormal” MD5 hash. It will parse it into separate pairs of bytes and cut leading zeros.

EDIT: Typo fixed

function convertMD5($md5)
{
    $bytearr = str_split($md5, 2);
    $ret = '';

    foreach ($bytearr as $byte)
        $ret .= ($byte[0] == '0') ? str_replace('0', '', $byte) : $byte;

    return $ret;
}

, , ( , 0x00 "0" ), :

function convertMD5($md5)
{
    $bytearr = str_split($md5, 2);
    $ret = '';

    foreach ($bytearr as $byte)
        $ret .= ($byte[0] == '0') ? $byte[1] : $byte;

    return $ret;
}
+2
$md5 = md5('catch12');
$new_md5 = '';
for ($i = 0; $i < 32; $i += 2)
{
  if ($md5[$i] != '0') $new_md5 .= $md5[$i];
  $new_md5 .= $md5[$i+1];
}

echo $new_md5;
0

For a strip of leading zeros (00-> 0, 0a-> a, 10-> 10)

function stripZeros($md5hex) {
  $res =''; $t = str_split($md5hex, 2);
  foreach($t as $pair) $res .= dechex(hexdec($pair));
  return $res;  
  }

To remove leading zeros and zero bytes (00-> nothing, 0a-> a, 10-> 10)

function stripZeros($md5hex) {
  $res =''; $t = str_split($md5hex, 2);
  foreach($t as $pair)  {
    $b = dechex(hexdec($pair));
    if ($b!=0) $res .= $b;
    }
  return $res;  
  }
0
source

All Articles