简体   繁体   English

PHP脚本到MD5分别哈希每行

[英]PHP Script to MD5 Hash Each Line Separately

I'm trying to create a script that can MD5 hash many lines (50,000+) quickly. 我正在尝试创建一个脚本,该脚本可以使MD5快速哈希很多行(50,000多个)。 I have a script I've been trying to make work, but it gives me different outputs sometimes and I can't figure out why. 我有一个我一直在努力工作的脚本,但是有时它会给我不同的输出,我不知道为什么。 Any ideas? 有任何想法吗?

  <?php  
     if(isset($_POST['btn'])){
            $value=$_POST['text'];
            $ids = explode(PHP_EOL, $value);
            $content = '';
            for ($i=0;$i<count($ids);$i++){
                $content .= md5($ids[$i]).'<br>';
            }

            echo nl2br($content);
     }
     ?>

When I try to hash: 当我尝试哈希时:

apples  
bananas  
oranges  
pineapples

It results in: 结果是:

265f78fc274d8428fd63dabc24400cb4  
63a63ddf49984e0f1cef336aeb6ca39c  
229b1cc78a248c6cea47fa95565dc9ca  
019b111ec0c13ed923922715bfb1670a  

But I should be getting: 但是我应该得到:

daeccf0ad3c1fc8c8015205c332f5b42  
ec121ff80513ae58ed478d5c5787075b  
91b07b3169d8a7cb6de940142187c8df  
019b111ec0c13ed923922715bfb1670a  

The lines in your input string are separated by \\r\\n , but on your server PHP_EOL is set to \\n . 输入字符串中的行用\\r\\n分隔,但是在服务器上, PHP_EOL设置为\\n So when you split the input into lines, there's a \\r at the end of each line except the last. 因此,当您将输入分成几行时,除了最后一行,每行的末尾都有一个\\r echo md5("apples\\r"); produces 265f78fc274d8428fd63dabc24400cb4 . 产生265f78fc274d8428fd63dabc24400cb4

Use trim() to remove extra whitespace. 使用trim()删除多余的空格。

$content .= md5(trim($ids[$i])).'<br>';

You have whitespaces in your input data. 输入数据中包含空格。 And don't collect all 50k+ hashes in memory. 并且不要在内存中收集所有超过50k的哈希。
Try this 尝试这个

<?php  
if(isset($_POST['btn'])){
    $value=$_POST['text'];
    $ids = explode(PHP_EOL, $value);
    for ($i=0;$i<count($ids);$i++){
        echo nl2br(md5(trim($ids[$i])));
    }
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM