Khởi tạo mật khẩu ngẫu nhiên với code Python

Khởi tạo mật khẩu ngẫu nhiên với code PythonCuongquach.com | Làm thế nào để bạn có thể tạo ra khởi tạo mật khẩu ngẫu nhiên gồm kí tự + số ngẫu nhiên giúp chúng ta có 1 mật khẩu mạnh mẽ bằng code python nào? Chúng ta đều biết một mật khẩu mạnh, là phải đạt tiêu chí tối thiểu gồm độ dài chuỗi thấp nhất là 8 và phải trộn lẫn giữa số, chữ thường và chữ hoa với nhau. Vậy để làm được điều này, chúng ta có thể sử dụng ngay module ‘random‘ và ‘string‘ trợ giúp.

Ví dụ trong bài này sử dụng Python 2.7, Python 3.x syntax sẽ khác.

1. Sử dụng module string

– Import module string vào nào.

>>> import string

– Xuất ra đủ 1 dãy alphabet chữ thường và chữ hoa.

>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

– Xuất ra các kí tự đặc biệt.

>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

– Xuất ra số.

>>> string.digits
'0123456789'

 
– Khá là đầy đủ cho việc khởi tạo mật khẩu an toàn chưa nào. Mình thì hạn chế sử dụng thêm kí tự đặc biệt trong phần generate do sẽ có nhiều hệ thống không chấp nhận 1 vài kí tự đặc biệt.

– Nhưng mà in ra vậy rồi thì chúng ta phải làm gì để lựa chọn các kí tự hoặc số 1 cách ngẫu nhiên cơ chứ ? Chính vì điều đó mà chúng ta sẽ sử dụng module ‘random‘ của Python.

2. Sử dụng module random

– Module random có rất là nhiều phương thức method hỗ trợ như tạo số ngẫu nhiên,.. nhưng ta cần ở đây trong nội dung bài này chỉ là phương thức chọn ngẫu nhiên kí tự ta đã chuẩn bị sẵn.

>>> import string, random
>>> chars = string.letters
>>> random.choice(chars)
't'
>>> random.choice(chars)
'V'
>>> random.choice(chars)
'J'
>>> random.choice(chars)
'a'

– Tới đây bạn đã có thể hình dung ra code cơ bản để khởi tạo mật khẩu ngẫu nhiên chưa nào

3. Code khởi tạo mật khẩu ngẫu nhiên

– Mình sẽ fix độ dài của mật khẩu là nằm trong khoảng từ 8 đến 15 trong code mẫu đơn giản.
– Cho chạy ngẫu nhiên độ dài , vòng lặp for xuất các kí tự ngẫu nhiên trong biến ‘chars_fixed‘ và nối chúng lại với nhau.

import string
import random

chars_fixed = string.letters + string.digits
min_size_pass = 8
max_size_pass = 15

password = "".join(random.choice(chars_fixed) for x in range(random.randint(min_size_pass, max_size_pass)))
print "This is your password : %s" % password

Output:

This is your password : DZ2b6wjXj8mD
This is your password : 6wiu7pcgb

Cực kì đơn giản phải không nào, như vậy bạn đã có thể tự tạo cho bản thân một hàm python để khởi tạo mật khẩu ngẫu nhiên bằng code Python rồi. Chúc các bạn thành công.

Nguồn: https://cuongquach.com/

How to generate random password string in Python

How to generate random password with Python programming language – Cuongquach.com | Ok, i will introduce you a good way with some neccessary modules to make a strong password (digits + upper character + lower character), the min length of password string will be 8. In this post, i will use Python 2.7 syntax for example, if you choose Python 3.x you need to search document of module.

1. Using module string python

Let’s import module string.

>>> import string

Print a string contains all alphabet characters.

>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

Print all special characters.

>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

Print a string contains numbers.

>>> string.digits
'0123456789'

In this post, i will exclude all special characters in operation of generating random password ? Why ? Because in some OS or applications, they dont accept almost of special characters so we dont need them so much for inconvenience.

Howerver, what we will do next ? We just have few list of needed character for combining operation to make a strong password. Ok, next step we will use module ‘random‘ in Python to make a bit interesting action.

2. Using module random python

Module random in python supports you a lot of useful method for any case like generate random digit, choice an object in list,… in this post we will only need to use method pick a random character once time.

>>> import string, random
>>> chars = string.letters
>>> print chars
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> random.choice(chars)
't'
>>> random.choice(chars)
'V'
>>> random.choice(chars)
'J'
>>> random.choice(chars)
'a'

 
– Go to this time, i think we can imagine how sample code will work to create a random password.

3. Sample code to generate random password

In this case, i will fix the min size and max size for length of password (from 8 -> 15).
Run loop ‘for‘ and extract random character in database pre-defined characters. After that, concentrate all of extracted character together without any delimiter.

import string
import random

chars_fixed = string.letters + string.digits
min_size_pass = 8
max_size_pass = 15

password = "".join(random.choice(chars_fixed) for x in range(random.randint(min_size_pass, max_size_pass)))
print "This is your password : %s" % password

 Output:

This is your password : DZ2b6wjXj8mD
This is your password : 6wiu7pcgb

 Very easy right, now you can use above code to deploy a function for generate random password. Just do it by yourself.

Quách Chí Cường: Bạn đang theo dõi website "https://cuongquach.com/" nơi lưu trữ những kiến thức tổng hợp và chia sẻ cá nhân về Quản Trị Hệ Thống Dịch Vụ & Mạng, được xây dựng lại dưới nền tảng kinh nghiệm của bản thân mình, Quách Chí Cường. Hy vọng bạn sẽ thích nơi này !
Related Post