shatest.cpp
Go to the documentation of this file.
1 
21 #include <stdint.h>
22 #include <string.h>
23 #include <iostream>
24 #include <iomanip>
25 #include <string>
26 #include <vector>
27 
28 #include "cryptotools.hpp"
29 
30 using namespace cryptoTools;
31 
32 
46 bool parseInput(std::vector<unsigned char> &in)
47 {
48  in.clear();
49  std::string str;
50  std::getline(std::cin,str,'\n');
51  if (str.size()>1)
52  for (unsigned int i=0; i<str.size(); i+=2)
53  if ( (i+1) < str.size() )
54  {
55  unsigned char hexaByte = 0;
56  switch(str[i])
57  {
58  case '0': hexaByte += 0 *16; break;
59  case '1': hexaByte += 1 *16; break;
60  case '2': hexaByte += 2 *16; break;
61  case '3': hexaByte += 3 *16; break;
62  case '4': hexaByte += 4 *16; break;
63  case '5': hexaByte += 5 *16; break;
64  case '6': hexaByte += 6 *16; break;
65  case '7': hexaByte += 7 *16; break;
66  case '8': hexaByte += 8 *16; break;
67  case '9': hexaByte += 9 *16; break;
68  case 'a': hexaByte += 10*16; break;
69  case 'b': hexaByte += 11*16; break;
70  case 'c': hexaByte += 12*16; break;
71  case 'd': hexaByte += 13*16; break;
72  case 'e': hexaByte += 14*16; break;
73  case 'f': hexaByte += 15*16; break;
74  }
75  switch (str[i+1])
76  {
77  case '0': hexaByte += 0 ; break;
78  case '1': hexaByte += 1 ; break;
79  case '2': hexaByte += 2 ; break;
80  case '3': hexaByte += 3 ; break;
81  case '4': hexaByte += 4 ; break;
82  case '5': hexaByte += 5 ; break;
83  case '6': hexaByte += 6 ; break;
84  case '7': hexaByte += 7 ; break;
85  case '8': hexaByte += 8 ; break;
86  case '9': hexaByte += 9 ; break;
87  case 'a': hexaByte += 10; break;
88  case 'b': hexaByte += 11; break;
89  case 'c': hexaByte += 12; break;
90  case 'd': hexaByte += 13; break;
91  case 'e': hexaByte += 14; break;
92  case 'f': hexaByte += 15; break;
93  }
94  in.push_back(hexaByte);
95  }
96  return (std::cin.eof());
97 }
98 
99 
100 int main(int argc, char ** argv)
101 {
102  if (argc == 1)
103  {
104  std::cout<<"Specify \"SHA-256\", \"SHA-384\" or"
105  <<"\"SHA-512\" in the CLI arguments."
106  <<std::endl;
107  return 1;
108  }
109  else
110  {
111  SHAx * s;
112  if (strcmp(argv[1],"SHA-256")==0)
113  s = new SHA256();
114  else if (strcmp(argv[1],"SHA-384")==0)
115  s = new SHA384();
116  else
117  s = new SHA512();
118  bool finished = false;
119  std::vector<unsigned char> message;
120  std::vector<uint8_t> digest;
121  std::cout<<"hash function: "<<s->getType();
122  std::cout<<", "<<s->getHashLength()<<" bits long"<<std::endl;
123  while (!finished)
124  {
125  finished = parseInput(message);
126  if (!finished)
127  {
128  s->hash(message);
129  digest = s->getHash();
130  for (unsigned int i=0; i<digest.size(); i++)
131  std::cout<<std::hex<<std::setfill('0')<<std::setw(2)<<(int)digest[i];
132  std::cout<<std::endl;
133  }
134  }
135  return 0;
136  }
137 }
138