0%

Chapter 2

Hashes, MACs, and Random Values

Encode, Hash, Sign, or Encrypt

Your attack test from Chapter 1 searches a copy of the state for the text correct horse. Suppose the server stored Y29ycmVjdCBob3JzZQ== for ana instead. Would the test pass? Yes: the text it looks for is gone. Would the password be safe? No. That is correct horse in , and anyone can turn it back.

So “the password is not readable” is too loose a goal. Four different operations can make data look unreadable, and they promise four different things. You have used two of them already, base64 in the Basic header and a digest as an , without having to choose between them. This lesson tells all four apart.

Four operations

OperationNeeds a key?Can it be undone?What it is for
Encoding (base64)noyes, by anyonecarrying bytes where only text fits. It protects nothing
Hashing (SHA-256)nono, and the same input always gives the same digestshowing that two things are the same, as the ETag did
Signing with a one secret key, shared by maker and checkernot undone: computed again and comparedshowing that a message was made by someone who has the key and was not changed since
Encryptionyesonly with the keyhiding the content

Read the table by its columns. The key column asks who can perform the operation: for encoding and hashing, everybody. The undo column asks who can get the input back: for encoding everybody, for hashing nobody. More exactly, nobody knows a way to compute the input from a digest. An input can still be guessed and checked, and Chapter 3 is about that.

Three of the four are in Python’s standard library:

Try it

The first pair of lines is encoding: b'Y29ycmVjdCBob3JzZQ==', and then b'correct horse' again, with no key involved. The second pair is hashing: the same 64 characters twice, starting 4104d36f. Nothing turns them back into the password, but anybody who has the password can compute them. The last two lines are new. They are keyed tags of the same bytes under two different keys, cut to their first 8 characters, and they differ: b4520de8 and 20c498cf. Only someone who has the key can compute the tag that belongs to it. The next lesson explains hmac.new.

What does print(base64.b64decode("Y29ycmVjdCBob3JzZQ==")) print?

Two things called signing

The word “signing” is used for two different mechanisms, and it helps to keep them apart from the start.

A message authentication code, or MAC, is a tag made with a shared secret key. Whoever can check the tag has the key, so they could also have made it. A MAC tells the server “this came from someone with our key”, which is useful when the server is talking to itself: it hands out a message now and wants to recognize it later.

A public-key signature is made with a private key and checked with a different, public one. Anyone can check it, and only one party can make it.

This course makes MACs and calls that signing, which is also the word token libraries use. You meet the other kind in Chapter 10, inside a .

What this course does not build

You will not write encryption or public-key signatures here. Python’s standard library has no general way to encrypt data or to make a public-key signature. Its ssl does both, but only inside . And nobody should write their own: these are the parts of security where a small mistake is invisible and fatal. Where this course needs secrecy on the network, it uses TLS, in Chapter 10.

Choosing

Four situations, and the operation each one calls for:

  • A header may only contain plain characters, and you need to put arbitrary bytes in it. That is encoding. It was the job of base64 in the Basic header.

  • A client wants to know whether the book changed since it last asked. That is hashing: equal digests mean equal bodies.

  • The server gives a client a small message and must later know that what comes back is what it made. That is a keyed tag.

  • Nobody on the network path may read a password while it travels. That is encryption, and here it means TLS.

A site offers a file for download and shows a value next to it, so that anyone can check that the copy they downloaded is the same as the file on the site. Nobody involved holds a secret. Which operation?

A database stores each password base64-encoded. What does a thief with a copy have?

Of the four operations, one has no code in your project yet: the keyed tag. In the next lesson you write it.