source: roaraudio/libroar/hash.c @ 4451:839e9e3f1b46

Last change on this file since 4451:839e9e3f1b46 was 4451:839e9e3f1b46, checked in by phi, 14 years ago

implement hashes based un libgcrypt

File size: 4.8 KB
Line 
1//hash.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38#ifdef ROAR_HAVE_LIBGCRYPT
39#include <gcrypt.h>
40#endif
41
42static const struct hashes {
43 const int    id;
44 const char * name;
45} _libroar_hashes[] = {
46/*
47grep '^  +HT_' doc/new-cmds | sed 's/ *#(.*)$//; s/^  +HT_//; s/ *=.*$//' | while read n; do printf " {ROAR_HT_%-12s \"%-12s},\n" $n, $n\"; done
48*/
49 {ROAR_HT_NONE,        "NONE"       },
50 {ROAR_HT_MD5,         "MD5"        },
51 {ROAR_HT_SHA1,        "SHA1"       },
52 {ROAR_HT_RIPEMD160,   "RIPEMD160"  },
53 {ROAR_HT_MD2,         "MD2"        },
54 {ROAR_HT_TIGER,       "TIGER"      },
55 {ROAR_HT_HAVAL,       "HAVAL"      },
56 {ROAR_HT_SHA256,      "SHA256"     },
57 {ROAR_HT_SHA384,      "SHA384"     },
58 {ROAR_HT_SHA512,      "SHA512"     },
59 {ROAR_HT_SHA224,      "SHA224"     },
60 {ROAR_HT_MD4,         "MD4"        },
61 {ROAR_HT_CRC32,       "CRC32"      },
62 {ROAR_HT_RFC1510,     "RFC1510"    },
63 {ROAR_HT_RFC2440,     "RFC2440"    },
64 {ROAR_HT_WHIRLPOOL,   "WHIRLPOOL"  },
65 {ROAR_HT_UUID,        "UUID"       },
66 {ROAR_HT_GTN8,        "GTN8"       },
67 {ROAR_HT_GTN16,       "GTN16"      },
68 {ROAR_HT_GTN32,       "GTN32"      },
69 {ROAR_HT_GTN64,       "GTN64"      },
70 {ROAR_HT_CLIENTID,    "CLIENTID"   },
71 {ROAR_HT_STREAMID,    "STREAMID"   },
72 {ROAR_HT_SOURCEID,    "SOURCEID"   },
73 {ROAR_HT_SAMPLEID,    "SAMPLEID"   },
74 {ROAR_HT_MIXERID,     "MIXERID"    },
75 {ROAR_HT_BRIDGEID,    "BRIDGEID"   },
76 {ROAR_HT_LISTENID,    "LISTENID"   },
77 {ROAR_HT_ACTIONID,    "ACTIONID"   },
78 {ROAR_HT_MSGQUEUEID,  "MSGQUEUEID" },
79 {ROAR_HT_MSGBUSID,    "MSGBUSID"   },
80 {ROAR_HT_GTIN8,       "GTIN8"      },
81 {ROAR_HT_GTIN13,      "GTIN13"     },
82 {ROAR_HT_ISBN10,      "ISBN10"     },
83 {ROAR_HT_ISBN13,      "ISBN13"     },
84 {-1, NULL}
85};
86
87static inline int roar_ht2gcrypt_tested (const int ht) {
88 const char * name;
89
90 if ( ht > 512 )
91  return -1;
92
93 // test the algo:
94 name = gcry_md_algo_name(ht);
95
96 if ( name == NULL )
97  return -1;
98
99 if ( *name == 0 )
100  return -1;
101
102 return ht;
103}
104
105const char * roar_ht2str (const int    ht) {
106 int i;
107
108 for (i = 0; _libroar_hashes[i].id != -1; i++)
109  if ( _libroar_hashes[i].id == ht )
110   return _libroar_hashes[i].name;
111
112 return NULL;
113}
114
115int          roar_str2ht (const char * ht) {
116 int i;
117
118 for (i = 0; _libroar_hashes[i].id != -1; i++)
119  if ( !strcasecmp(_libroar_hashes[i].name, ht) )
120   return _libroar_hashes[i].id;
121
122 return -1;
123}
124
125int roar_hash_buffer(void * digest, const void * data, size_t datalen, int algo) {
126 return roar_hash_salted_buffer(digest, data, datalen, algo, NULL, 0);
127}
128
129#ifdef ROAR_HAVE_LIBGCRYPT
130static inline int roar_hash_salted_buffer_gcrypt(void * digest, const void * data, size_t datalen, int algo, const void * salt, size_t saltlen) {
131 gcry_md_hd_t hdl;
132
133 algo = roar_ht2gcrypt_tested(algo);
134 if ( algo == -1 )
135  return -1;
136
137
138 if ( salt == NULL ) {
139  // optimized for unsalted:
140  gcry_md_hash_buffer(algo, digest, data, datalen);
141  return 0;
142 } else {
143  if ( gcry_md_open(&hdl, algo, 0) != 0 )
144   return -1;
145
146  gcry_md_write(hdl, data, datalen);
147  gcry_md_write(hdl, salt, saltlen);
148
149  memcpy(digest, gcry_md_read(hdl, algo), gcry_md_get_algo_dlen(algo));
150
151  gcry_md_close(hdl);
152 }
153
154 return 0;
155}
156#endif
157
158int roar_hash_salted_buffer(void * digest, const void * data, size_t datalen, int algo, const void * salt, size_t saltlen) {
159 if ( digest == NULL || data == NULL )
160  return -1;
161
162#ifdef ROAR_HAVE_LIBGCRYPT
163 return roar_hash_salted_buffer_gcrypt(digest, data, datalen, algo, salt, saltlen);
164#else
165 return -1;
166#endif
167}
168
169//ll
Note: See TracBrowser for help on using the repository browser.