计算文件的SHA256哈希值

#include <iostream>
#include <openssl/sha.h>
#include <string>
#include <string.h>
#include <vector>
#include <fstream>
using namespace std;

std::vector<char> readfile(const char* filename) {
  std::vector<char> data;
  std::fstream fs; 
  fs.open(filename, std::ios::in);
  char buffer;
  while (!(fs.eof()) && fs.get(buffer)) {
    data.push_back((char)buffer);
  }
  fs.close();
  return data;
}
int main(int argc, char**argv)
{
    unsigned char md[33] = {0};
    vector<char> filedata = readfile(argv[1]);
    SHA256((const unsigned char *)filedata.data(), filedata.size(), md);

    string sha256_res = "";
    char buf[3] = {0};
    for(int i = 0; i < 32; i++ )
    {
        sprintf(buf,"%02x", md[i]);
        sha256_res.append(buf);
    }
    cout << sha256_res << endl;
    return 0;
}


上一篇:最后看一下什么是线程本地存储(TLS)


下一篇:【快速文档】slider标签,在小程序中插入一个滑动选择器