Section 15.19

Unicode normalization

Definition

Unicode normalization is the conversion of a Unicode §12.48 string into one of four standard forms, so that strings the Unicode Standard declares equivalent become identical sequences of code points. A code point is the number Unicode assigns to a character or mark, written U+ and hexadecimal digits. Unicode Standard Annex #15 defines NFC (Normalization Form C), NFD (Normalization Form D), NFKC (Normalization Form KC) and NFKD (Normalization Form KD). Encoding the code points as bytes is a separate, later step.

The idea

The letter é can be stored in two ways: as the single code point U+00E9, the precomposed letter e with acute accent, or as U+0065, the plain letter e, followed by U+0301, the combining acute accent, a mark that attaches to the character before it. Both display as é, and two keyboards or programs can produce the two sequences for the same word. A program that compares code points, or the bytes that encode them, sees two different names: a password check fails, a file lookup misses, a duplicate account slips through.

The Unicode Standard declares such sequences canonically equivalent: they represent the same text and should look and behave the same. Normalization picks one representative for each class of equivalent strings, the way writing fractions in lowest terms picks 1/21/2 to stand for 2/42/4 and 3/63/6. The picture stops at one point: Unicode has a second, looser relation, compatibility equivalence, which also joins characters with the same basic content but a different appearance or function. Examples are the ligature U+FB01, which draws f and i as one shape, and the letters f and i; or the superscript two U+00B2 and the digit 2. Treating these as equal suits a search box but is wrong in a formula, where x2x^2 and x2 differ. An application must choose which relation it wants.

How it works

Three building blocks.

The four forms. NFD is canonical decomposition followed by canonical ordering, and NFC is NFD followed by canonical composition. NFKD is compatibility decomposition followed by canonical ordering, and NFKC is NFKD followed by canonical composition. D stands for decomposed, C for composed, and K marks the two forms that apply compatibility mappings.

Where to normalize. An application chooses either literal identity, comparing code points exactly as received, or one normalization form, and applies the choice where text enters the system, before any comparison, hash, signature or stored copy depends on it. Every producer and consumer of the same data follows the same rule. A cryptographic hash function §5.1 or a digital signature §5.14 works on bytes, so the order is: normalize, encode, for example in UTF-8 (8-bit Unicode Transformation Format), then hash or sign.

Stability. The Unicode Consortium guarantees that a string normalized under one version of the standard stays normalized under every later version, provided it contains only code points assigned in the version used. An unassigned code point can receive a decomposition later, so a system that depends on stable normalized forms can refuse unassigned code points.

Worked example

Take the two spellings of é. UTF-8 writes a code point from 0x80 to 0x7FF as two bytes of the patterns 110xxxxx and 10xxxxxx, holding 11 bits in the positions marked x, and a code point below 0x80 as one byte with the same value.

SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output) of A begins with the hexadecimal digits 4a99557e4033c353, and of B with bf12767b0f2a56b2. The strings look identical on screen, but the hash function sees only bytes. Normalize first, and the difference disappears. NFC turns both strings into U+00E9, the bytes C3 A9 and the hash beginning 4a99557e; NFD turns both into the bytes 65 CC 81. Either choice works when every party makes the same one; a party hashing NFC bytes and another hashing NFD bytes disagree again.

Ordering and compatibility at work: q followed by U+0307, the combining dot above (combining class 230), then U+0323, the combining dot below (class 220), is canonically equivalent to q with the two marks swapped. NFD puts the smaller class first, so both orders become q, U+0323, U+0307; with no precomposed q with a dot, NFC gives the same three code points. The Kelvin sign U+212A canonically decomposes to the ordinary K, U+004B, so even NFC replaces it. And x followed by U+00B2 is unchanged by NFC, while NFKC turns it into U+0078 U+0032, the text x2.

Stated precisely

Write NFD(s)\mathrm{NFD}(s), NFC(s)\mathrm{NFC}(s), NFKD(s)\mathrm{NFKD}(s) and NFKC(s)\mathrm{NFKC}(s) for the normal forms of a string ss of code points, and s‖ts \mathbin{\Vert} t for ss followed by tt. For all strings ss and tt:

Let HH be a hash function and EE the UTF-8 encoding. The rule h(s)=H(E(NFC(s)))h(s) = H(E(\mathrm{NFC}(s))) gives all canonically equivalent strings the same value h(s)h(s). This assumes that every party uses the same form and encoding, and that the strings contain only code points assigned in each party’s Unicode version, so that the stability guarantee applies. Without normalization, different code-point sequences ss and tt give different byte strings E(s)E(s) and E(t)E(t), and so different hashes, except with negligible probability when HH is a cryptographic hash function.

Limits and common mistakes

Normalization does not catch look-alike characters from different scripts. Latin a, U+0061, and Cyrillic a, U+0430, look the same in most fonts, but no normalization form makes them equal. Unicode Technical Standard #39, Unicode Security Mechanisms, defines confusable detection for that problem.

Normalization does not fold case, so NFC leaves A and a different; case-insensitive matching needs case folding, a separate operation. Normalization also leaves invisible characters, such as the zero width space U+200B, in place.

The compatibility forms destroy information on purpose. Use NFKC or NFKD for matching, as in search or identifier comparison, and keep the original text where it must be displayed or signed exactly.

Normalizing pieces and then joining them does not give a normalized string; normalize after joining. Normalization is also not an encoding: two parties that agree on NFC but encode in UTF-8 and in UTF-16 (16-bit Unicode Transformation Format) still hash different bytes.

File systems differ. Most Linux file systems treat a file name as bytes and do not normalize it, while Apple’s older HFS Plus (Hierarchical File System Plus) stores names in a variant of NFD. A name written in NFC can come back in NFD, and a byte-by-byte comparison then misses the file.

Some formats deliberately do not normalize. RFC 8785 defines the JSON §12.26 Canonicalization Scheme (JCS) for JavaScript Object Notation (JSON), a text format for structured data; the Canonical JSON §12.28 entry describes it. JCS applies no Unicode normalization: its section 3.1 requires every component to keep string data as is. Under JCS, U+00E9 and the pair U+0065 U+0301 remain distinct. Hash the canonical UTF-8 bytes of each JSON string, including its quotation marks, with SHA-256. The two digests begin with f2886017 and 3d68ce21, respectively.

Questions answered

Can two strings look the same yet hash differently?

Yes. Two strings can display identically yet consist of different code points, the numbers Unicode assigns to characters and marks, and a hash function, which reads bytes, then gives different results. The letter é can be the single code point U+00E9, encoded in UTF-8 (8-bit Unicode Transformation Format) as the 2 bytes C3 A9, or the letter e followed by the combining acute accent U+0301, encoded as the 3 bytes 65 CC 81. The Unicode Standard calls the two canonically equivalent, yet SHA-256 (Secure Hash Algorithm with a 256-bit output) of the first begins 4a99557e and of the second bf12767b.

The fix is one written rule that every producer and consumer follows: either hash the code points exactly as received, or first convert every string to one Unicode normalization form, usually NFC (Normalization Form C), then encode, then hash. Normalization does not equate look-alike characters from different scripts, such as Latin a and Cyrillic a; those need confusable detection.

Related entries: Canonical JSON (§12.28), Authentication coverage (§5.41), Prefix-free encoding (§15.38), Unicode code points, scalar values, code units, and byte encodings (§12.48), Duplicate JSON key (§15.20). Further reading: [30, 49, 195].