---
title: "SHA-256"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/subsys/crypto/sha256/
last_updated: 2026-11-18
sdk_version: 4.44
---
# SHA-256

Source: https://developer.repebble.com/pebbleos/docs/samples/subsys/crypto/sha256/

This sample hashes the two example messages of FIPS 180-4 with [SHA-256](https://developer.repebble.com/pebbleos/docs/subsystems/crypto.md), one of them two blocks long, then a thousand bytes both in one call and fed in uneven chunks: the context keeps a partial block until the next update completes it, so both digests are the same.

Boards with a hash accelerator use it for long runs of blocks, and software for the rest.

Run:

`pbl samples samples/subsys/crypto/sha256`

Boards:

every board

## Sources

src/main.c

```
#include \<stdint.h\>#include \<stdio.h\>#include \<string.h\>#include \<pbl/crypto/sha256.h\>#include \<pbl/util/math.h\>#define DATA\_LEN 1000Ustatic uint8\_t s\_data[DATA\_LEN];static void prv\_print(const char \*what, const uint8\_t digest[PBL\_SHA256\_DIGEST\_SIZE]) {printf("sha256: %s: ", what);for (size\_t i = 0; i \< PBL\_SHA256\_DIGEST\_SIZE; i++) {printf("%02x", digest[i]);}printf("\n");}int main(void) {static const char two\_blocks[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";uint8\_t digest[PBL\_SHA256\_DIGEST\_SIZE], chunked[PBL\_SHA256\_DIGEST\_SIZE];struct pbl\_sha256\_ctx ctx;size\_t off = 0, chunk = 1;pbl\_sha256("abc", 3, digest);prv\_print("\"abc\"", digest);pbl\_sha256(two\_blocks, strlen(two\_blocks), digest);prv\_print("56 bytes, two blocks", digest);for (size\_t i = 0; i \< DATA\_LEN; i++) {s\_data[i] = (uint8\_t)(i \* 7U);}pbl\_sha256(s\_data, DATA\_LEN, digest);/\* Partial blocks are kept in the context until the next update completes them. \*/pbl\_sha256\_init(&ctx);while (off \< DATA\_LEN) {size\_t len = MIN(chunk, DATA\_LEN - off);pbl\_sha256\_update(&ctx, &s\_data[off], len);off += len;chunk = chunk \* 7U % 97U + 1U;}pbl\_sha256\_final(&ctx, chunked);printf("sha256: %u bytes in uneven chunks, the same as in one go: %s\n", DATA\_LEN,memcmp(digest, chunked, sizeof(digest)) == 0 ? "yes" : "no");printf("Sample done\n");return 0;}
```

## Output

```
sha256: "abc": ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
sha256: 56 bytes, two blocks: 248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1
sha256: 1000 bytes in uneven chunks, the same as in one go: yes
Sample done
```

Generated from PebbleOS [7753e801d](https://github.com/coredevices/pebbleos/commit/7753e801d93d20f2d37534ce2fa69d9952015237). This page is maintained in the pebbleos repository: [docs/samples/subsys/crypto/sha256.md](https://github.com/coredevices/pebbleos/blob/7753e801d93d20f2d37534ce2fa69d9952015237/docs/samples/subsys/crypto/sha256.md).
