@@ -371,6 +371,64 @@ include a `salt <https://en.wikipedia.org/wiki/Salt_%28cryptography%29>`_.
371371 .. versionadded :: 3.6
372372
373373
374+ .. _hashlib-saslprep :
375+
376+ String preparation
377+ ------------------
378+
379+ .. function :: saslprep(data, *, allow_unassigned_code_points)
380+
381+ Prepare a Unicode string according to :rfc: `4013 ` (SASLprep), which is a
382+ profile of the :rfc: `3454 ` *stringprep * algorithm. SASLprep is used to
383+ normalise usernames and passwords before they are transmitted in
384+ authentication protocols such as SASL (e.g. SMTP, IMAP, LDAP).
385+
386+ *data * may be a :class: `str ` or :class: `bytes `. Byte strings are returned
387+ unchanged. Unicode strings are processed in four steps:
388+
389+ 1. **Map ** — non-ASCII space characters (table C.1.2) are replaced with
390+ ``U+0020 ``; characters commonly mapped to nothing (table B.1) are
391+ removed.
392+ 2. **Normalise ** — the string is normalised using Unicode NFKC.
393+ 3. **Prohibit ** — a :exc: `ValueError ` is raised if the string contains
394+ any character from the RFC 4013 prohibited-output tables (control
395+ characters, private-use characters, non-characters, and others).
396+ 4. **Bidi check ** — a :exc: `ValueError ` is raised if the string mixes
397+ right-to-left and left-to-right text in a way that violates
398+ :rfc: `3454 ` section 6.
399+
400+ *allow_unassigned_code_points * must be supplied as a keyword argument.
401+ Pass ``False `` for *stored strings * such as passwords stored in a
402+ database (unassigned code points are prohibited, per :rfc: `3454 `
403+ section 7). Pass ``True `` for *queries * such as a password typed at a
404+ prompt (unassigned code points are permitted). Always pass this
405+ explicitly; there is no default.
406+
407+ Returns the prepared :class: `str `, or the original *data * unchanged if
408+ it is a :class: `bytes ` object.
409+
410+ >>> from hashlib import saslprep
411+ >>> saslprep(" I\u00AD X" , allow_unassigned_code_points = False ) # soft hyphen removed
412+ 'IX'
413+ >>> saslprep(" \u2168 " , allow_unassigned_code_points = False ) # Roman numeral IX
414+ 'IX'
415+ >>> saslprep(b " user" , allow_unassigned_code_points = False ) # bytes unchanged
416+ b'user'
417+
418+ .. versionadded :: 3.15
419+
420+ .. seealso ::
421+
422+ :rfc: `4013 `
423+ SASLprep: Stringprep Profile for User Names and Passwords.
424+
425+ :rfc: `3454 `
426+ Preparation of Internationalized Strings ("stringprep").
427+
428+ :mod: `stringprep `
429+ The underlying Unicode character tables used by this function.
430+
431+
374432.. _hashlib-blake2 :
375433
376434BLAKE2
0 commit comments