String. Generate random sequences.
https://www.youtube.com/watch?v=KzqSDvzOFNA
What the function does?
Generate Random Strings and Passwords
Import string and random module
Task 1 ”Easy PAssWord"
Task 2 “Check Password”
Task 3 encryption program
Python Secrets Module to Generate secure random numbers for managing secrets.
Why use the secrets module?
Secrets Module Functions
Task 4 ”Secure token"
Task encryption program
169.69K

Programming 2 lesson new

1. String. Generate random sequences.

12.6.1.1 use the functions of the library Random to get a pseudo-random
number;
12.6.1.2 determine the function of the library Random for the specified task;
12.6.1.3 identify code snippets that use random sequence generation;
12.6.1.4 solve problems using random numbers and random sequences;

2. https://www.youtube.com/watch?v=KzqSDvzOFNA

Let’s remember!
https://www.youtube.com/watch?v=KzqSDvzOFNA

3. What the function does?

Function
What it does
randint(x, y)
Generates a random integer from x to y, including the x and y.
random()
Generates a random floating-point number in the interval [0,1)
sample(population, k)
Selects k unique random elements from a population sequence or set.
choice(seq)
Chooses a random element from a non-empty sequence seq.
shuffle(x)
Shuffles list x in place.

4. Generate Random Strings and Passwords

5. Import string and random module

• The string module contains various string constant which
contains the ASCII characters of all cases. It has separate
constants for lowercase, uppercase letters, digits, and
special symbols, which we use as a source to generate a
random string.
Pass string constants as a source of randomness to
the random module to create a random string

6.

7.

import random
import string
print(random.choice(string.ascii_uppercase))
print(random.choice(string.digits))
print(random.choice(string.punctuation))

8.

9. Task 1 ”Easy PAssWord"

Task 1 ”Easy PAssWord"
Write a Python program that generates a simple password.
The password must contain:
1 uppercase letter
4 lowercase letters
1 digit
Example output:
Qabcd7

10. Task 2 “Check Password”

Write a Python program that checks how strong a
password is.
If the password is strong enough – print "Password is
strong!".
Otherwise – print "Password is weak!" and explain
why.
Example
Input: sWdRF
Output: Password is weak! No punctuation (

11. Task 3 encryption program

• Write a Python program that automatically tries to "crack" the
password generated in Task 1.
• The program should test all possible combinations until it finds
the correct password.
• When the password is found, print it and show the number of
attempts.
Example output:
Password found: Qabcd7
Attempts: 15293

12. Python Secrets Module to Generate secure random numbers for managing secrets.

Python Secrets Module
to Generate secure
random numbers for
managing secrets.
Python 3.6 introduced a secrets module for
generating robust and secure random numbers. In
this lesson, you’ll learn how to
use secrets.SystemRandom() class and secrets
module functions to create random numbers, data,
URLs, and tokens securely and safely.

13. Why use the secrets module?

Why use the secrets module?
• The cryptographically secure random generator generates random
data using synchronization methods to ensure that no two
processes can obtain the same data simultaneously.
• The random generator provided by the Python random module is a
pseudo-random number generator that is not cryptographically
secure. As a result secrets module is added in Python 3.6 and
onwards to secure the random number generator.
• Before Python 3.6, we have the random() and SystemRandom class
to cryptographically secure random generator.

14. Secrets Module Functions

Secrets Module Functions
Function
Description
secrets.SystemRandom()
Get an instance of the secure random generator
secrets.randbelow(n)
Generate a secure random integer number
secrets.choice(seq)
Returns a secure random element from a non-empty
sequence
secrets.randbits(k)
returns a secure unsigned integer with k random bits
secrets.token_bytes([nbytes=None])
Return a secure random byte string
secrets.token_hex([nbytes=None])
Return a secure random text string, in hexadecimal
format
secrets.token_urlsafe([nbytes=None])
Return a secure random URL-safe text string
secrets.compare_digest(a, b)
To reduce the risk of timing attacks

15. Task 4 ”Secure token"

Task 4 ”Secure token"
Write a Python program that generates a
secure password using the secrets
module.
The password should contain:
8 random characters (letters and digits).

16. Task encryption program

• Create the encryption program for given string using
Random library
English     Русский Rules