<a id="ch-05"></a>

## Chapter 5 — The hash print is dict order

<!-- open-cyber-academy:lesson-figures:start ch-05 -->
### Lesson map

Lesson 05. Map, example, EX051.

```mermaid
flowchart TD
  D["Three pairs, insertion order"] --> P["One pass, no sort"]
  P --> O["budget, then the installer, then photo"]
  O --> Q{"Did the match earn the first line?"}
  Q -->|No| K["Keep the embedded print"]
```

The loop follows the dict. A match does not jump the queue.

### Logical drawing

```text
budget.xlsx               0edf30e2fa30  no match
FreeTemplates_setup.exe   3e5d3b83407c  Fake-template adware (synthetic)
photo.jpg                 ffcab6e8630a  no match
```
<!-- open-cyber-academy:lesson-figures:end ch-05 -->

### Learning outcomes

After this chapter, you should be able to:

1. Name the order `files` yields.
2. Separate that order from a match-first ranking.
3. Refuse a note that prints the installer row on top.

### 5.1 One pass, and width 25 is not a ranking.

`lab_hashcheck.py` stores three pairs in a dict. The first pair is the name `budget.xlsx` with the byte string for a synthetic spreadsheet. The second pair is `FreeTemplates_setup.exe` with the installer bytes. The third pair is `photo.jpg` with the photo bytes. Since Python 3.7 a dict keeps insertion order, and `files.items()` walks that order. The loop body hashes the current bytes, slices a prefix, and prints. Nothing in the file calls `sorted` on the names.

The format starts with `{name:25}`. Width 25 pads a short name with spaces on the right. `budget.xlsx` is 11 characters, so 14 pad spaces sit inside the field. `FreeTemplates_setup.exe` is 23 characters, so two pad spaces sit inside the field. `photo.jpg` is 9 characters, so 16 pad spaces sit inside the field. The format then adds one space before the prefix. Those spaces do not encode risk. The installer row is second because that pair was inserted second, even though a reader might want the only match moved to the top.

A later decision to restore the laptop is a sentence in the report. It does not delete a pair. A print that started with `FreeTemplates_setup.exe` would be a different dict.

| What the loop uses | What it does not use |
|---|---|
| Insertion position in `files` | A match-first sort |
| The name and the byte string | A startup name |
| Format width 25 | A severity score |

### 5.2 Worked example

| Offered result | What produced it | Decision |
|---|---|---|
| Three lines, spreadsheet first | Dict order, one pass | Keep |
| The installer still second | Its pair was not moved | Keep |
| "The script sorted matches first." | No sort is called | Refuse |
| "The no-match lines were dropped." | Two pairs still print `no match` | Refuse |

> **Synthetic case.** These three pairs are the only hash output.

### Errors and limits

Do not rebuild the dict so that the matching name prints first.

### Apply

- **EX051 — Dict order.** The print names `budget.xlsx`, then `FreeTemplates_setup.exe`, then `photo.jpg`. May a match-first ranking, or a later decision to restore the laptop, replace that order? *Rubric:* 3 = no; the three pairs stay in insertion order; width 25 is padding; do not sort the script;; 2 = the three lines stay and the restore stays prose;; 1 = one line;; 0 = none.

<!-- open-cyber-academy:chapter-navigation:start ch-05 -->
### Continue studying — lesson 05 (chapter 05 of 20)

[← Chapter 04](#ch-04) · [Course chapter index](../SUBJECTS.md) · [Chapter 06 →](#ch-06)

Continue to the next authored lesson.
<!-- open-cyber-academy:chapter-navigation:end -->

## Worked answers

<a id="answer-ex051"></a>
**EX051.** No. The three pairs stay in insertion order. Width 25 only pads the name. A later restore does not reorder the script.
