How to calculate a SHA-512 hash in C++ on Linux?

≯℡__Kan透↙ 提交于 2019-12-03 11:37:07
Zimbabao

Have you checked OpenSSL. I myself have not used it but documentation says it supports it.

Here is list of few more implementations.

Example code

 md = EVP_get_digestbyname("sha512");
 EVP_MD_CTX_init(&mdctx);
 EVP_DigestInit_ex(&mdctx, md, NULL);
 EVP_DigestUpdate(&mdctx, mess1, strlen(mess1));
 EVP_DigestUpdate(&mdctx, mess2, strlen(mess2));
 EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
 EVP_MD_CTX_cleanup(&mdctx);

Check this code. It is fully portable and does not need any additional configurations. Only STL would suffice. You'll just need to declare

#include "sha512.hh"

and then use the functions

sw::sha512::calculate("SHA512 of std::string") // hash of a string, or
sw::sha512::file(path) // hash of a file specified by its path, or
sw::sha512::calculate(&data, sizeof(data)) // hash of any block of data

whenever you need them. Their return value is std::string

Tamás Szelei

I'm using Botan for various cryptographic purposes. It has many kinds of SHA(-512) algorithms.

When I was looking at C++ crypto libraries I also found Crypto++. The style of the Botan API was more straightforward for me, but both of these libraries are solid and mature.

I have had great success with this:

Secure Hash Algorithm (SHA)

BSD license. It covers SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512. It has neat helper functions reduce steps for simple cases:

SHA256_Data(const sha2_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH])

It also has a lot of performance tuning options.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!