Swift · Hashing and Checksums

Hashing and Checksums

import StringZilla 6.0.0-rc.1 · 91a27bdd

hash(seed:) computes a fast 64-bit UInt64 hash of the content, with an optional seed.

let h = "the quick brown fox".hash()
let seeded = "the quick brown fox".hash(seed: 42)
assert(h != seeded)  // a different seed yields a different hash

For data arriving in chunks, StringZillaHasher hashes incrementally. update(_:) is chainable, finalize() and its alias digest() return the UInt64 without consuming the state, and reset(seed:) restarts it.

let hasher = StringZillaHasher(seed: 0)
hasher.update("the quick ").update("brown fox")
let digest = hasher.finalize()
assert(digest == "the quick brown fox".hash())

sha256() returns the SHA-256 digest of the content as a 32-byte [UInt8].

let sum = "hello".sha256()  // [UInt8] of length 32
assert(sum.count == 32)

The streaming StringZillaSha256 mirrors the incremental hasher. update(_:) accepts either a string view or a [UInt8], finalize() and its alias digest() return the 32-byte digest, hexdigest() returns the 64-character lowercase hex string, and reset() restarts it.

let sha = StringZillaSha256()
sha.update("hello, ").update("world")
let hex = sha.hexdigest()  // 64-char hex string
assert(hex.count == 64)

API

ClassStringZillaHasherA progressive hasher for computing StringZilla hashes incrementally.5
StringProtocol+StringZilla.swift:724 ↗
class StringZillaHasher

Use this class when you need to hash data that arrives in chunks or when building up a hash over time.

initCreates a new hasher with the specified seed.
StringProtocol+StringZilla.swift:729 ↗
init(seed: UInt64 = 0)

Parameters

seedThe seed value for the hash function, defaulting to 0.
updateUpdates the hash state with additional string content.
StringProtocol+StringZilla.swift:742 ↗
@discardableResult func update<S>(_ content: S) -> StringZillaHasher where S : StringZillaViewable

Parameters

contentThe string content to add to the hash.

Returns

Self for method chaining.
finalizeFinalizes the hash computation and returns the result.
StringProtocol+StringZilla.swift:752 ↗
func finalize() -> UInt64

Returns

The computed 64-bit hash value.

Note

This is a non-consuming operation and can be called multiple times.

*× 2Alias for finalize().
StringProtocol+StringZilla.swift:759 ↗
func digest() -> UInt64
resetResets the hasher to its initial state.
StringProtocol+StringZilla.swift:764 ↗
func reset(seed: UInt64? = nil)

Parameters

seedNew seed value; the original seed is not retained, so omitting this re-seeds with 0.
ClassStringZillaSha256A progressive SHA-256 hasher for computing cryptographic checksums incrementally.5
StringProtocol+StringZilla.swift:772 ↗
class StringZillaSha256

Use this class when you need to hash data that arrives in chunks or when building up a hash over time.

initCreates a new SHA-256 hasher.
StringProtocol+StringZilla.swift:776 ↗
init()
updateUpdates the hash state with additional data.
StringProtocol+StringZilla.swift:789 ↗
@discardableResult func update<S>(_ content: S) -> StringZillaSha256 where S : StringZillaViewable
@discardableResult func update(_ data: [UInt8]) -> StringZillaSha256

Updates the hash state with raw byte data.

Parameters

contentThe data to add to the hash.
dataThe byte data to add to the hash.

Returns

Self for method chaining.
finalizeFinalizes the hash computation and returns the result as a 32-byte array.
StringProtocol+StringZilla.swift:812 ↗
func finalize() -> [UInt8]

Returns

The computed SHA-256 digest.

Note

This is a non-consuming operation and can be called multiple times.

hexdigestReturns the current SHA-256 hash as a lowercase hexadecimal string.
StringProtocol+StringZilla.swift:827 ↗
func hexdigest() -> String

Returns

A 64-character hex string.
resetResets the hasher to its initial state.
StringProtocol+StringZilla.swift:840 ↗
func reset()