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

4

15

Is there a standard library or commonly used library that can be used for calculating SHA-512 hashes on Linux?

I'm looking for a C or C++ library.

Swansea answered 26/2, 2011 at 4:21 Comment(0)
C
12

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);
Cordite answered 26/2, 2011 at 4:39 Comment(4)
Heh. You beat me to it because "OpenSSL supports SHA512." is only 24 characters and answers must be at least 30.Rehnberg
Can you link to the documentation for the function I can use for calculating the hash?Swansea
To find the code sample in the manual pages do: man EVP_MD_CTX_init on Linux.Longmire
The last version of the Wikipedia article to list implementations was this 1 March 2014 revision. I’ve updated the link above to point there. The entire section was removed later the same day: “This list is far-far from complete and already uselessly long, fails WP:NOT”. NIST maintains a list of validated implementations; at this writing, there are 3,761 entries.Materi
A
11

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

Artie answered 15/2, 2016 at 7:30 Comment(1)
The software you linked to does create accurate hashes for cases where only a single .update() call is made, but fails when there are multiple .update() calls. Just FYI.Hewett
P
7

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.

Parahydrogen answered 25/10, 2011 at 16:25 Comment(1)
@Gracchus In many cases there are no specific docs for some algorithms. The reason is that each algorithm is implemented as a filter which you can use very similarly to others. If you are interested in specific parameters, look up the algorithm itself on wikipedia because the implementation should be canonical. Comments in the headers might also help. The manual provides information about using the filters.Vestal
G
1

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.

Greatniece answered 25/10, 2011 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.