SHA256 examples? What is HashWordType?

37 views
Skip to first unread message

Jefferson Carpenter

unread,
Jan 3, 2018, 1:48:53 AM1/3/18
to Crypto++ Users
Hello,

Is there any example code for using this library's SHA256 algorithm?  What is a HashWordType?

The code that comes to mind is:

HashWordType sum;
SHA256::InitState(sum);
SHA256::Transform(sum, (const HashWordType*)data);

but I'm totally unsure about just casting the data to a HashWordType pointer in the 3rd line, and also not entirely sure whether / why InitState is necessary to call before calling Transform.  There's surely a correct way to get your data into a HashWordType*, but unfortunately on the docs page I'm looking at (https://www.cryptopp.com/docs/ref/class_s_h_a256.html) HashWordType is not a hyperlink, it's just opaque text.

Jefferson Carpenter

Jeffrey Walton

unread,
Jan 3, 2018, 2:27:29 AM1/3/18
to Jefferson Carpenter, Crypto++ Users
On Wed, Jan 3, 2018 at 1:48 AM, Jefferson Carpenter
<jefferson...@gmail.com> wrote:
> Hello,
>
> Is there any example code for using this library's SHA256 algorithm? What
> is a HashWordType?

Checkout the wiki at https://www.cryptopp.com/wiki/Hash_Functions .
Our pages on hash functions are kind of lame, but its about as
difficult as:

byte digest[SHA1::DIGESTSIZE];
byte message[] = { ... };

SHA1 hash;
hash.Update(message, sizeof(message));
hash.Final(digest);

> HashWordType sum;
> SHA256::InitState(sum);
> SHA256::Transform(sum, (const HashWordType*)data);
>
> but I'm totally unsure about just casting the data to a HashWordType pointer
> in the 3rd line, and also not entirely sure whether / why InitState is
> necessary to call before calling Transform. There's surely a correct way to
> get your data into a HashWordType*, but unfortunately on the docs page I'm
> looking at (https://www.cryptopp.com/docs/ref/class_s_h_a256.html)
> HashWordType is not a hyperlink, it's just opaque text.

Yeah, HashWordType is mostly internal except when it bleeds through
like now. It is a word32 or word64, and it is specified in the
template parameters.

What you usually want to use is the base class members from
HashTransformation . That interface gives you Update and Final. Also
see https://www.cryptopp.com/docs/ref/class_hash_transformation.html .

The interface you are seeing - InitState and Transform - is the
"static" portion of IteratedHashWithStaticTransform. The short of it
is, the interface allows use cases like MDC, where a user password is
used as initial state, and the hash transformation is the traditional
hash function. Also see
https://www.cryptopp.com/docs/ref/mdc_8h_source.html .

Jeff
Reply all
Reply to author
Forward
0 new messages