E-MailRelay
ghash.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // ghash.cpp
19 //
20 
21 #include "gdef.h"
22 #include "ghash.h"
23 #include "gassert.h"
24 #include <sstream>
25 
26 std::string G::Hash::xor_( const std::string & s1 , const std::string & s2 )
27 {
28  G_ASSERT( s1.length() == s2.length() ) ;
29  std::string::const_iterator p1 = s1.begin() ;
30  std::string::const_iterator p2 = s2.begin() ;
31  std::string result ;
32  result.reserve( s1.length() ) ;
33  for( ; p1 != s1.end() ; ++p1 , ++p2 )
34  {
35  unsigned char c1 = static_cast<unsigned char>(*p1) ;
36  unsigned char c2 = static_cast<unsigned char>(*p2) ;
37  unsigned char c = static_cast<unsigned char>( c1 ^ c2 ) ;
38  result.append( 1U , static_cast<char>(c) ) ;
39  }
40  return result ;
41 }
42 
43 std::string G::Hash::ipad( size_t blocksize )
44 {
45  return std::string( blocksize , '\066' ) ;
46 }
47 
48 std::string G::Hash::opad( size_t blocksize )
49 {
50  return std::string( blocksize , '\134' ) ;
51 }
52 
53 std::string G::Hash::printable( const std::string & input )
54 {
55  std::string result ;
56  result.reserve( input.length() * 2U ) ;
57  const char * hex = "0123456789abcdef" ;
58  const size_t n = input.length() ;
59  for( size_t i = 0U ; i < n ; i++ )
60  {
61  unsigned char c = static_cast<unsigned char>(input.at(i)) ;
62  result.append( 1U , hex[(c>>4U)&0x0F] ) ;
63  result.append( 1U , hex[(c>>0U)&0x0F] ) ;
64  }
65  G_ASSERT( result.size() == (input.size()*2U) ) ;
66  return result ;
67 }
68 
69 /// \file ghash.cpp
static std::string printable(const std::string &input)
Converts a binary string into a printable form, using a lowercase hexadecimal encoding.
Definition: ghash.cpp:53