added level hashes and finished credits

This commit is contained in:
2011-04-19 12:16:52 +02:00
parent f3ffdc3e31
commit 3950195593
6 changed files with 136 additions and 15 deletions
+42
View File
@@ -53,3 +53,45 @@ std::string sha256_hash (std::string input) {
return std::string (result_buf, 64);
}
std::string sha256_hash_file (const char *filename) {
// we simply read the whole file into a string and hash this
std::ifstream file_stream (filename, std::ios_base::binary);
if (!file_stream.is_open()) {
std::cerr << "Could not hash file " << filename << ": could not open file." << std::endl;
return "";
}
file_stream.seekg (0,std::ios_base::end);
std::streampos file_end = file_stream.tellg();
int file_size = static_cast<int> (file_end);
if (file_size == 0) {
std::cerr << "Could not hash file " << filename << ": file has no content!" << std::endl;
return "";
}
unsigned char *file_buffer = NULL;
file_buffer = new unsigned char[file_size];
file_stream.seekg(0, std::ios_base::beg);
file_stream.read ((char*)file_buffer, file_size);
char result_buf[64];
SHA256_CTX ctx256;
SHA256_Init(&ctx256);
SHA256_Update(&ctx256, file_buffer, file_size);
SHA256_End (&ctx256, result_buf);
file_stream.close();
delete[] file_buffer;
// std::cerr << "file hash is " << std::string (result_buf, 64) << std::endl;
return std::string (result_buf, 64);
}
+1
View File
@@ -6,5 +6,6 @@
std::string strip_whitespaces (const std::string input_str);
bool create_dir (const std::string &dir_str);
std::string sha256_hash (std::string input);
std::string sha256_hash_file (const char *filename);
#endif /* _UTILS_H */