SoFunction
Updated on 2024-12-10

Data encryption in Python using ItsDangerous shortcut implementation

The complete sample code and files for this article have been uploaded to myGithubrepository/CNFeffery/PythonPracticalSkills

This is the 11th installment of my series of articles on "Practical Python Secrets", which is grounded in the author's daily work using thePythonAccumulated insights are brought to you in each installment with a simple tip that can be learned in a few minutes.

As the 11th installment of the series, we're about to learn that in thePythonQuickly encrypt data in the

Data security, as a clichéd topic, is one of the most important aspects of our daily use ofPythoncarry out tasks such aswebApplication development and other tasks around the topic, we want to transfer some data through the network at the same time, but also to ensure the security of the data, after all, the transfer of some sensitive data to an unknown environment for use is a very dangerous thing.

And by using thePythonstorehouseItsDangerousWe can then efficiently and effectively complete the process of encrypting/decrypting data, which is part of thepalletsThe project plan, which also contains theFlaskJinjaQuartprominentwebDeveloping related projects:

pass (a bill or inspection etc)pip install itsdangerousAfter completing the installation, we can use it to encrypt/decrypt data, although it provides a fairly large variety of methods to meet a variety of data encryption/decryption scenarios, but we only need to use its daily twoAPIThe interface is sufficient:

Routine data encryption/decryption

In the most common scenario, we pass an existing key and utilize theURLSafeSerializer()The data can be transformed into an encrypted result by building a serializer:

And for the encrypted result, just use the same key to build the serializer for decryption:

Time-out-capable data encryption/decryption

In some scenarios where we want to encrypt data generated from the point in time when it was generated and time out after a certain amount of time, we can use theURLSafeTimedSerializer()Its usage is similar to that ofURLSafeSerializer()It's basically the same, except that theloads()Specifically parameterizable when decrypting datamax_age, when decryption is more than a few minutes away from the generation of this encryption resultmax_agecorresponding number of seconds, an error is triggered:

This will satisfy most of the daily use scenarios 😊, about theItsDangerousFor more advanced content, please visit the website./

To this article on the use of Python ItsDangerous shortcut to achieve data encryption is introduced to this article, more related Python data encryption content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!