溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python通過hashlib庫將密碼hash后存入數(shù)據(jù)庫

發(fā)布時間:2020-06-21 16:39:07 來源:網(wǎng)絡(luò) 閱讀:306 作者:stone_syy 欄目:編程語言
  1. 通過Python將密碼hash后存入MySQL數(shù)據(jù)庫中,構(gòu)建一個自己的密碼庫
    MySQL版本:5.6
    python 版本:3.6
    pycharm:community 2019.2.4
  2. 創(chuàng)建相關(guān)表

    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后存入數(shù)據(jù)庫

    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('結(jié)束,共耗時:{:.2f}秒'.format(end-start))

    GitHub地址

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI