31 template <
unsigned int N,
typename U,
typename S>
class HashState ;
40 template <
typename U>
static std::string extension( U n ) ;
45 template <
typename U>
static void convert_( U n , std::string::iterator p ) ;
49 HashStateImp() = delete ;
61template <
unsigned int N,
typename U,
typename S>
67 static_assert( N != 0 && (N%4) == 0 ,
"hash state size must be a multiple of four" ) ;
69 static std::string
encode(
const uint_type * ) ;
73 static std::string
encode(
const uint_type * , size_type n ) ;
78 static std::string
encode( uint_type hi , uint_type low ,
const uint_type * ) ;
81 static std::string
encode( uint_type hi , uint_type low , uint_type v0 , uint_type v1 ,
82 uint_type v2 , uint_type v3 , uint_type v4 = 0 ) ;
86 static void decode(
const std::string & s , uint_type * values_out , size_type & size_out ) ;
92 static void decode(
const std::string & , uint_type & size_hi_out , uint_type & size_low_out ,
93 uint_type * value_0 , uint_type * value_1 , uint_type * value_2 , uint_type * value_3 ,
94 uint_type * value_4 =
nullptr ) ;
98 static void decode(
const std::string & , uint_type & size_hi_out , uint_type & size_low_out ,
99 uint_type * values_out ) ;
106 static void convert(
char hi ,
char himid ,
char lomid ,
char lo , uint_type & n ) ;
107 static void convert(
const std::string & str , uint_type & n ) ;
108 static void convert(
const std::string & s , uint_type * state ) ;
109 static void convert(
const std::string & s , uint_type * state , size_type & ) ;
112template <
unsigned int N,
typename U,
typename S>
115 std::string result( N ,
'\0' ) ;
116 for( std::size_t i = 0U ; i < N/4 ; i++ )
118 convert_( values[i] , result.begin() + (i*4U) ) ;
123template <
unsigned int N,
typename U,
typename S>
126 std::string result( N+4U ,
'\0' ) ;
127 for( std::size_t i = 0U ; i < N/4 ; i++ )
129 convert_( values[i] , result.begin() + (i*4U) ) ;
131 convert_( n , result.begin() + N ) ;
135template <
unsigned int N,
typename U,
typename S>
141 return encode( values , n ) ;
144template <
unsigned int N,
typename U,
typename S>
146 uint_type v1 , uint_type v2 , uint_type v3 , uint_type v4 )
148 unsigned int NN = N ;
152 std::array<uint_type,N/4> values {} ;
153 if( NN > 0 ) values[0] = v0 ;
154 if( NN > 4 ) values[1] = v1 ;
155 if( NN > 8 ) values[2] = v2 ;
156 if( NN > 12 ) values[3] = v3 ;
157 if( NN > 16 ) values[4] = v4 ;
158 return encode( values.data() , n ) ;
162std::string G::HashStateImp::extension( U n )
164 std::string result( 4U ,
'\0' ) ;
165 convert_( n , result.begin() ) ;
169template <
unsigned int N,
typename U,
typename S>
172 if( str.length() < (N+4U) )
173 return decode( str+std::string(N+4U,
'\0') , values_out , size_out ) ;
174 convert( str , values_out , size_out ) ;
177template <
unsigned int N,
typename U,
typename S>
179 uint_type * v0 , uint_type * v1 , uint_type * v2 , uint_type * v3 , uint_type * v4 )
181 if( str.length() < (N+4U) )
182 return decode( str+std::string(N+4U,
'\0') , hi , low , v0 , v1 , v2 , v3 , v4 ) ;
183 std::array<uint_type,N/4> values {} ;
185 convert( str , values.data() , n ) ;
186 if( v0 && N > 0 ) *v0 = values[0] ;
187 if( v1 && N > 4 ) *v1 = values[1] ;
188 if( v2 && N > 8 ) *v2 = values[2] ;
189 if( v3 && N > 12 ) *v3 = values[3] ;
190 if( v4 && N > 16 ) *v4 = values[4] ;
192 low = ( n << 3 ) & 0xffffffffUL ;
195template <
unsigned int N,
typename U,
typename S>
197 uint_type * values_out )
199 if( str.length() < (N+4U) )
200 return decode( str+std::string(N+4U,
'\0') , hi , low , values_out ) ;
202 convert( str , values_out , n ) ;
204 low = ( n << 3 ) & 0xffffffffUL ;
208void G::HashStateImp::convert_( U n , std::string::iterator p_out )
210 *p_out++ =
static_cast<char>(n&0xffU) ; n >>= 8U ;
211 *p_out++ =
static_cast<char>(n&0xffU) ; n >>= 8U ;
212 *p_out++ =
static_cast<char>(n&0xffU) ; n >>= 8U ;
213 *p_out++ =
static_cast<char>(n&0xffU) ;
216template <
unsigned int N,
typename U,
typename S>
220 n_out |=
static_cast<unsigned char>(hi) ; n_out <<= 8 ;
221 n_out |=
static_cast<unsigned char>(himid) ; n_out <<= 8 ;
222 n_out |=
static_cast<unsigned char>(lomid) ; n_out <<= 8 ;
223 n_out |=
static_cast<unsigned char>(lo) ;
226template <
unsigned int N,
typename U,
typename S>
229 convert( str.at(3) , str.at(2) , str.at(1) , str.at(0) , n_out ) ;
232template <
unsigned int N,
typename U,
typename S>
235 for( std::size_t i = 0U ; i < (N/4U) ; i++ )
237 convert( str.at(i*4U+3U) , str.at(i*4U+2U) , str.at(i*4U+1U) , str.at(i*4U+0U) , state_out[i] ) ;
241template <
unsigned int N,
typename U,
typename S>
244 convert( str , state_out ) ;
246 convert( str.at(N+3U) , str.at(N+2U) , str.at(N+1U) , str.at(N) , nn ) ;
247 n_out =
static_cast<size_type
>(nn) ;
Functions for representing the intermediate state of a hash function as a non-printable string.
static std::string encode(const uint_type *)
Returns the hash state as an N-character string of non-printing characters.
static void decode(const std::string &s, uint_type *values_out, size_type &size_out)
Converts an encode()d string back into a hash state of N/4 integers and a data size returned by refer...