Skip to content

Commit

Permalink
add cmake usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
franziskuskiefer committed Nov 30, 2022
1 parent 649ed38 commit 2526e14
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/cmake/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
25 changes: 25 additions & 0 deletions examples/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.10)

include(FetchContent)
FetchContent_Declare(hacl
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
GIT_REPOSITORY https://github.com/cryspen/hacl-packages/
GIT_TAG 88560a8b5bae5db890e11f1ea71376adfa767baf
)
FetchContent_MakeAvailable(hacl)

project(hacl-blake-example)
set(CMAKE_OSX_ARCHITECTURES arm64)

add_executable(example blake-example.cc)

# Add includes from HACL
target_include_directories(example PRIVATE
${hacl_SOURCE_DIR}/include
${hacl_SOURCE_DIR}/build
${hacl_SOURCE_DIR}/karamel/include
${hacl_SOURCE_DIR}/karamel/krmllib/dist/minimal
${hacl_SOURCE_DIR}/vale/include
)
# Link the HACL library
target_link_libraries(example PRIVATE hacl)
12 changes: 12 additions & 0 deletions examples/cmake/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# CMake Example

This folder contains an example for how to use the HACL Packages C library in a
CMake project.

```sh
mkdir build
cd build
cmake ..
cmake --build .
./example
```
44 changes: 44 additions & 0 deletions examples/cmake/blake-example.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2022 Cryspen Sarl
*
* Licensed under the Apache License, Version 2.0 or MIT.
* - http://www.apache.org/licenses/LICENSE-2.0
* - http://opensource.org/licenses/MIT
*/
#include "Hacl_Hash_Blake2.h"

using namespace std;

void print_hex_ln(size_t bytes_len, uint8_t *bytes)
{
for (int i = 0; i < bytes_len; ++i)
{
printf("%02x", bytes[i]);
}

printf("\n");
}

int main(int argc, char const *argv[])
{
// Reserve memory for a 64 byte digest, i.e.,
// for a BLAKE2B run with full 512-bit output.
uint32_t output_len = 64;
uint8_t output[64];

// The message we want to hash.
const char *message = "Hello, HACL Packages!";
uint32_t message_len = strlen(message);

// BLAKE2B can be used as an HMAC, i.e., with a key.
// We don't want to use a key here and thus provide a zero-sized key.
uint32_t key_len = 0;
uint8_t *key = 0;

Hacl_Blake2b_32_blake2b(
output_len, output, message_len, (uint8_t *)message, key_len, key);

print_hex_ln(output_len, output);

return 0;
}

0 comments on commit 2526e14

Please sign in to comment.