Type Method
hash(string:)
Computes a SHA-256 digest of a Swift String encoded as UTF-8.
static func hash(string: String) -> SHA256.SHA256Digest
Parameters
-
string
-
The text to hash. UTF-8 encoding is applied unconditionally; callers needing a different encoding should call hash(data:) with explicitly encoded bytes.
Return Value
A SHA256.SHA256Digest of exactly 32 bytes.
Mentioned In
Discussion
A convenience over hash(data:) that hashes the bytes returned by Data(string.utf8). Behavior is identical to hashing those bytes directly; the return type, length, and test-vector conformance (RFC 6234, FIPS 180-4) are the same.
Warning
SHA-256 hashes a byte sequence, not a semantic string. Two visually identical strings with different Unicode normalization forms (NFC vs NFD) produce different digests. When the input may come from user-visible text — filenames, labels, display names — normalize explicitly (for example with String.precomposedStringWithCanonicalMapping) before hashing. See UAX No. 15 for the normalization forms.