#include "openssl/sha.h"
#include "openssl/crypto.h"
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
void sha1sum(const char *body, const int len, const char *file_name) {
std::stringstream ss;
SHA_CTX c;
unsigned char md[SHA_DIGEST_LENGTH] = {0};
SHA1_Init(&c);
SHA1_Update(&c, (unsigned char *) body, (len));
SHA1_Final(md, &c);
OPENSSL_cleanse(&c, sizeof(c));
ss << file_name << " ";
//////
for (int i = 0; i < SHA_DIGEST_LENGTH; i++) {
//printf("[%02x]", md[i]);
ss << std::hex << std::setfill('0') << std::setw(2) << (int) md[i];
}
printf("%s\n", ss.str().c_str());
}
int main(int argc, const char **argv) {
if (argc != 2) {
cerr << "argc err" << endl;
return 1;
}
std::ifstream is(argv[1], std::ifstream::binary);
if (is) {
// get length of file:
is.seekg(0, is.end);
int length = is.tellg();
is.seekg(0, is.beg);
char *buffer = new char[length];
//std::cout << "Reading " << length << " characters... ";
// read data as a block:
is.read(buffer, length);
if (is) {
//std::cout << "all characters read successfully.";
sha1sum(buffer, length, argv[1]);
} else
std::cout << "error: only " << is.gcount() << " could be read";
is.close();
// ...buffer contains the entire file...
delete[] buffer;
}
return 0;
}
备份地址: 【sha1sum】