aboutsummaryrefslogtreecommitdiff
path: root/src/util/crypto_hash.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-08-19 19:18:17 +0000
committerChristian Grothoff <christian@grothoff.org>2013-08-19 19:18:17 +0000
commit165d153298d75b64a6a426b534a435945690b436 (patch)
tree3c95f417ebda73a68b347ec5c8aa3e18d2262593 /src/util/crypto_hash.c
parent6afb005da9165db3cd814864956f08e23828de63 (diff)
downloadgnunet-165d153298d75b64a6a426b534a435945690b436.tar.gz
gnunet-165d153298d75b64a6a426b534a435945690b436.zip
-remove ShortHashCode from API, deprecate SList
Diffstat (limited to 'src/util/crypto_hash.c')
-rw-r--r--src/util/crypto_hash.c139
1 files changed, 2 insertions, 137 deletions
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c
index 2e436c454..c1d82abae 100644
--- a/src/util/crypto_hash.c
+++ b/src/util/crypto_hash.c
@@ -1,10 +1,10 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009, 2012 Christian Grothoff (and other contributing authors) 3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your 7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version. 8 option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
@@ -17,11 +17,6 @@
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19 19
20 SHA-512 code by Jean-Luc Cooke <jlcooke@certainkey.com>
21
22 Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
23 Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
24 Copyright (c) 2003 Kyle McMartin <kyle@debian.org>
25*/ 20*/
26 21
27/** 22/**
@@ -56,21 +51,6 @@ GNUNET_CRYPTO_hash (const void *block, size_t size, struct GNUNET_HashCode * ret
56 51
57 52
58/** 53/**
59 * Compute short (256-bit) hash of a given block.
60 *
61 * @param block the data to GNUNET_CRYPTO_hash, length is given as a second argument
62 * @param size the length of the data to GNUNET_CRYPTO_hash
63 * @param ret pointer to where to write the hashcode
64 */
65void
66GNUNET_CRYPTO_short_hash (const void *block, size_t size,
67 struct GNUNET_CRYPTO_ShortHashCode * ret)
68{
69 gcry_md_hash_buffer (GCRY_MD_SHA256, ret, block, size);
70}
71
72
73/**
74 * Context used when hashing a file. 54 * Context used when hashing a file.
75 */ 55 */
76struct GNUNET_CRYPTO_FileHashContext 56struct GNUNET_CRYPTO_FileHashContext
@@ -590,119 +570,4 @@ GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
590} 570}
591 571
592 572
593
594/**
595 * Double short (256-bit) hash to create a long hash.
596 *
597 * @param sh short hash to double
598 * @param dh where to store the (doubled) long hash (not really a hash)
599 */
600void
601GNUNET_CRYPTO_short_hash_double (const struct GNUNET_CRYPTO_ShortHashCode *sh,
602 struct GNUNET_HashCode *dh)
603{
604 char *ptr;
605
606 ptr = (char*) dh;
607 memcpy (ptr, sh, sizeof (struct GNUNET_CRYPTO_ShortHashCode));
608 memcpy (&ptr[sizeof (struct GNUNET_CRYPTO_ShortHashCode)], sh, sizeof (struct GNUNET_CRYPTO_ShortHashCode));
609}
610
611
612/**
613 * Truncate doubled short hash back to a short hash.
614 *
615 * @param dh doubled short hash to reduce again
616 * @param sh where to store the short hash
617 * @return GNUNET_OK on success, GNUNET_SYSERR if this was not a
618 * doubled short hash
619 */
620int
621GNUNET_CRYPTO_short_hash_from_truncation (const struct GNUNET_HashCode *dh,
622 struct GNUNET_CRYPTO_ShortHashCode *sh)
623{
624 const struct GNUNET_CRYPTO_ShortHashCode *s;
625
626 s = (const struct GNUNET_CRYPTO_ShortHashCode *) dh;
627 if (0 != memcmp (&s[0],
628 &s[1],
629 sizeof (struct GNUNET_CRYPTO_ShortHashCode)))
630 return GNUNET_SYSERR;
631 *sh = *s;
632 return GNUNET_OK;
633}
634
635
636/**
637 * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
638 *
639 * @param enc the encoding
640 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
641 * @param result where to store the GNUNET_CRYPTO_hash code
642 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
643 */
644int
645GNUNET_CRYPTO_short_hash_from_string2 (const char *enc, size_t enclen,
646 struct GNUNET_CRYPTO_ShortHashCode * result)
647{
648
649 char upper_enc[enclen];
650 char* up_ptr = upper_enc;
651
652 GNUNET_STRINGS_utf8_toupper(enc, &up_ptr);
653 return GNUNET_STRINGS_string_to_data (upper_enc, enclen,
654 (unsigned char*) result,
655 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
656}
657
658
659/**
660 * Convert short hash to ASCII encoding.
661 *
662 * @param block the hash code
663 * @param result where to store the encoding (struct GNUNET_CRYPTO_ShortHashAsciiEncoded can be
664 * safely cast to char*, a '\\0' termination is set).
665 */
666void
667GNUNET_CRYPTO_short_hash_to_enc (const struct GNUNET_CRYPTO_ShortHashCode * block,
668 struct GNUNET_CRYPTO_ShortHashAsciiEncoded *result)
669{
670 char *np;
671
672 np = GNUNET_STRINGS_data_to_string ((const unsigned char *) block,
673 sizeof (struct GNUNET_CRYPTO_ShortHashCode),
674 (char*) result,
675 sizeof (struct GNUNET_CRYPTO_ShortHashAsciiEncoded) - 1);
676 GNUNET_assert (NULL != np);
677 *np = '\0';
678}
679
680/**
681 * Compare function for ShortHashCodes, producing a total ordering
682 * of all hashcodes.
683 *
684 * @param h1 some hash code
685 * @param h2 some hash code
686 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
687 */
688int
689GNUNET_CRYPTO_short_hash_cmp (const struct GNUNET_CRYPTO_ShortHashCode * h1,
690 const struct GNUNET_CRYPTO_ShortHashCode * h2)
691{
692 unsigned int *i1;
693 unsigned int *i2;
694 int i;
695
696 i1 = (unsigned int *) h1;
697 i2 = (unsigned int *) h2;
698 for (i = (sizeof (struct GNUNET_CRYPTO_ShortHashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
699 {
700 if (i1[i] > i2[i])
701 return 1;
702 if (i1[i] < i2[i])
703 return -1;
704 }
705 return 0;
706}
707
708/* end of crypto_hash.c */ 573/* end of crypto_hash.c */