自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何使用Python制作隨機(jī)密碼生成器

譯文
開發(fā)
密碼安全愁禿頭?不妨構(gòu)建一個(gè)安全的隨機(jī)密碼生成器

作者 | Teri Eyenike

譯者 | 布加迪

審校丨諾亞

  每個(gè)網(wǎng)站都有安全接口的某種表單,需要用戶驗(yàn)證身份。這些表單常常使用你的電子郵件和密碼來訪問網(wǎng)站。登錄時(shí)使用安全密碼對(duì)于防止壞人訪問你的帳戶至關(guān)重要。

  本文教你如何使用Python創(chuàng)建一個(gè)隨機(jī)密碼生成器,只需生成結(jié)合字母、數(shù)字和符號(hào)的加密字符,從而使密碼難以被破解或猜中。

  不妨構(gòu)建一個(gè)安全的隨機(jī)密碼生成器。

入手準(zhǔn)備

  要構(gòu)建一個(gè)隨機(jī)密碼生成器,我們將使用這種方法:

  寫出所有可接受的密碼字符類型,比如字母、數(shù)字和符號(hào)。

  讓用戶能夠?yàn)樯傻拿艽a輸入所需數(shù)量的字母、符號(hào)和數(shù)字。

  隨機(jī)化字符順序,使密碼難以被猜中。

創(chuàng)建隨機(jī)密碼生成器

  如你所知,互聯(lián)網(wǎng)上的一些應(yīng)用程序會(huì)在你創(chuàng)建新帳戶時(shí)建議使用隨機(jī)密碼。隨機(jī)字符由你決定,可以長(zhǎng)達(dá)八個(gè)字符。

  ??創(chuàng)建一個(gè)新文件main.py??,編寫應(yīng)用程序的腳本。

# main.py
letters = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
]
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

print("Welcome to the PyPassword Generator!")

nr_letters = int(input("How many letters would you like in your password?\n"))

nr_symbols = int(input(f"How many symbols would you like?\n"))

nr_numbers = int(input(f"How many numbers would you like?\n"))

  上面代碼塊中的字符組成了列表中顯示的密碼生成器的組合。

  接下來確保用戶可以輸入數(shù)字,這個(gè)整數(shù)代表在最終輸出顯示并使用變量聲明時(shí)字符出現(xiàn)的次數(shù)。

  \n:表示輸入值會(huì)進(jìn)入到下面一行

  現(xiàn)在,不妨更新其余代碼。復(fù)制并粘貼以下內(nèi)容:

# main.py
# Password Generator Project

import random # add this

# letters, numbers, and symbols lists

# users' input for the amount of characters

# add these below
password_list = []

for char in range(1, nr_letters + 1):
password_list.append(random.choice(letters))

for char in range(1, nr_symbols + 1):
password_list.append(random.choice(numbers))

for char in range(1, nr_numbers + 1):
password_list.append(random.choice(symbols))

random.shuffle(password_list)

  代碼塊執(zhí)行以下操作:

  • 導(dǎo)入用于生成隨機(jī)數(shù)的內(nèi)置random模塊。
  • 使用變量password_list創(chuàng)建空列表[]。
  • 遍歷range函數(shù)中的數(shù)字,從起始索引創(chuàng)建數(shù)字序列,以最后一個(gè)索引加1結(jié)束。
  • 接下來,為空列表附加內(nèi)容,使用random.choice()方法為每一個(gè)被聲明為變量的字符獲取隨機(jī)選擇的元素。
  • 使用.shuffle()方法對(duì)新創(chuàng)建的password_list進(jìn)行改換,每次輸入新密碼就改變?cè)氐奈恢谩?/li>

將密碼列表轉(zhuǎn)換成字符串

  復(fù)制并更新以下代碼:

# main.py

# import

# letters, numbers, and symbols lists

# users' input for the amount of characters

# randomize characters

# add this
password = ""
for char in password_list:
password += char

# convert list to string
pwd = ''.join(password_list)
print(f"Your random password to use is: {pwd}")

  將列表轉(zhuǎn)換成字符串的過程如下:

  • 創(chuàng)建空字符串變量password。
  • 使用for關(guān)鍵字遍歷密碼列表。
  • 將密碼字符串與循環(huán)的char變量連接起來。
  • 使用.join()方法將列表迭代由密碼列表改為字符串。
  • 最后,使用f-strings顯示密碼的結(jié)果。

  代碼最終結(jié)果:

# main.py

import random

letters = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
]
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

print("Welcome to the PyPassword Generator!")
nr_letters = int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))

password_list = []

for char in range(1, nr_letters + 1):
password_list.append(random.choice(letters))

for char in range(1, nr_symbols + 1):
password_list.append(random.choice(numbers))

for char in range(1, nr_numbers + 1):
password_list.append(random.choice(symbols))

random.shuffle(password_list)

password = ""
for char in password_list:
password += char
print("char", char)

# convert list to string
pwd = ''.join(password_list)
print(f"Your random password to use is: {pwd}")

結(jié)語

  在本文中你開發(fā)了一個(gè)應(yīng)用程序,該應(yīng)用程序每次生成不同的隨機(jī)密碼,從而支持動(dòng)態(tài)使用環(huán)境,生成盡可能多的密碼。

  原文鏈接:

  ??https://hackernoon.com/how-to-create-a-random-password-generator-using-python??

責(zé)任編輯:張潔 來源: 51CTO技術(shù)棧
相關(guān)推薦

2011-05-19 11:30:00

密碼密碼生成器

2024-11-01 15:51:06

2011-12-23 13:42:05

JavaScript

2009-07-31 09:00:44

ASP.NET生成隨機(jī)

2022-12-15 08:49:58

ReactQR生成器

2024-12-23 08:00:00

2015-03-26 11:51:22

2018-11-19 10:10:51

Python數(shù)據(jù)庫隨機(jī)生成器

2014-07-23 10:07:34

2017-09-06 09:26:03

Python生成器協(xié)程

2021-12-04 22:07:44

Python

2021-12-10 09:45:19

生成器配置代碼

2017-05-18 10:00:40

Linuxpandom隨機(jī)數(shù)生成器

2018-02-07 08:53:25

Linux命令隨機(jī)密碼

2020-12-20 10:04:44

Qrcode二維碼生成器QR Code

2010-09-07 16:31:17

SQL語句insert

2021-04-22 21:15:38

Generator函數(shù)生成器

2022-02-15 10:30:58

UUID

2021-11-04 09:00:00

JavaSpring BootURL

2020-10-05 21:57:43

Python生成器可迭代對(duì)象
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)