Nachi
Nachi

Reputation: 67

openssl sha256 difference between two way of calculating

I am using openssl sha256 function with C++. What is the difference between the below two way of calculating sha256 ?

Way1: Just call SHA256 method like below

SHA256((unsigned char*)buffer_to_hash, buffer_size, hashed_payload);

Way2: Use SHA256_CTX, SHA256_Init, SHA256_Update and SHA256_Final. As mentioned here generate sha256 with openssl and C++

Both the programs produce the same result

Example for both methods have been mentioned here

Upvotes: 1

Views: 1379

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490008

Modulo bugs and such, the two have basically different intents. The first does batch style processing. That is to say: you give it one complete string, and it produces a hash of that complete string.

The second does hashing incrementally. If you don't have (or want) access to the entire string to be hashed at once, it allows you to read some data, operate on it, read some more, operate on it, and so on until you've read all the data. At the end, you can get the overall hash of the entire data stream.

Just for example, let's assume you're running this on a computer with 4 Gigs of RAM, and you want to hash a 16 gigabyte file. Since the data won't fit in RAM (or even close to it) the second is probably a better choice for this case. Likewise, if you're receiving data over the network, but don't (necessarily) want to store all the raw packets just so you can hash them and (for example) acknowledge receipt.

Upvotes: 2

Related Questions