Advertisement
rockdrilla

C++11 : OpenSSL : SHA256

Sep 29th, 2014
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. /*
  2. g++ test.cxx -o test -std=c++11 -lcrypto
  3. */
  4.  
  5. #include <cstring>
  6. #include <ctime>
  7. #include <fstream>
  8. #include <functional>
  9. #include <iomanip>
  10. #include <iostream>
  11. #include <sstream>
  12. #include <string>
  13.  
  14. #include "openssl/sha.h"
  15.  
  16. std::string sha256_generic(const std::function<void(const void ** data, size_t * length)> & callback) {
  17.     unsigned char hash[SHA256_DIGEST_LENGTH];
  18.     SHA256_CTX ctx;
  19.     SHA256_Init(&ctx);
  20.  
  21.     const void * data;
  22.     size_t length;
  23.     while (true) {
  24.         callback(&data, &length);
  25.         if (length == 0 || data == nullptr)
  26.             break;
  27.  
  28.         SHA256_Update(&ctx, data, length);
  29.     }
  30.  
  31.     SHA256_Final(hash, &ctx);
  32.  
  33.     std::ostringstream ostr;
  34.     ostr << std::hex << std::setfill('0') << std::right;
  35.     for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
  36.         ostr << std::setw(2) << static_cast<int>(hash[i]);
  37.     }
  38.  
  39.     return ostr.str();
  40. }
  41.  
  42. std::string sha256_string(const std::string & str) {
  43.     bool done = false;
  44.  
  45.     return sha256_generic(
  46.     [&](const void ** data, size_t * length) {
  47.         if (done) {
  48.             *data = nullptr;
  49.             *length = 0;
  50.         } else {
  51.             *data = static_cast<const void*>(str.c_str());
  52.             *length = str.length();
  53.             done = true;
  54.         }
  55.     } );
  56. }
  57.  
  58. std::string sha256_file(const std::string & filename, size_t buffer_size = 65536) {
  59.     std::ifstream ifile;
  60.     ifile.open(filename, std::ios::in | std::ios::binary);
  61.     if (! ifile.is_open()) {
  62.         return "";
  63.     }
  64.  
  65.     char * buffer = new char[buffer_size];
  66.  
  67.     return sha256_generic(
  68.     [&](const void ** data, size_t * length) {
  69.         std::streamsize read = ifile.readsome(buffer, static_cast<std::streamsize>(buffer_size));
  70.         if (read > 0) {
  71.             *data = buffer;
  72.             *length = static_cast<size_t>(read);
  73.         } else {
  74.             *data = nullptr;
  75.             *length = 0;
  76.  
  77.             delete [] buffer;
  78.  
  79.             ifile.close();
  80.         }
  81.     } );
  82. }
  83.  
  84. int main(int argc, char* argv[]) {
  85.     std::cout << sha256_string("") << std::endl;
  86.     std::cout << sha256_string("Hello, World!") << std::endl;
  87.  
  88.     std::cout << sha256_file("test.cxx") << std::endl;
  89.     std::cout << sha256_file("test") << std::endl;
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement