PHP高级工程师|全栈工程师|WX: Aben_Ring

md5密码入库

<?php
//连接数据库
$pdo = new PDO('mysql:host=localhost;dbname=md5;charset=UTF8', 'root', '');
# 设置为false,保证返回数据的结果中,字段类型和数据库中字段类型一致。否则int字段的数据,返回结果可能是字符串类型
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);

//配置项

# 定义字符串
$str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_@!#%;.";
// $str = "abcde";
# 设置密码长度
$pass_len = 5;

//N进制算法
function createNext($str,$arr){
    
    $tmpStr = '';
    $isAdd = false;

    for($i = strlen($str)-1;$i>=0;$i--){

        $char = substr($str,$i,1);


        if($i===strlen($str)-1){
            foreach ($arr as $key => $value) {              
                if($value === $char){   
                    if($key+1 < count($arr)){
                        $tmpStr = $arr[$key+1].$tmpStr;                     
                    }else{
                        $tmpStr = $arr[0].$tmpStr;                  
                        $isAdd = true;
                    }               
                }
            }
        }else{
            if($isAdd){
                foreach ($arr as $key => $value) {              
                    if($value === $char){

                        if($key+1 < count($arr)){
                            $tmpStr = $arr[$key+1].$tmpStr;
                            $isAdd = false;
                        }else{
                            $tmpStr = $arr[0].$tmpStr;  
                            $isAdd = true;
                        }                   
                    }
                }
            }else{
                $tmpStr = $char.$tmpStr;
            }
        }   
    }

    if($isAdd){
        $tmpStr = $arr[1].$tmpStr;
    }
    return $tmpStr;
}

// $str = "abcde";
$str_len = strlen($str);
$arr = [];
for($i=0;$i<$str_len;$i++){
    $arr[] = substr($str,$i,1);
}

//批量插入隔断,数据达到这个数量的时候,执行一次SQL语句
$pageSize = pow($str_len,2);    


# 进制算法的第一个数字(字母)
// $firstStr = "aaaaaa";
$firstStr = "";
$firstWord = substr($str,0,1);
for($k = 0 ; $k < $pass_len ; $k++ ){
    $firstStr .= $firstWord;
}


$total = pow($str_len,$pass_len);
$values = ",('".$firstStr."','".md5($firstStr)."','".md5(md5($firstStr))."','".sha1($firstStr)."')";

for ($i=2; $i <= $total; $i++) {
    $res = createNext($firstStr,$arr);
    $firstStr = $res;
    
    $values .= ",('".$firstStr."','".md5($firstStr)."','".md5(md5($firstStr))."','".sha1($firstStr)."')";
    if($i%$pageSize == 0){
        # 分表。控制每张表中数据的数据量
        $table = 'md5_'.$pass_len."_".(int)($i/(pow($str_len,6)));
        createTable($table);
        $sql = "INSERT INTO ".$table."(`p`,`md5`,`md5md5`,`sha1`) values ".trim($values,",").";";
        $stmt = $pdo->prepare($sql);
        if (!$stmt->execute()) {
            echo "执行SQL失败:".$sql."<br/>";
            die;
        }
        $values = "";
    }
}


function createTable($table){
    global $pdo;
    $sql = "CREATE TABLE IF NOT EXISTS ".$table."( 
        `id` int(12) NOT NULL AUTO_INCREMENT, 
        `p` VARCHAR (32) DEFAULT NULL, 
        `md5` VARCHAR (32) DEFAULT NULL, 
        `md5md5` VARCHAR (32) DEFAULT NULL,
        `sha1` VARCHAR (40) DEFAULT NULL,
        PRIMARY KEY (`id`) 
    ) ENGINE = MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET = utf8";
    $stmt = $pdo->prepare($sql);
    if (!$stmt->execute()) {
        echo "创建数据表失败:".$sql."<br/>";
        die;
    }
}

 

写入文件。测试过一个5位数的md5加密文件,大小占到164G。和mysql数据库差不多

//配置项

# 定义字符串
// $str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_@!#%;.";
$str = "abcde";
# 设置密码长度
$pass_len = 6;

//N进制算法
function createNext($str,$arr){
    
    $tmpStr = '';
    $isAdd = false;

    for($i = strlen($str)-1;$i>=0;$i--){

        $char = substr($str,$i,1);


        if($i===strlen($str)-1){
            foreach ($arr as $key => $value) {              
                if($value === $char){   
                    if($key+1 < count($arr)){
                        $tmpStr = $arr[$key+1].$tmpStr;                     
                    }else{
                        $tmpStr = $arr[0].$tmpStr;                  
                        $isAdd = true;
                    }               
                }
            }
        }else{
            if($isAdd){
                foreach ($arr as $key => $value) {              
                    if($value === $char){

                        if($key+1 < count($arr)){
                            $tmpStr = $arr[$key+1].$tmpStr;
                            $isAdd = false;
                        }else{
                            $tmpStr = $arr[0].$tmpStr;  
                            $isAdd = true;
                        }                   
                    }
                }
            }else{
                $tmpStr = $char.$tmpStr;
            }
        }   
    }

    if($isAdd){
        $tmpStr = $arr[1].$tmpStr;
    }
    return $tmpStr;
}

$str_len = strlen($str);
$arr = [];
for($i=0;$i<$str_len;$i++){
    $arr[] = substr($str,$i,1);
}

//批量插入隔断,数据达到这个数量的时候,执行一次SQL语句
$pageSize = pow($str_len,2);    


# 进制算法的第一个数字(字母)
// $firstStr = "aaaaaa";
$firstStr = "";
$firstWord = substr($str,0,1);
for($k = 0 ; $k < $pass_len ; $k++ ){
    $firstStr .= $firstWord;
}


$total = pow($str_len,$pass_len);
$values = $firstStr." ".md5($firstStr)." ".md5(md5($firstStr))." ".sha1($firstStr);

for ($i=2; $i <= $total; $i++) {
    $res = createNext($firstStr,$arr);
    $firstStr = $res;
    $values .= " ".$firstStr." ".md5($firstStr)." ".md5(md5($firstStr))." ".sha1($firstStr);
    if($i%$pageSize == 0){
        # 分表。控制每张表中数据的数据量
        $myfile = 'md5_'.$pass_len."_".(int)($i/(pow($str_len,6)));
        if (file_exists($myfile)) {
            file_put_contents($myfile, $values, FILE_APPEND);
        }else{
            $thisfile = fopen($myfile, "w") or die("Unable to open file ".$myfile);
            fwrite($thisfile, $values);
        }
        $values = "";
    }
}

 

posted @ 2018-08-20 09:43  懒牛拉车  阅读(523)  评论(0编辑  收藏  举报