C++ Hash Functions in Linux: MD5, SHA-1, SHA-256 and More
There are several hash functions available for C++ in Linux. Here are a few commonly used ones:
- MD5: The MD5 hash function is commonly used for generating a 128-bit hash value. It is available in Linux through libraries like OpenSSL or Crypto++. Example usage:
#include <openssl/md5.h>
std::string md5_hash(const std::string& input) {
unsigned char digest[MD5_DIGEST_LENGTH];
MD5((const unsigned char*)input.c_str(), input.length(), digest);
// Convert hash to string
std::stringstream ss;
for(int i = 0; i < MD5_DIGEST_LENGTH; i++)
ss << std::hex << std::setw(2) << std::setfill('0') << (int)digest[i];
return ss.str();
}
- SHA-1: The SHA-1 hash function generates a 160-bit hash value. It is available in Linux through libraries like OpenSSL or Crypto++. Example usage:
#include <openssl/sha.h>
std::string sha1_hash(const std::string& input) {
unsigned char digest[SHA_DIGEST_LENGTH];
SHA1((const unsigned char*)input.c_str(), input.length(), digest);
// Convert hash to string
std::stringstream ss;
for(int i = 0; i < SHA_DIGEST_LENGTH; i++)
ss << std::hex << std::setw(2) << std::setfill('0') << (int)digest[i];
return ss.str();
}
- SHA-256: The SHA-256 hash function generates a 256-bit hash value. It is available in Linux through libraries like OpenSSL or Crypto++. Example usage:
#include <openssl/sha.h>
std::string sha256_hash(const std::string& input) {
unsigned char digest[SHA256_DIGEST_LENGTH];
SHA256((const unsigned char*)input.c_str(), input.length(), digest);
// Convert hash to string
std::stringstream ss;
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
ss << std::hex << std::setw(2) << std::setfill('0') << (int)digest[i];
return ss.str();
}
Note that these are just a few examples, and there are many other hash functions available for C++ in Linux. The choice of hash function depends on your specific requirements and security needs.
原文地址: https://www.cveoy.top/t/topic/qqrq 著作权归作者所有。请勿转载和采集!