1576132248
In this piece, you’ll learn the proper ways to generate strong random passwords and tokens that are cryptographically secured. Having secure random numbers allows us to manage sensitive information, such as password and security tokens. We will be using the secrets module, available since Python 3.6. The official documentation states:
“… secrets should be used in preference to the default pseudo-random number generator in the random module, which is designed for modelling and simulation, not security or cryptography.”
There are three sections in this article:
Let’s get started.
The secrets module provides a few built-in functions that we can use to generate numbers and tokens. No setup is required but we need to import the module before we use it.
import secrets
Use the randbelow
function to generate a number. It accepts an integer and the number generated is between 0 and the input integer minus 1. The input integer must be higher than 0
secrets.randbelow(2) # generate either 0 or 1
secrets.randbelow(10) # generate a number from 0 to 9
secrets.randbelow(0) # error
secrets.randbelow(-10) # error
You can also use the randbits
function to generate a random number. It accepts an integer which represents the number of bits. The input integer must be higher than 0.
secrets.randbits(1) # generate either 0 or 1
secrets.randbits(2) # generate a number from 0 to 3
secrets.randbits(4) # generate a number from 0 to 15
secrets.randbits(8) # generate a number from 0 to 255
The module also provides a way for us to choose a random element from a non-empty sequence. Let’s try it out using the choice
function
colour = ['red', 'blue', 'green', 'purple', 'yellow']
secrets.choice(colour)
token_bytes
function is the perfect choice for generating bytes. You can specify an integer as a parameter. It will determine a random integer if you don’t specify anything.
secrets.token_bytes(8) # generate 8 random bytes string
You should see a random byte string like this:
b'\x1bq\x8e\x83\x08\xb2g\x17'
If you wanted a string in hexadecimal, you can use the token_hex
function. Just like the token_bytes
function, it accepts an integer which is used to generate n number of bytes, each byte will be converted to two hex digits later.
secrets.token_hex(16) # generate 16 random hexadecimal string
This is an example of the output:
cd7b7fb7e0c5c1fa17389050f884526e
Sometimes, you might want a string that is Base64 encoded for your web application. The token_urlsafe
function comes in handy for such a use case.
secrets.token_urlsafe(16)
I got the following result:
S357dE8QSuE
In this section, I will outline some of the best practices for generating a secure password and token. Feel free to test them on your own.
import string
import secrets
alphabet = string.ascii_letters + string.digits
password = ''.join(secrets.choice(alphabet) for i in range(10))
print(password)
ascii_letters
— contains both the lower case and upper case from A-Zimport string
import secrets
alphabet = string.hexdigits + string.punctuation
password = ''.join(secrets.choice(alphabet) for i in range(10))
print(password)
import string
import secrets
alphabet = string.ascii_letters + string.digits
while True:
password = ''.join(secrets.choice(alphabet) for i in range(10))
if (any(c.islower() for c in password) and any(c.isupper() for c in password) and any(c.isdigit() for c in password)):
break
print(password)
islower
— Determine if the character is lowercaseisupper
— Determine if the character is uppercaseisdigit
— Determine if the character is a digitimport string
import secrets
alphabet = string.ascii_letters + string.digits
while True:
password = ''.join(secrets.choice(alphabet) for i in range(10))
if (sum(c.isupper() for c in password) >= 2 and sum(c.isdigit() for c in password) >= 2):
break
print(password)
import secrets
animal = ['horse', 'elephant', 'monkey', 'donkey', 'goat', 'chicken', 'duck', 'mouse']
fruit = ['apple', 'banana', 'peach', 'orange', 'papaya', 'watermelon', 'durian']
electronic = ['computer', 'laptop', 'smartphone', 'battery', 'charger', 'cable']
vegetable = ['lettuce', 'spinach', 'celery', 'cabbage', 'turnip', 'cucumber', 'eggplant']
word_list = animal + fruit + electronic + vegetable
password = set()
while True:
password.add(secrets.choice(word_list))
if(len(password) >= 4):
break
print(' '.join(password))
import secrets
url = 'https://mywebsite/reset?key=' + secrets.token_urlsafe()
print(url)
Let’s recap what we’ve learned today. We started off exploring the basic functions provided by the secrets
module.
Then, we tested the functions to generate some random password and tokens in string token or bytes.
Finally, we tried to play with the module and generated a few different types of password that are strong and secured.
Please be reminded that you should not store your password in any plain text or encrypted file that is easily recoverable. They should be salted and hashed using an irreversible, one-way hash function.
Thanks for reading and hope you enjoyed this tutorial.
#python #programming
1617086469
Create a secure password using our generator tool. Help prevent a security threat by getting a strong password today on hackthestuff.com.
#password #strong password generator #password generator #password generator tool #random generator tool #google generator tool
1619614811
Random Password Generator is a program that automatically generates a password randomly. Those generated passwords are mix with numbers, alphabets, symbols, and punctuations. This type of program helps the user to create a strong password.
Step By Step Tutorial :https://cutt.ly/ZbiDeyL
#password generator #random password generator #python password generator #random password generator javascript #html #javascript
1592807820
What is 2FA
Two-Factor Authentication (or 2FA as it often referred to) is an extra layer of security that is used to provide users an additional level of protection when securing access to an account.
Employing a 2FA mechanism is a vast improvement in security over the Singe-Factor Authentication method of simply employing a username and password. Using this method, accounts that have 2FA enabled, require the user to enter a one-time passcode that is generated by an external application. The 2FA passcode (usually a six-digit number) is required to be input into the passcode field before access is granted. The 2FA input is usually required directly after the username and password are entered by the client.
#tutorials #2fa #access #account security #authentication #authentication method #authentication token #cli #command line #cpanel #feature manager #google authenticator #one time password #otp #otp authentication #passcode #password #passwords #qr code #security #security code #security policy #security practices #single factor authentication #time-based one-time password #totp #two factor authentication #whm
1625013180
There are two types of random number generators: pseudo-random number generator and true random number generator.
Pseudorandom numbers depend on computer algorithms. The computer uses algorithms to generate random numbers. These random numbers are not truly random because they are predictable like the generated numbers using NumPy random seed.
Whereas, truly random numbers are generated by measuring truly physical random parameters so we can ensure that the generated numbers are truly random.
The pseudo-random numbers are not safe to use in cryptography because they can be guessed by attackers.
In Python, the built-in random module generates pseudo-random numbers. In this tutorial, we will discuss both types. So let’s get started.
Table of Contents
#python #random #generate random numbers #random numbers #generate random numbers in python
1656193861
Hello guys, Today in this post we’ll learn How to Create a Simple Login Page with a fantastic design. To create it we are going to use pure CSS and HTML. Hope you enjoy this post.
A login page is one of the most important component of a website or app that allows authorized users to access an entire site or a part of a website. You would have already seen them when visiting a website. Let's head to create it.
Whether it’s a signup or login page, it should be catchy, user-friendly and easy to use. These types of Forms lead to increased sales, lead generation, and customer growth.
Demo
Click to watch demo!
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<link rel="stylesheet" href="styledfer.css">
</head>
<body>
<div id="login-form-wrap">
<h2>Login</h2>
<form id="login-form">
<p>
<input type="email" id="email" name="email" placeholder="Email " required><i class="validation"><span></span><span></span></i>
</p>
<p>
<input type="password" id="password" name="password" placeholder="Password" required><i class="validation"><span></span><span></span></i>
</p>
<p>
<input type="submit" id="login" value="Login">
</p>
</form>
<div id="create-account-wrap">
<p>Don't have an accout? <a href="#">Create One</a><p>
</div>
</div>
<script src='https://code.jquery.com/jquery-2.2.4.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.0/jquery.validate.min.js'></script>
</body>
</html>
body {
background-color: #020202;
font-size: 1.6rem;
font-family: "Open Sans", sans-serif;
color: #2b3e51;
}
h2 {
font-weight: 300;
text-align: center;
}
p {
position: relative;
}
a,
a:link,
a:visited,
a:active {
color: #ff9100;
-webkit-transition: all 0.2s ease;
transition: all 0.2s ease;
}
a:focus, a:hover,
a:link:focus,
a:link:hover,
a:visited:focus,
a:visited:hover,
a:active:focus,
a:active:hover {
color: #ff9f22;
-webkit-transition: all 0.2s ease;
transition: all 0.2s ease;
}
#login-form-wrap {
background-color: #fff;
width: 16em;
margin: 30px auto;
text-align: center;
padding: 20px 0 0 0;
border-radius: 4px;
box-shadow: 0px 30px 50px 0px rgba(0, 0, 0, 0.2);
}
#login-form {
padding: 0 60px;
}
input {
display: block;
box-sizing: border-box;
width: 100%;
outline: none;
height: 60px;
line-height: 60px;
border-radius: 4px;
}
#email,
#password {
width: 100%;
padding: 0 0 0 10px;
margin: 0;
color: #8a8b8e;
border: 1px solid #c2c0ca;
font-style: normal;
font-size: 16px;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
position: relative;
display: inline-block;
background: none;
}
#email:focus,
#password:focus {
border-color: #3ca9e2;
}
#email:focus:invalid,
#password:focus:invalid {
color: #cc1e2b;
border-color: #cc1e2b;
}
#email:valid ~ .validation,
#password:valid ~ .validation
{
display: block;
border-color: #0C0;
}
#email:valid ~ .validation span,
#password:valid ~ .validation span{
background: #0C0;
position: absolute;
border-radius: 6px;
}
#email:valid ~ .validation span:first-child,
#password:valid ~ .validation span:first-child{
top: 30px;
left: 14px;
width: 20px;
height: 3px;
-webkit-transform: rotate(-45deg);
transform: rotate(-45deg);
}
#email:valid ~ .validation span:last-child
#password:valid ~ .validation span:last-child
{
top: 35px;
left: 8px;
width: 11px;
height: 3px;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
.validation {
display: none;
position: absolute;
content: " ";
height: 60px;
width: 30px;
right: 15px;
top: 0px;
}
input[type="submit"] {
border: none;
display: block;
background-color: #ff9100;
color: #fff;
font-weight: bold;
text-transform: uppercase;
cursor: pointer;
-webkit-transition: all 0.2s ease;
transition: all 0.2s ease;
font-size: 18px;
position: relative;
display: inline-block;
cursor: pointer;
text-align: center;
}
input[type="submit"]:hover {
background-color: #ff9b17;
-webkit-transition: all 0.2s ease;
transition: all 0.2s ease;
}
#create-account-wrap {
background-color: #eeedf1;
color: #8a8b8e;
font-size: 14px;
width: 100%;
padding: 10px 0;
border-radius: 0 0 4px 4px;
}
Congratulations! You have now successfully created our Simple Login Page in HTML and CSS.
My Website: codewithayan, see this to checkout all of my amazing Tutorials.