Jump to content


Hashing multiple lines from a textarea

  • Please log in to reply
3 replies to this topic

#1 johnny042

  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 08 January 2013 - 10:55 PM

So I have a need to generate md5 hashes for multiple lines out of a textarea.
The problem is all of the individual "\n" lines are returning incorrect md5 outputs EXCEPT for the last line (it always returns the correct md5 output)

Here is the code and a sample input/output test at the bottom:

The HTML form

<form action="hash.php" method="post">
<textarea name="hashed" rows=20 cols=41></textarea></br>
<input type="submit" value="Generate">

PHP code used to take individual \n lines and generate the individual line md5 output

$data = $_POST['hashed'] ;
foreach(explode("\n", $data) as $line) {
$hashedData = hash('md5',$line, FALSE);
echo '<strong>'. $hashedData .'</strong></br>';

1. You'll notice the first line (apple) generates "md5(apple + something else)" (I'm thinking the actual \n is generated as part of apple hence wrong output)
2. Same thing happens to orange
3. The third line (apple) generates the correct "md5(apple)" output

Sample input:

Sample output:

Any suggestions or a possible workaround?

A few things already tried:
- using commas as a separator but it didn't work out too well :(
- tried reverse-md5'ing the wrong output but no one has yet to rainbow it
- Clean $line from enter \n spaces and %20 spaces, no dice
$clean = str_replace(' ','',$str);

Edited by johnny042, 08 January 2013 - 11:00 PM.

#2 Jessica

  • Staff Alumni
  • This is not my name.
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 08 January 2013 - 10:57 PM

Did you try trim?
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#3 requinix

  • Administrators
  • Lazy Administrator
  • 9,374 posts
  • LocationWA

Posted 08 January 2013 - 11:04 PM

echo md5("apple\r");

The Reimann Zeta Function Trolley Problem | "Summer is when I, the great ice fairy, can show my true power!"

#4 johnny042

  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 08 January 2013 - 11:24 PM

echo md5("apple\r");

Did you try trim?

BOTH were great replies !! It was Jessica for the win,

Thank you Based Jessica :happy-04: :happy-04:

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users