CALL US

  +91 8266041801

  +44 203 372 4609

  +1 408 454 6110

  evontech

Get Free Quote

Python 3.6 adds New secrets Module for Robust Account and Password Security

python-ev.png

Python 3.6, the newest major release of the Python language, has added a new module, called secrets, to help generate cryptographically strong random numbers for managing secrets, like account authentication, tokens and related secrets. Python developers are highly likely to prefer secrets over the default pseudo-random number generator in the random module, since it’s not meant for cryptography or security, but modelling and simulation.

Let’s understand with an example how one can create their own cryptographically strong pseudo-random values and generate tokens using the secrets module.

How to create Cryptographically Strong Pseudo-Random Values using secrets

>>> import secrets
>>> import string
>>> characters = string.ascii_letters + string.digits
>>> secure_password = ''.join(secrets.choice(chNo Records.aracters) for i in range(10))
>>> secure_password
'SRvM54ZAs1'

The first step is to import the secrets and the string modules. Then we create a string of uppercase letters and integers. Now, in order to choose characters randomly to generate a secure password, we need to use the secrets module’s choice() method. The reason it’s being called a secure password is because there’s been a use of mixed case, numbers and symbols in the password, which is highly advised to people to keep their passwords protected from hack attacks.

How to Generate Tokens using secrets

There is not one but several methods to generate tokens using the secrets module. Below are mentioned some examples to ease your learning on token generation using secrets.

>>>: secrets.token_bytes()
b'\xd1Od\xe0\xe4\xf8Rn\xf4G\xdb\x08\xa8\x85\xeb\xba>\x8cO\xa7XV\x1cb\xd6\x11\xa0\xcaK'

>>> secrets.token_bytes(8)
b'\xfc,9y\xbe]\x0e\xfb'

>>> secrets.token_hex(16)
'6cf3baf51c12ebfcbe26d08b6bbe1ac0'

>>> secrets.token_urlsafe(16)
'5t_jLGlV8yp2Q5tolvBesQ'

The token_bytes function here allows to return a random byte string containing nbytes number of bytes. A reasonable default could also be put into use when nbytes is None or not supplied. In the first example, there is no mention of number of bytes, hence Python itself choose a reasonable number there. The token-bytes function was used again, but this time with 8 bytes. The next function used was token_hex, to return a random text string, in hexadecimal. The token_urlsafe function is the last one used there, meant to return a random URL-safe text string. Base64 encoding was also used for text.

Click here to unveil 7 Python libraries to use in 2017

How Many Bytes to Use for Tokens?

You should have sufficient randomness for your tokens to secure them against brute-force attacks. It’s advised that at least 32 bytes (256 bits) of randomness should be used to protect tokens from security breaches.

The Python developer community will see the secrets module as an important addition to Python 3.6. With secrets, Python 3.6 developers now have a reliable way to generate cryptographically strong tokens and passwords.

What’s your view on addition of secrets to Python 3.6? Would you like to give the secrets module a try for generating tokens and passwords? Please share your views in the comment box below.

Source 1: http://www.blog.pythonlibrary.org/

Source 2: https://docs.python.org 


Why to Use HTML5 and CSS3 for Your Business?
Why Do Developers Now Compare Vue.js to JavaScript...

Comments

 
No comments yet
Already Registered? Login Here
Guest
Monday, 08 August 2022
If you'd like to register, please fill in the username, password and name fields.

SEARCH BLOG

development Salesforce CRM offshore software development business offshore software development company software application outsourcing C++ apps Cloud computing web java web development mobile app development Big Data Analytics cloud mobile app development company J2ee custom app development Big Data website project management IT developers javascript IT consulting and software development developer QA Offshore development India software development NodeJs Salesforce customization Automation Offshore development testing Web app development programming Software development and testing Evon Technologies Product Development Salesforce Cloud Services consultant Salesforce consulting Android development enterprise data QA and Testing MVP Development Progressive Web Apps digital marketing services Salesforce development Agile Development risk management digital marketing services india offshore Python Agile product Development iOS apps Social Media Marketing Salesforce Lightning EmployeeEngagement project management methodology mobile data security startups language business analysts Offshore software development services Joomla developers Salesforce cloud Salesforce CRM consulting software development outsourcing Salesforce Mobile Development business management project Scrum time tracking Blockchain Offshore development company Findnerd Project Manager marketing automation App Development Outsourcing QA services WordPress Collaboration Recreationalactivity India DevOps Web application Virtual reality Real time data accessibility GDPR MVP software Android app development outsourcing VR Apps

Our Team

The versatile 8 in 2006 to 300+ Mavens in 2022, team Evon has paved its way into the tech world by creating some seriously cool geeky stuff. Startup or established, you got to meet our team.

Certifications

  •   CMMI - Level 3
  •   ISO 27001 : 2013
  •   ISO 9001 : 2015

Recent Tweets

Evon Technologies

Your calm mind is the ultimate weapon against your challenges. #MondayMorning #MondayMotivation #MondayBlues https://t.co/hzHvCHG511

Evon Technologies

Thank you so much @PriyankaJoshiB for Retweet.

Get in Touch

  +91 8266041801

  +44 203 372 4609

  +1 408 454 6110

  evontech

 This email address is being protected from spambots. You need JavaScript enabled to view it.

   A- 5, IT Park, Dehradun, Uttarakhand, India, PIN - 248001.

follow us on

×
We use cookies on our website to provide you with a more personalised digital experience and for analytics related to our website and other media. For more information, please review our Privacy Policy and Cookies Policy.