Interface Documentation
Version: 2.-1 (devel)
checksum.hh
Go to the documentation of this file.
1 /*
2  @@@@@@@@ @@ @@@@@@ @@@@@@@@ @@
3  /@@///// /@@ @@////@@ @@////// /@@
4  /@@ /@@ @@@@@ @@ // /@@ /@@
5  /@@@@@@@ /@@ @@///@@/@@ /@@@@@@@@@/@@
6  /@@//// /@@/@@@@@@@/@@ ////////@@/@@
7  /@@ /@@/@@//// //@@ @@ /@@/@@
8  /@@ @@@//@@@@@@ //@@@@@@ @@@@@@@@ /@@
9  // /// ////// ////// //////// //
10 
11  Copyright (c) 2016, Triad National Security, LLC
12  All rights reserved.
13  */
14 #pragma once
15 
18 #include <flecsi/utils/logging.hh>
19 
20 #if !defined(ENABLE_OPENSSL)
21 #error ENABLE_OPENSSL not defined! This file depends on OpenSSL!
22 #endif
23 
24 #include <openssl/evp.h>
25 
26 namespace flecsi {
27 namespace utils {
28 
29 struct checksum_t {
30  unsigned char value[EVP_MAX_MD_SIZE];
31  char strvalue[EVP_MAX_MD_SIZE * 2 + 1];
32  unsigned int length;
33 }; // struct checksum_t
34 
45 template<typename T>
46 void
47 checksum(T * buffer,
48  std::size_t elements,
49  checksum_t & sum,
50  const char * digest = "md5") {
51  std::size_t bytes = elements * sizeof(T);
52 
53  EVP_MD_CTX * ctx = EVP_MD_CTX_create();
54 
58  OpenSSL_add_all_digests();
59 
63  EVP_MD_CTX_init(ctx);
64 
68  const EVP_MD * md = EVP_get_digestbyname(digest);
69  clog_assert(md, "invalid digest");
70 
74  EVP_DigestInit_ex(ctx, md, NULL);
75 
79  EVP_DigestUpdate(ctx, reinterpret_cast<void *>(buffer), bytes);
80 
84  EVP_DigestFinal_ex(ctx, sum.value, &sum.length);
85 
89  EVP_MD_CTX_destroy(ctx);
90 
91  char tmp[256];
92  strcpy(sum.strvalue, "");
93 
94  for(std::size_t i(0); i < sum.length; i++) {
95  sprintf(tmp, "%02x", sum.value[i]);
96  strcat(sum.strvalue, tmp);
97  } // for
98 
99 } // checksum
100 
101 } // namespace utils
102 } // namespace flecsi
Definition: checksum.hh:29
void checksum(T *buffer, std::size_t elements, checksum_t &sum, const char *digest="md5")
Definition: checksum.hh:47
Definition: default_node.hh:20