Jump to content


Photo

Hashing multiple lines from a textarea


  • Please log in to reply
3 replies to this topic

#1 johnny042

johnny042

    Newbie

  • New Members
  • Pip
  • 6 posts

Posted 08 January 2013 - 05: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">
</form>


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:
apple
orange
apple

Sample output:
2f9c032ecf479087117e8d6f7fe1a84f
5b3ef7abd937c92e99d740df67182c1b
1f3870be274f6c49b3e31a0c6728957f



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 - 06:00 PM.


#2 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 08 January 2013 - 05: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

requinix

    Transforming Moderator

  • Moderators
  • 6,322 posts
  • LocationWA

Posted 08 January 2013 - 06:04 PM

echo md5("apple\r");


#4 johnny042

johnny042

    Newbie

  • New Members
  • Pip
  • 6 posts

Posted 08 January 2013 - 06: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

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com