Practical Cryptography in Python: Learning Correct Cryptography by Example
Practical Cryptography in Python: Learning Correct Cryptography by Example
in Python
Learning Correct Cryptography
by Example
Seth James Nielson
Christopher K. Monson
Practical Cryptography in Python: Learning Correct Cryptography by Example
Seth James Nielson Christopher K. Monson
Austin, TX, USA Hampstead, MD, USA
Introduction�������������������������������������������������������������������������������������������������������������xv
Chapter 2: Hashing������������������������������������������������������������������������������������������������� 21
Hash Liberally with hashlib��������������������������������������������������������������������������������������������������������� 21
Making a Hash of Education������������������������������������������������������������������������������������������������������� 25
Preimage Resistance������������������������������������������������������������������������������������������������������������� 27
Second-Preimage and Collision Resistance�������������������������������������������������������������������������� 33
Digestible Hash��������������������������������������������������������������������������������������������������������������������������� 36
Pass Hashwords...Er...Hash Passwords�������������������������������������������������������������������������������������� 39
Pick Perfect Parameters�������������������������������������������������������������������������������������������������������� 44
v
Table of Contents
vi
Table of Contents
vii
Table of Contents
viii
Table of Contents
Bibliography��������������������������������������������������������������������������������������������������������� 361
Index��������������������������������������������������������������������������������������������������������������������� 363
ix
About the Authors
Seth James Nielson is the Founder and Chief Scientist of
Crimson Vista, Inc., a boutique computer security research
and consulting company. He is also an adjunct professor at
Johns Hopkins University where he teaches network security
and has also served as the Director of Advanced Research
Projects in the Information Security Institute. As part of his
Hopkins work, he co-founded the https://cryptodoneright.
org knowledge base, through a generous grant from Cisco.
xi
About the Technical Reviewer
Mike Ounsworth is a Software Security Architect at Entrust
Datacard. He holds an undergraduate degree in computer
science with concentrations in mathematics and physics
and an MSc in computer science in robotics and artificial
intelligence. Professionally, his day job is mainly application
security architecture and penetration testing, with some
research side projects in cryptography and post-quantum
cryptography. Outside of work, he mentors teams competing
in the high-school-age FIRST Robotics Competition.
xiii
Introduction
The interconnected world of the current era has drastically changed everything,
including banking, entertainment, and even statecraft. Despite differences in users,
purposes, and security profiles, these digital applications have at least one thing in
common: they all require properly applied cryptography to work correctly.
Informally, cryptography is the mathematics of secrets. We need secret codes to
make messages unreadable to unauthorized eyes, to make messages unchangeable, and
to know who sent the message. Practical cryptography is the design and use of these
codes in real systems.
This book is primarily for computer programmers with little or no previous
background with cryptography. Although mathematics makes brief appearances in the
book, the overall approach is to teach introductory cryptography concepts by example.
Our journey begins with some introductory components, including hashing
algorithms, symmetric encryption, and asymmetric encryption. Next, we go beyond
encryption and into the realm of digital certificates, signatures, and message
authentication codes. The final chapters show how these various elements come
together in interesting and useful combinations, such as Kerberos and TLS.
Another important part of cryptography by example is cryptography by bad
example! In this book we will break things on purpose to help the reader appreciate what
motivates accepted best practices. Exercises and examples include walk-throughs of real
vulnerabilities that have afflicted the Internet. The bad examples will help the reader
gain a greater intuition of what goes wrong in cryptography and why.
xv