<p>CSHA256 itself mimics OpenSSL's SHA256_CTX, and its constructor mimics the Init function. The Write method corresponds to the Update function. The Finalize method matches the Final function.</p>

<p>In short: you construct a CSHA256 object, call Write any number of times to feed it bytes to hash, and then call Finalize to compute the resulting hash.</p>

CC BY-SA 4.0
2019-06-06T08:25:40.277Z
Could you explain the difference between valtype and uint256? Why is one used in some parts of the code and the other in other parts of the code?
kcalvinalvin
CC BY-SA 4.0
2019-06-06T08:29:07.407Z
valtype is an alias for a byte vector. uint256 is a 256-bit blob.
Pieter Wuille
CC BY-SA 4.0
2019-06-06T08:17:41.967Z
2019-06-06T08:17:41.967Z
Pieter Wuille
<p>Looking at Bitcoin Core implementation of SHA256, I noticed it was a bit different from OpenSSL's implementation.</p>

<p>In OpenSSL, you would initialize a variable with SHA256_CTX when in Bitcoin Core it's done with either valtype (aka. std::vector<code>&lt;unsigned char&gt;</code>) or uint256. Also, you would go through the steps INIT, Update, Final.</p>

<p>In Bitcoin Core, it's Write and Finalize.</p>

<p>It would be awesome if someone could go through the basic steps and perhaps explain why it's implemented differently. Thank you!</p>

<bitcoin-core><cryptography><sha256>
What's the purpose of CSHA256::Write and CSHA256::Finalize
kcalvinalvin
# What's the purpose of CSHA256::Write and CSHA256::Finalize

(Asked by: username_0 on 2019-06-06)

Looking at Bitcoin Core implementation of SHA256, I noticed it was a bit different from OpenSSL's implementation.

In OpenSSL, you would initialize a variable with SHA256\_CTX when in Bitcoin Core it's done with either valtype (aka. std::vector`<unsigned char>`) or uint256. Also, you would go through the steps INIT, Update, Final.

In Bitcoin Core, it's Write and Finalize.

It would be awesome if someone could go through the basic steps and perhaps explain why it's implemented differently. Thank you!

**Tags:** bitcoin-core, cryptography, sha256


---

## 1 answers

### Answer by username_1 on 2019-06-06. Score: 1 | Accepted answer

CSHA256 itself mimics OpenSSL's SHA256\_CTX, and its constructor mimics the Init function. The Write method corresponds to the Update function. The Finalize method matches the Final function.

In short: you construct a CSHA256 object, call Write any number of times to feed it bytes to hash, and then call Finalize to compute the resulting hash.

> **Comments:**
> **username_0**: Could you explain the difference between valtype and uint256? Why is one used in some parts of the code and the other in other parts of the code?  
> **username_1**: valtype is an alias for a byte vector. uint256 is a 256-bit blob.  

---

