Search code examples
phpparametersoperatorsmd5or-operator

What does the | operator do when it appears in the md5() parameters?


In a piece of code originally not written by me, I found this:

md5($variable1|$variable2|$variable3)

I know the code is encrypting something using md5. What I am trying to understand is what the | operator is doing when used in this way.

UPDATE 1: It is not concatenating of course. It would make sense to me something like this:

$finalString = $variable1.$variable2.$variable3;
md5($finalString);

But | is the OR operator. I am trying to understand what md5($variable1|$variable2|$variable3) does, using the | operator as part of the parameters within the md5() method. This is PHP code by the way.


Solution

  • The pipe operator | indicates that a bitwise OR is taking place between your three variables.

    However, if $variable3 is textual as you indicate, then I do not understand why you would be doing this.

    Take a look at the code below. It utilises the example data you posted in the comments:

    $variable1 = 2432;
    $variable2 = 3234322;
    $variable3 = 'adtw2GEt4PrPghhfLApae';
    
    echo '1: ' . $variable1 . '<br />';
    echo '2: ' . $variable2 . '<br />';
    echo '3: ' . $variable3 . '<br /><br />';
    
    echo '1|2: ' . ($variable1 | $variable2) . '<br />';
    echo '1|2|3: ' . ($variable1 | $variable2 | $variable3) . '<br /><br />';
    
    echo '1.2: ' . ($variable1 . $variable2) . '<br />';
    echo '1.2.3: ' . ($variable1 . $variable2 . $variable3) . '<br /><br />';
    
    echo 'md5 1|2|3: ' . md5($variable1|$variable2|$variable3) . '<br />';
    echo 'md5 1.2.3: ' . md5($variable1.$variable2.$variable3) . '<br />';
    

    You'll see straight away that 1|2 gives the same result as 1|2|3, meaining that the second bitwise OR does nothing (because it is a string):

    1|2: 3234706
    1|2|3: 3234706
    

    If you treated all three variables as a string then you will get a totally different MD5 hash, as you'd expect:

    md5 1|2|3: fdea81fcefba5a598cf3124d7dbf3854
    md5 1.2.3: 8cc6383034ed459ad7a135fcb8cb86de
    

    So md5($variable1|$variable2|$variable3); is the same as md5(3234706); (which is just $variable1|$variable2) and doesn't even use the third variable.

    MD5 is not encryption: I notice that you mentioned encryption in the comments. MD5 is not encryption, it is a hashing algorithm, which is altogether different.