How to hash a PHP variable

Discussion in 'PHP' started by @nibb, May 8, 2011.

  1. #1
    Hi, im trying this and it doesnt seem to work.

    I know it must be an innocent newbie error.

    
    $pass = wupi
    $key = hash('SHA256', '$pass');
    
    PHP:
    It seems the result is always the same and the hash function assumes the text to hash is $pass instead of "wupi" or what I ever i pass to that variable.
     
    @nibb, May 8, 2011 IP
  2. artus.systems

    artus.systems Well-Known Member

    Messages:
    87
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    103
    #2
    Try this....
    $pass="wupi"; // <- inside quote
    $key = hash('SHA256', '$pass');
    echo $key;
    PHP:
     
    Last edited: May 8, 2011
    artus.systems, May 8, 2011 IP
  3. @nibb

    @nibb Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    Please excuse me, my original code had the quotes and ; on the end. I just edited when I posted the reply in the forum.

    Here is actually how im trying to pass it:

    $pass = $_GET["password"];
    echo $pass;
    $key = hash('SHA256', '$pass');


    The echo line 2 is just for testing. That is not needed.

    I want to change the key on the fly with a get command but the generated output is alwas the same.

    I also tried putting the get directly on the hash or converting and several other ways but it doesn't seem to work.
     
    @nibb, May 8, 2011 IP
  4. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #4
    Why did you put the $pass variable between single quotes? You should use double quotes, better yet forget the quotes and use the variable as is. Now it should generate the hash of the value instead of the string '$pass'.

    $key = hash('SHA256', $pass);
    PHP:
     
    bogi, May 9, 2011 IP
  5. @nibb

    @nibb Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5

    Thanks bogi, that worked !
     
    @nibb, May 15, 2011 IP