# Hashing and Checksums

> StringZilla for Swift

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

```swift
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.

```swift
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]`.

```swift
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.

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

`import StringZilla`

## StringZillaHasher

A progressive hasher for computing StringZilla hashes incrementally.

```swift
class StringZillaHasher
```

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

## init

Creates a new hasher with the specified seed.

```swift
init(seed: UInt64 = 0)
```

- `seed`: The seed value for the hash function, defaulting to 0.

## update

Updates the hash state with additional string content.

```swift
@discardableResult func update<S>(_ content: S) -> StringZillaHasher where S : StringZillaViewable
```

- `content`: The string content to add to the hash.

## finalize

Finalizes the hash computation and returns the result.

```swift
func finalize() -> UInt64
```

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

## *

Alias for `finalize()`.

```swift
func digest() -> UInt64
```

## reset

Resets the hasher to its initial state.

```swift
func reset(seed: UInt64? = nil)
```

- `seed`: New seed value; the original seed is not retained, so omitting this re-seeds with 0.

## StringZillaSha256

A progressive SHA-256 hasher for computing cryptographic checksums incrementally.

```swift
class StringZillaSha256
```

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

## init

Creates a new SHA-256 hasher.

```swift
init()
```

## update

Updates the hash state with additional data.

```swift
@discardableResult func update<S>(_ content: S) -> StringZillaSha256 where S : StringZillaViewable
```

```swift
@discardableResult func update(_ data: [UInt8]) -> StringZillaSha256
```

Updates the hash state with raw byte data.

- `content`: The data to add to the hash.
- `data`: The byte data to add to the hash.

## finalize

Finalizes the hash computation and returns the result as a 32-byte array.

```swift
func finalize() -> [UInt8]
```

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

## hexdigest

Returns the current SHA-256 hash as a lowercase hexadecimal string.

```swift
func hexdigest() -> String
```

## reset

Resets the hasher to its initial state.

```swift
func reset()
```

