m8

A general-purpose preprocessor for metaprogramming.


m8

/

src

/

ob

/

crypto.cc

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include "crypto.hh"

#include <openssl/sha.h>

#include <string>
#include <sstream>
#include <iomanip>

namespace OB
{

namespace Crypto
{

std::string sha256(std::string const str)
{
  unsigned char digest[SHA256_DIGEST_LENGTH];
  SHA256_CTX sha256;
  SHA256_Init(&sha256);
  SHA256_Update(&sha256, str.c_str(), str.size());
  SHA256_Final(digest, &sha256);
  std::stringstream out;
  for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
  {
    out << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(digest[i]);
  }
  return out.str();
}

} // namespace Crypto

} // namespace OB
Back to Top