1
2 /* $Id$ */
3 /*
4 ** Copyright (C) 2014-2021 Cisco and/or its affiliates. All rights reserved.
5 ** Copyright (C) 2013 Sourcefire, Inc.
6 **
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License Version 2 as
9 ** published by the Free Software Foundation. You may not use, modify or
10 ** distribute this program under any other version of the GNU General
11 ** Public License.
12 **
13 ** This program is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with this program; if not, write to the Free Software
20 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23 /***************************************************************************
24 *
25 * File: sf_sechash.C
26 *
27 * Purpose: Provide a set of secure hash utilities
28 *
29 * History:
30 *
31 * Date: Author: Notes:
32 * ---------- ------- ----------------------------------------------
33 * 12/05/13 ECB Initial coding begun
34 *
35 **************************************************************************/
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <sys/types.h>
45
46 #include "sf_types.h"
47 #include "sf_sechash.h"
48 #include "snort_debug.h"
49
50
51 static struct
52 {
53 Secure_Hash_Type type;
54 char *name;
55 unsigned int length;
56 } Secure_Hash_Map[] =
57 {
58 { SECHASH_SHA512, "SHA512", 64 },
59 { SECHASH_SHA256, "SHA256", 32 },
60 { SECHASH_MD5, "MD5", 16 },
61 { SECHASH_NONE, "", 0 }
62 };
63
SecHash_Type2Length(const Secure_Hash_Type type)64 unsigned int SecHash_Type2Length( const Secure_Hash_Type type )
65 {
66 unsigned int index;
67
68 index = 0;
69
70 while(Secure_Hash_Map[index].type != SECHASH_NONE)
71 {
72 if( Secure_Hash_Map[index].type == type )
73 {
74 return( Secure_Hash_Map[index].length );
75 }
76 index += 1;
77 }
78
79 return( 0 );
80 }
81
SecHash_Name2Type(const char * name)82 Secure_Hash_Type SecHash_Name2Type( const char *name )
83 {
84 unsigned int index;
85
86 index = 0;
87
88 while(Secure_Hash_Map[index].type != SECHASH_NONE)
89 {
90 if( strcasecmp(name,Secure_Hash_Map[index].name) == 0 )
91 {
92 return( Secure_Hash_Map[index].type );
93 }
94 index += 1;
95 }
96
97 return( SECHASH_NONE );
98 }
99