Practical Cryptography in Python by Seth James Nielson & Christopher K. Monson

Practical Cryptography in Python by Seth James Nielson & Christopher K. Monson

Author:Seth James Nielson & Christopher K. Monson
Language: eng
Format: epub
ISBN: 9781484249000
Publisher: Apress


Listing 5-5Sign Unencrypted Data

There’s probably a bit more in Listing 5-5 than expected, particularly in the padding configuration. Let’s walk through it all.

First, we generate a key pair. For RSA, the public key is derivable from the private key, so generating the private key generates the key pair. The API includes a call to obtain the public key from the private key. In this example, both keys are used. In a real example, the signing and verification code would live in completely different programs, and the verification program would only have access to the public key, not the private key.

In Chapter 4 we also learned how to serialize and de-serialize these kinds of RSA keys from disk.

In the next part of the code, we sign the message. You will notice that we are using padding here just as we did for RSA encryption, but it is a different scheme. The recommended paddings for RSA are OAEP for encryption and PSS for signatures. Perhaps that surprises you given that RSA signatures are generated by encrypting a hash. If it’s all encryption anyway, why do we need different padding schemes?

The answer is that, because signatures are operating on a hash, there are certain characteristics that must be true about the data. The nature of arbitrary data encryption vs. hash encryption drives the two different padding schemes.

Like the OAEP padding used in Chapter 4, PSS padding function also requires the use of a “mask generation function.” At the time of this writing, there is only one such function, MGF1.

Finally, the signature algorithm requires a hashing function. In this example, we are using SHA-256.

The parameters to the verification algorithm should be self-explanatory. Note that the validation function does not return a true or false, rather it raises an exception if the data does not match the signature.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Building Machine Learning Systems with Python by Richert Willi Coelho Luis Pedro(2117)
Hands-On Python Deep Learning for the Web by Anubhav Singh(2033)
Mastering OpenCV 4 with Python by Fernández Villán Alberto;(1947)
Python Feature Engineering Cookbook by Soledad Galli(1935)
Applied Deep Learning with Keras by Matthew Moocarme Mahla Abdolahnejad and Ritesh Bhagwat(1774)
Building Serverless Python Web Services with Zappa by Abdulwahid Abdulhaque Barguzar(1741)
Python for Finance: Analyze Big Financial Data by Yves Hilpisch(1737)
Python for Finance Cookbook by Eryk Lewinson(1719)
Mastering Python Data Visualization by Kirthi Raman(1705)
PYTHON PROGRAMMING ADVANCED: The Guide for Data Analysis and Data Science. Discover Machine Learning With the Optimum Recipes for Mastering Python and ... (Crash Course Tips and Tricks Book 3) by ERIC MATTHEWS & LEWIS TAYLOR(1656)
Django 3 By Example by Antonio Melé(1614)
Supervised Machine Learning with Python by Taylor Smith(1597)
Interactive Data Visualization with Python by Anshu Kumar & Shubhangi Hora & Sharath Chandra Guntuku & Abha Belorkar(1449)
Hands-On GPU Programming with Python and CUDA by Dr. Brian Tuomanen(1368)
Hands-On Image Processing with Python by Sandipan Dey(1336)
Scientific Computing with Python 3 by Claus Fuhrer & Jan Erik Solem & Olivier Verdier(1325)
Flask Framework Cookbook by Shalabh Aggarwal(1300)
Python Deep Learning. by Ivan Vasilev(1276)
Training Systems using Python Statistical Modeling by Curtis Miller(1269)
Django 3 Web Development Cookbook - Fourth Edition by Aidas Bendoraitis(1259)