9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] sha2_256 behavior and hashing functions in general
@ 2025-04-19 15:50 i
  0 siblings, 0 replies; only message in thread
From: i @ 2025-04-19 15:50 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 1409 bytes --]

I see in tlshand.c in libsec:

        // get current hash value, but allow further messages to be hashed in
        sha2_256(nil, 0, seed, &hsh.sha2_256);

I assume passing nil and 0 allows the hashing function to just dump the current hash in seed in our case.

I also assume multiple calls with nil and 0 will return the same value.

It doesn't!

Calling sha2_256 with nil and 0 again returns a different value.

Am I misunderstanding the behavior or is this just incomplete?

Test program:

#include <u.h>
#include <libc.h>
#include <libsec.h>

void
main(void)
{
uchar d1[SHA2_256dlen], d2[SHA2_256dlen], d3[SHA2_256dlen], s1[] = "hello,", s2[] = "world";
DigestState s;
int i;

sha2_256(s1, 6, nil, &s);
sha2_256(s2, 5, d1, &s);
print( "main: 1st call: d1: \n");
for(i = 0; i < SHA2_256dlen; i++) {
print("%02x", d1[i]);
}
print("\n");
sha2_256(nil, 0, d2, &s);
print( "main: 2st call: d2: \n");
for(i = 0; i < SHA2_256dlen; i++) {
print("%02x", d2[i]);
}
print("\n");
sha2_256(nil, 0, d3, &s);
print( "main: 3rd call: d3: \n");
for(i = 0; i < SHA2_256dlen; i++) {
print("%02x", d3[i]);
}
print("\n");
exits(0);
}
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T47e5e753bbea6149-Md6163eb2b0836a5b4c8ebcbe
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

[-- Attachment #2: Type: text/html, Size: 2838 bytes --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2025-04-19 15:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-19 15:50 [9fans] sha2_256 behavior and hashing functions in general i

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).