- メインページ
- モジュール
- ネームスペース
- データ構造
- ファイル

- ファイル一覧
- グローバル

hash_algorithm.h
説明を見る。
1 //
2 // Copyright (c) 2003-2011, MIST Project, Nagoya University
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the name of the Nagoya University nor the names of its contributors
16 // may be used to endorse or promote products derived from this software
17 // without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28
40 #ifndef __INCLUDE_HASH_ALGORITHM__
41 #define __INCLUDE_HASH_ALGORITHM__
42
43 #ifndef __INCLUDE_MIST_CONF_H__
44 #include "config/mist_conf.h"
45 #endif
46
47 #include <string>
48
49
50 // mist名前空間の始まり
51 _MIST_BEGIN
52
53
61
62
84 class hash_algorithm
85 {
86 public:
87 typedef size_t size_type;
88 typedef ptrdiff_t difference_type;
89 typedef unsigned char uint8;
90 typedef unsigned int uint32;
91 typedef unsigned long long int uint64;
92
93 protected:
94 size_type nbytes;
95 unsigned char *digest;
96
97 public:
99 virtual void compute_hash( const void *bytes, uint64 len ) = 0;
100
102 void compute_hash( const std::string &str ){ compute_hash( str.c_str( ), str.length( ) ); }
103
104
106 virtual const std::string name( ) const = 0;
107
109 size_type size( ) const { return( nbytes ); }
110
112 unsigned char operator []( size_type index ) const { return( digest[ index ] ); }
113
114
116 std::string to_string( ) const
117 {
118 static char x16[ 16 ] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
119 std::string str;
120
121 for( size_type i = 0 ; i < size( ) ; i++ )
122 {
123 str += x16[ ( digest[ i ] >> 4 ) & 0x0f ];
124 str += x16[ digest[ i ] & 0x0f ];
125 }
126
127 return( str );
128 }
129
130 protected:
131 // ハッシュ関数のコピーは禁止！！
132 hash_algorithm &operator =( const hash_algorithm &h );
133 hash_algorithm( const hash_algorithm &h );
134
135 protected:
137 hash_algorithm( size_type num_digest_bytes ) : nbytes( num_digest_bytes ), digest( new unsigned char[ nbytes ] ){ }
138
140 hash_algorithm( const std::string &str ) : nbytes( str.length( ) / 2 ), digest( new unsigned char[ nbytes ] )
141 {
142 static uint8 x16[ 256 ] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
143 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
144 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
145 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
146 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
147 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
148 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
149 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
150
151 for( size_type i = 0, indx = 0 ; i < size( ) ; i++, indx += 2 )
152 {
153 size_type s1 = static_cast< uint8 >( str[ indx ] );
154 size_type s2 = static_cast< uint8 >( str[ indx + 1 ] );
155 digest[ i ] = ( ( x16[ s1 ] << 4 ) & 0xf0 ) | ( x16[ s2 ] & 0x0f );
156 }
157 }
158
160 virtual ~hash_algorithm( )
161 {
162 delete [] digest;
163 }
164
165
166 public:
168 bool operator ==( const hash_algorithm &h ) const
169 {
170 if( size( ) != h.size( ) )
171 {
172 return( false );
173 }
174 else
175 {
176 for( size_type i = 0 ; i < size( ) ; i++ )
177 {
178 if( digest[ i ] != h[ i ] )
179 {
180 return( false );
181 }
182 }
183 return( true );
184 }
185 }
186
188 bool operator ==( const std::string &str ) const
189 {
190 return( str == to_string( ) );
191 }
192 };
193
194
202 inline std::ostream &operator <<( std::ostream &out, const hash_algorithm &h )
203 {
204 out << h.to_string( );
205 return( out );
206 }
207
208
210 // ハッシュ関数グループの終わり
211
212
213 // mist名前空間の終わり
214 _MIST_END
215
216
217 #ifndef __INCLUDE_MD5__
218 #include "md5.h"
219 #endif
220
221 #ifndef __INCLUDE_SHA__
222 #include "sha.h"
223 #endif
224
225
226 #endif // __INCLUDE_HASH_ALGORITHM__
227