温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python通过hashlib库将密码hash后存入数据库

发布时间:2020-06-21 16:39:07 来源:网络 阅读:298 作者:stone_syy 栏目:编程语言
  1. 通过Python将密码hash后存入MySQL数据库中,构建一个自己的密码库
    MySQL版本:5.6
    python 版本:3.6
    pycharm:community 2019.2.4
  2. 创建相关表

    CREATE TABLE society.18wangcode_sha1_hash(id INT(9) AUTO_INCREMENT PRIMARY KEY,
    pwd VARCHAR(60) NOT NULL,
    hash_values VARCHAR(40) NOT NULL);

  3. Python代码

    通过hashlib库将密码hash后存入数据库

    import mysqlx.connection
    import time
    import hashlib
    import mysql.connector
    print('begin'.center(30, '*'))
    print('进行中,请稍等。。。。。。。。')
    start = time.time()
    conn = mysql.connector.connect(host='192.168.137.239', user='admin', password='123456', database='society')
    cursor = conn.cursor()
    
    def hash(pwd):
        method = hashlib.sha1()
        method.update(pwd.encode('utf-8', errors='ignore'))
        return method.hexdigest()
    
    try:
        with open(r'C:\Users\11826\Desktop\18万条密码.txt', 'r', encoding='utf-8', errors='ignore') as file:
            for password in file:
                cursor.execute('insert into society.18wangcode_sha1_hash (pwd, hash_values) values (%s, %s)',
                               (password, hash(password)))
    except:
        print('error')
    finally:
        cursor.close()
        conn.commit()
        conn.close()
    end = time.time()
    print('结束,共耗时:{:.2f}秒'.format(end-start))

    GitHub地址

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI