Ockam logo
Product
OverviewCloud SDKEdge SDKEmbedded SDKRegistryRouter
Contact us

SHA-256

Once we have an initialized vault handle of type ockam_vault_t, we can call any of the functions from ockam/vault.h using this handle.

The vault function ockam_vault_sha256 calculates a SHA-256 hash on input data. It takes an input buffer and the length of the input buffer and returns a 32-byte SHA-256 digest.

1char* input = "hello world";
2size_t input_length = strlen(input);
3
4const size_t digest_size = OCKAM_VAULT_SHA256_DIGEST_LENGTH;
5uint8_t digest[digest_size] = { 0 };
6size_t digest_length;
7
8error = ockam_vault_sha256(&vault,
9 &input[0],
10 input_length,
11 &digest[0],
12 digest_size,
13 &digest_length);
14if (error != OCKAM_ERROR_NONE) { goto exit; }

If the function succeeds the return value is OCKAM_ERROR_NONE.

Complete Example

1#include "ockam/error.h"
2
3#include "ockam/memory.h"
4#include "ockam/memory/stdlib.h"
5
6#include "ockam/random.h"
7#include "ockam/random/urandom.h"
8
9#include "ockam/vault.h"
10#include "ockam/vault/default.h"
11
12#include <stdio.h>
13#include <string.h>
14
15int main(void)
16{
17 int exit_code = 0;
18
19 ockam_error_t error = OCKAM_ERROR_NONE;
20 ockam_error_t deinit_error = OCKAM_ERROR_NONE;
21
22 /* Initialize the default vault using urandom for random and stdlib for memory */
23
24 ockam_memory_t memory = { 0 };
25 ockam_random_t random = { 0 };
26 ockam_vault_t vault = { 0 };
27 ockam_vault_default_attributes_t vault_attributes = { .memory = &memory, .random = &random };
28
29 error = ockam_memory_stdlib_init(&memory);
30 if (error != OCKAM_ERROR_NONE) { goto exit; }
31
32 error = ockam_random_urandom_init(&random);
33 if (error != OCKAM_ERROR_NONE) { goto exit; }
34
35 error = ockam_vault_default_init(&vault, &vault_attributes);
36 if (error != OCKAM_ERROR_NONE) { goto exit; }
37
38 /* Compute the SHA-256 hash of the input string */
39
40 char* input = "hello world";
41 size_t input_length = strlen(input);
42
43 const size_t digest_size = OCKAM_VAULT_SHA256_DIGEST_LENGTH;
44 uint8_t digest[digest_size] = { 0 };
45 size_t digest_length;
46
47 error = ockam_vault_sha256(&vault,
48 &input[0],
49 input_length,
50 &digest[0],
51 digest_size,
52 &digest_length);
53 if (error != OCKAM_ERROR_NONE) { goto exit; }
54
55 int i;
56 for (i = 0; i < digest_size; i++) { printf("%02x", digest[i]); }
57 printf("\n");
58
59exit:
60
61 /* Deinitialize to free resources associated with this handle. */
62
63 deinit_error = ockam_vault_deinit(&vault);
64 ockam_random_deinit(&random);
65 ockam_memory_deinit(&memory);
66
67 if (error == OCKAM_ERROR_NONE) { error = deinit_error; }
68 if (error != OCKAM_ERROR_NONE) { exit_code = -1; }
69 return exit_code;
70}
71
Previous

Generate Random Bytes

Next

Key Generation/ECDH

On this page
  • SHA-256
  • Complete Example
Edit this page
Star Ockam repo