aboutsummaryrefslogtreecommitdiff
path: root/src/util/crypto_hash.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2015-01-09 16:27:28 +0000
committerChristian Grothoff <christian@grothoff.org>2015-01-09 16:27:28 +0000
commit712767683a32140be3fd7d195b44be676853eb12 (patch)
treece558dd6d53a94f983aac55dafc56351641802a1 /src/util/crypto_hash.c
parentefd634ccf636b870b2dbd79f8969f8999c5573fa (diff)
downloadgnunet-712767683a32140be3fd7d195b44be676853eb12.tar.gz
gnunet-712767683a32140be3fd7d195b44be676853eb12.zip
adding API for incremental hashing (from Taler)
Diffstat (limited to 'src/util/crypto_hash.c')
-rw-r--r--src/util/crypto_hash.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c
index d68e890a7..0be6d4096 100644
--- a/src/util/crypto_hash.c
+++ b/src/util/crypto_hash.c
@@ -588,4 +588,85 @@ GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
588} 588}
589 589
590 590
591/**
592 * Context for cummulative hashing.
593 */
594struct GNUNET_HashContext
595{
596 /**
597 * Internal state of the hash function.
598 */
599 gcry_md_hd_t hd;
600};
601
602
603/**
604 * Start incremental hashing operation.
605 *
606 * @return context for incremental hash computation
607 */
608struct GNUNET_HashContext *
609GNUNET_CRYPTO_hash_context_start ()
610{
611 struct GNUNET_HashContext *hc;
612
613 hc = GNUNET_new (struct GNUNET_HashContext);
614 GNUNET_assert (0 ==
615 gcry_md_open (&hc->hd,
616 GCRY_MD_SHA512,
617 0));
618 return hc;
619}
620
621
622/**
623 * Add data to be hashed.
624 *
625 * @param hc cummulative hash context
626 * @param buf data to add
627 * @param size number of bytes in @a buf
628 */
629void
630GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
631 const void *buf,
632 size_t size)
633{
634 gcry_md_write (hc->hd, buf, size);
635}
636
637
638/**
639 * Finish the hash computation.
640 *
641 * @param hc hash context to use
642 * @param r_hash where to write the latest / final hash code
643 */
644void
645GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
646 struct GNUNET_HashCode *r_hash)
647{
648 const void *res = gcry_md_read (hc->hd, 0);
649
650 GNUNET_assert (NULL != res);
651 if (NULL != r_hash)
652 memcpy (r_hash,
653 res,
654 sizeof (struct GNUNET_HashCode));
655 GNUNET_CRYPTO_hash_context_abort (hc);
656}
657
658
659/**
660 * Abort hashing, do not bother calculating final result.
661 *
662 * @param hc hash context to destroy
663 */
664void
665GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc)
666{
667 gcry_md_close (hc->hd);
668 GNUNET_free (hc);
669}
670
671
591/* end of crypto_hash.c */ 672/* end of crypto_hash.c */