-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsha3-512.cpp
More file actions
75 lines (61 loc) · 2.25 KB
/
sha3-512.cpp
File metadata and controls
75 lines (61 loc) · 2.25 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//Compile: g++ -O3 -Wall -static sha3-512.cpp -o sha3-512.exe -lssl -lcrypto -lcrypt32 -ladvapi32 -lws2_32 -I./openssl/include
#include <iostream>
#include <fstream>
#include <string>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <algorithm>
#include <limits>
#include <iomanip>
// Function to read a file's content into a string
std::string readFile(const std::string& filePath) {
std::ifstream file(filePath, std::ios::binary);
if (!file) {
return "";
}
std::string fileContents;
file.seekg(0, std::ios::end);
fileContents.resize(file.tellg());
file.seekg(0, std::ios::beg);
file.read(&fileContents[0], fileContents.size());
file.close();
return fileContents;
}
// Function to compute the SHA-3-512 hash using OpenSSL
std::string sha3_512_hash(const std::string& input) {
unsigned char hash[SHA512_DIGEST_LENGTH];
EVP_MD_CTX* ctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(ctx, EVP_sha3_512(), nullptr);
EVP_DigestUpdate(ctx, input.c_str(), input.length());
EVP_DigestFinal_ex(ctx, hash, nullptr);
EVP_MD_CTX_free(ctx);
std::ostringstream hashStream;
for (int i = 0; i < SHA512_DIGEST_LENGTH; ++i) {
hashStream << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
}
return hashStream.str();
}
int main(int argc, char* argv[]) {
std::string filePath;
if (argc > 1) {
filePath = argv[1];
} else {
std::cout << "Enter the file path: ";
std::getline(std::cin, filePath);
}
std::string fileContents = readFile(filePath);
while (fileContents.empty()) {
std::cout << "File not found. Enter the file path: ";
std::getline(std::cin, filePath);
fileContents = readFile(filePath);
}
std::string hash = sha3_512_hash(fileContents);
// Convert the hash to uppercase
std::transform(hash.begin(), hash.end(), hash.begin(), ::toupper);
std::cout << "SHA-3-512 Hash: " << hash << std::endl;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// Wait for the user to press Enter before quitting
std::cout << "Press Enter to Quit...";
std::cin.get();
return 0;
}