gcrypt-hash.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* GNU Guix --- Functional package management for GNU
  2. Copyright (C) 2012, 2013 Ludovic Courtès <ludo@gnu.org>
  3. This file is part of GNU Guix.
  4. GNU Guix is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or (at
  7. your option) any later version.
  8. GNU Guix is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. #include <gcrypt-hash.hh>
  16. #include <assert.h>
  17. extern "C" {
  18. void
  19. guix_hash_init (struct guix_hash_context *ctx, int algo)
  20. {
  21. gcry_error_t err;
  22. err = gcry_md_open (&ctx->md_handle, algo, 0);
  23. assert (err == GPG_ERR_NO_ERROR);
  24. }
  25. void
  26. guix_hash_update (struct guix_hash_context *ctx, const void *buffer, size_t len)
  27. {
  28. gcry_md_write (ctx->md_handle, buffer, len);
  29. }
  30. void
  31. guix_hash_final (void *resbuf, struct guix_hash_context *ctx,
  32. int algo)
  33. {
  34. memcpy (resbuf, gcry_md_read (ctx->md_handle, algo),
  35. gcry_md_get_algo_dlen (algo));
  36. gcry_md_close (ctx->md_handle);
  37. ctx->md_handle = NULL;
  38. }
  39. }