Table of contents
1.
Introduction
2.
Complexity validators in web2py
2.1.
IS_STRONG
3.
Security validators in web2py
3.1.
CRYPT
4.
Frequently Asked Questions
4.1.
What does the sha512 algorithm do?
4.2.
Why do we use the “salt” argument in the security validator?
4.3.
What are the different types of validators?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Complexity and Security Validators in web2py

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The forms in web2py are used to take inputs from users and perform actions on them by storing the data. The data provided by the users must be validated for security and to ensure it is not breached or mishandled. But how do we perform security validation on the user’s data? We have validators to handle this validation in web2py. Validators are the classes that are used to validate input fields and provide security to the data. Let’s learn about complexity and security validators in web2py in this article.

web2py

Complexity validators in web2py

The complexity validators validate the data format and enforce complexity requirements on the data fields. Mostly this validator is used for passwords to ensure they are in the required format. Let’s discuss the complexity validators and how to use them.

IS_STRONG

Whenever we provide a password in an input field in any form or registration page, it shows if the password is strong or not. We use the IS_STRONG validator in web2py to achieve this. This validator checks if a password is strong enough to be stored securely so it cannot be hacked or easily cracked by others. 

Image showing strong password

min - defines the minimum length of the value

special - defines the minimum number of required special characters and what special characters can be included in the password.

upper - is the minimum number of upper case characters

invalid - defined the list of forbidden characters, by default invalid=' "'(double quote).

max - defines the maximum length of the value

lower - defines the minimum number of lower case characters

number - defines the minimum number of digit characters

error_message - displays an error message if the validation fails.

requiredPassword = IS_STRONG(min=8, max=20, invalid=’$’ special=2, upper=1, lower=1, number=1, error_message=”Password must contain the following: A lowercase letter, A capital (uppercase) letter, A number, Minimum 8 characters, Maximum 20 characters, Must not contain $ character”)
You can also try this code with Online Python Compiler
Run Code
screenshot for password validation

Security validators in web2py

We might have observed that the password and username we give are sometimes passed to the database through the URL. But the password is coded into different characters for privacy concerns. This is known as hashing. The security validators perform hashing on the password given as input by users. 

CRYPT

This validator performs hashing on the password to prevent them from being passed into the database with the same characters. By default, the CRYPT validator uses 1000 iterations of the pbkdf2 algorithm combined with the SHA512 algorithm to produce a 20-byte-long hash. You can validate a password and then hash it as shown below.

requiredPassword = [IS_STRONG(), CRYPT(key='sha512:thisisthekey', salt=False)]
You can also try this code with Online Python Compiler
Run Code


The above example validates the password according to the arguments and then hashed the password with the given key.

Frequently Asked Questions

What does the sha512 algorithm do?

The Secure Hash Algorithm 512 or sha512 is a hashing that converts the text of any length into a string of fixed size. Each output produces an SHA-512 string with 512 bits (64 bytes).

Why do we use the “salt” argument in the security validator?

The salt argument is a cryptographic salt made up of random string which adds random bits to each password before hashing. The bits added are unique to every password, so two or more users can choose the same password without any issues.

What are the different types of validators?

There are different types of validators provided by web2py, they are; security, complexity, special type, text, date and time, range, set and equity, database, custom, multiple, and dependency validators. 

Conclusion

We have discussed the concept of complexity and security validators in web2py in this article. You can now start coding in your preferred language.

Hey Ninjas! We hope this blog helped you better to understand the complexity and security validators in the web2py concept. Please check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems. Please upvote our blog to help the other ninjas grow.

Happy Coding!

Thank you by coding ninjas
Live masterclass