Hey hackers,
I'm seeing some behavior that confuses me a bit. I'm sure I'm doing something wrong, but I can't tell.
Here's a minimal example:
/ | #include <stdio.h> | #include <nettle/rsa.h> | | int main ( int argc, char ** argv ) { | struct sha1_ctx hash_ctx; | uint8_t headers[] = { | 0xDE, 0xEA, 0xAD, | 0xBE, 0xEF | }; | sha1_update(&hash_ctx, 5, headers); | | for (int i = 0; i < 3; ++i) { | uint8_t _fingerprint[SHA1_DIGEST_SIZE]; | sha1_digest(&hash_ctx, SHA1_DIGEST_SIZE, _fingerprint); | for (int i = 0; i < SHA1_DIGEST_SIZE; ++i) | printf("%02X ", _fingerprint[i]); | printf("\n"); | } | } \
The output, when run is:
/ | 89 22 AA 95 F2 02 C9 BB 35 FC 2D A2 DE 22 BE 48 2B B6 6C 1D | DA 39 A3 EE 5E 6B 4B 0D 32 55 BF EF 95 60 18 90 AF D8 07 09 | DA 39 A3 EE 5E 6B 4B 0D 32 55 BF EF 95 60 18 90 AF D8 07 09 \
(that last line is stable after the inital hash)
Does doing the sha1_digest alter state in some way? I can't image it is, but then how does this behavior crop up?
Much love, thanks all!
Cheers, Paul
-- #define sizeof(x) rand() :wq
Paul Tagliamonte paultag@gmail.com writes:
Here's a minimal example:
/ | #include <stdio.h> | #include <nettle/rsa.h> | | int main ( int argc, char ** argv ) { | struct sha1_ctx hash_ctx; | uint8_t headers[] = { | 0xDE, 0xEA, 0xAD, | 0xBE, 0xEF | }; | sha1_update(&hash_ctx, 5, headers);
You must call sha1_init(&hash_ctx) before the first sha1_update.
Does doing the sha1_digest alter state in some way? I can't image it is, but then how does this behavior crop up?
sha1_digest implies a sha1_init. So with sha1_digest(...); sha1_digest(...), the second call gives you the sha1 digest of the empty string. I think this is explained in the manual, http://www.lysator.liu.se/~nisse/nettle/nettle.html#Legacy-hash-functions
Regards, /Niels
On Thu, Nov 28, 2013 at 06:38:47AM +0100, Niels Möller wrote:
You must call sha1_init(&hash_ctx) before the first sha1_update.
Erm, yes. Sorry, the real code has this :)
Does doing the sha1_digest alter state in some way? I can't image it is, but then how does this behavior crop up?
sha1_digest implies a sha1_init. So with sha1_digest(...); sha1_digest(...), the second call gives you the sha1 digest of the empty string. I think this is explained in the manual, http://www.lysator.liu.se/~nisse/nettle/nettle.html#Legacy-hash-functions
Erm, so it is. Sorry 'bout that. Thanks!
Regards, /Niels
-- Niels Möller. PGP-encrypted email is preferred. Keyid C0B98E26. Internet email is subject to wholesale government surveillance.
Cheers, Paul
nettle-bugs@lists.lysator.liu.se