1#include "../picosha2.h"
6#define PICOSHA2_CHECK_EQUAL(a, b){\
8 std::cout << "\033[32m" << __FUNCTION__ << "(LINE:" << __LINE__ << ") is succeeded." << "\033[39m" << std::endl;\
11 std::cout << "\033[31m" << __FUNCTION__ << "(LINE:" << __LINE__ << ") is failed.\n\t" << #a << " != " << #b \
12 << "\033[39m" << std::endl;\
15#define PICOSHA2_CHECK_EQUAL_BYTES(a, b){\
16 if(is_same_bytes(a, b)){\
17 std::cout << "\033[32m" << __FUNCTION__ << "(LINE:" << __LINE__ << ") is succeeded." << "\033[39m" << std::endl;\
20 std::cout << "\033[31m" << __FUNCTION__ << "(LINE:" << __LINE__ << ") is failed.\n\t" << #a << " != " << #b \
21 << "\033[39m" << std::endl;\
25template<
typename InIter1,
typename InIter2>
26bool is_same_bytes(InIter1 first1, InIter1 last1, InIter2 first2, InIter2 last2){
27 if(std::distance(first1, last1) != std::distance(first2, last2)){
30 return std::search(first1, last1, first2, last2) != last1;
33template<
typename InContainer1,
typename InContainer2>
34bool is_same_bytes(
const InContainer1& bytes1,
const InContainer2& bytes2){
35 return is_same_bytes(bytes1.begin(), bytes1.end(), bytes2.begin(), bytes2.end());
38template<
typename OutIter>
39void input_hex(std::istream& is, OutIter first, OutIter last){
41 std::vector<char> buffer;
43 if(buffer.size() == 2){
44 *(first++) = (buffer.front()*16+buffer.back());
48 if(
'0' <= c && c <=
'9'){
49 buffer.push_back(c-
'0');
51 if(
'a' <= c && c <=
'f'){
52 buffer.push_back(c-
'a'+10);
57template<
typename OutIter>
58void hex_string_to_bytes(
const std::string& hex_str, OutIter first, OutIter last){
59 assert(hex_str.size() >= 2*std::distance(first, last));
60 std::istringstream iss(hex_str);
61 input_hex(iss, first, last);
64template<
typename OutContainer>
65void hex_string_to_bytes(
const std::string& hex_str, OutContainer& bytes){
66 hex_string_to_bytes(hex_str, bytes.begin(), bytes.end());
69typedef std::pair<std::string, std::string> mess_and_hash;
70const size_t sample_size = 10;
71const std::pair<std::string, std::string> sample_message_list[sample_size] = {
73 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"),
74 mess_and_hash(
"The quick brown fox jumps over the lazy dog",
75 "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"),
76 mess_and_hash(
"The quick brown fox jumps over the lazy dog.",
77 "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c"),
78 mess_and_hash(
"For this sample, this 63-byte string will be used as input data",
79 "f08a78cbbaee082b052ae0708f32fa1e50c5c421aa772ba5dbb406a2ea6be342"),
80 mess_and_hash(
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
81 "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"),
82 mess_and_hash(
"This is exactly 64 bytes long, not counting the terminating byte",
83 "ab64eff7e88e2e46165e29f2bce41826bd4c7b3552f6b382a9e7d3af47c245f8"),
84 mess_and_hash(
"This is exactly 64 bytes long, not counting the terminati",
85 "46db250ef6d667908de17333c25343778f495d7a8010b9cfa2af97940772e8cd"),
86 mess_and_hash(
"This is exactly 64 bytes long, not counting the terminatin",
87 "af38fc14dbbbcc6cd4c9cc73988e1b08373b4e6b04ba61b41f999731185b51af"),
88 mess_and_hash(
"This is exactly 64 bytes long, not counting the terminating",
89 "f778b361f650cdd9981ca13adb77f26b8419a407b3938fc54b14e9971045fa9d"),
90 mess_and_hash(
"This is exactly 64 bytes long, not counting the terminating b",
91 "9aa72d139c7d7e5a35ea525e2ba6704163555ba647927765a61ccbf12ec60479")
95 for(std::size_t i = 0; i < sample_size; ++i){
96 std::string src_str = sample_message_list[i].first;
97 std::cout <<
"src_str: " << src_str <<
" size: " << src_str.size() << std::endl;
98 std::string ans_hex_str = sample_message_list[i].second;
99 std::vector<unsigned char> ans(32);
100 hex_string_to_bytes(ans_hex_str, ans);
102 std::vector<unsigned char> hash(32);
103 picosha2::hash256(src_str.begin(), src_str.end(), hash.begin(), hash.end());
104 PICOSHA2_CHECK_EQUAL_BYTES(ans, hash);
107 std::vector<unsigned char> hash(32);
108 picosha2::hash256(src_str, hash);
109 PICOSHA2_CHECK_EQUAL_BYTES(ans, hash);
112 std::vector<unsigned char> hash(32);
113 picosha2::hash256(src_str.begin(), src_str.end(), hash);
114 PICOSHA2_CHECK_EQUAL_BYTES(ans, hash);
117 unsigned char hash_c_array[32];
118 picosha2::hash256(src_str.begin(), src_str.end(), hash_c_array, hash_c_array+32);
119 std::vector<unsigned char> hash(hash_c_array, hash_c_array+32);
120 PICOSHA2_CHECK_EQUAL_BYTES(ans, hash);
123 std::list<unsigned char> hash(32);
124 picosha2::hash256(src_str.begin(), src_str.end(), hash.begin(), hash.end());
125 PICOSHA2_CHECK_EQUAL_BYTES(ans, hash);
128 std::list<unsigned char> hash(32);
129 picosha2::hash256(src_str, hash);
130 PICOSHA2_CHECK_EQUAL_BYTES(ans, hash);
133 std::string hash_hex_str;
134 picosha2::hash256_hex_string(src_str.begin(), src_str.end(), hash_hex_str);
135 PICOSHA2_CHECK_EQUAL(ans_hex_str, hash_hex_str);
138 std::string hash_hex_str;
139 picosha2::hash256_hex_string(src_str, hash_hex_str);
140 PICOSHA2_CHECK_EQUAL(ans_hex_str, hash_hex_str);
143 std::string hash_hex_str =
144 picosha2::hash256_hex_string(src_str.begin(), src_str.end());
145 PICOSHA2_CHECK_EQUAL(ans_hex_str, hash_hex_str);
148 std::string hash_hex_str = picosha2::hash256_hex_string(src_str);
149 PICOSHA2_CHECK_EQUAL(ans_hex_str, hash_hex_str);
152 std::vector<unsigned char> src_vect(src_str.begin(), src_str.end());
154 std::vector<unsigned char> hash(32);
155 picosha2::hash256(src_vect.begin(), src_vect.end(), hash.begin(), hash.end());
156 PICOSHA2_CHECK_EQUAL_BYTES(ans, hash);
159 std::vector<unsigned char> hash(32);
160 picosha2::hash256(src_vect, hash);
161 PICOSHA2_CHECK_EQUAL_BYTES(ans, hash);
164 std::list<unsigned char> hash(32);
165 picosha2::hash256(src_vect.begin(), src_vect.end(), hash.begin(), hash.end());
166 PICOSHA2_CHECK_EQUAL_BYTES(ans, hash);
169 std::list<unsigned char> hash(32);
170 picosha2::hash256(src_vect.data(), src_vect.data()+src_vect.size(),
171 hash.begin(), hash.end());
172 PICOSHA2_CHECK_EQUAL_BYTES(ans, hash);
175 std::list<unsigned char> hash(32);
176 picosha2::hash256(src_vect, hash);
177 PICOSHA2_CHECK_EQUAL_BYTES(ans, hash);
180 std::string hash_hex_str;
181 picosha2::hash256_hex_string(src_vect.begin(), src_vect.end(), hash_hex_str);
182 PICOSHA2_CHECK_EQUAL(ans_hex_str, hash_hex_str);
185 std::string hash_hex_str;
186 picosha2::hash256_hex_string(src_vect, hash_hex_str);
187 PICOSHA2_CHECK_EQUAL(ans_hex_str, hash_hex_str);
190 std::string hash_hex_str =
191 picosha2::hash256_hex_string(src_vect.begin(), src_vect.end());
192 PICOSHA2_CHECK_EQUAL(ans_hex_str, hash_hex_str);
195 std::string hash_hex_str = picosha2::hash256_hex_string(src_vect);
196 PICOSHA2_CHECK_EQUAL(ans_hex_str, hash_hex_str);
199 std::list<char> src(src_str.begin(), src_str.end());
200 std::vector<unsigned char> hash(32);
201 picosha2::hash256(src.begin(), src.end(), hash.begin(), hash.end());
202 PICOSHA2_CHECK_EQUAL_BYTES(ans, hash);
207 std::ifstream ifs(
"test.cpp");
208 std::string file_str((std::istreambuf_iterator<char>(ifs)),
209 std::istreambuf_iterator<char>());
211 std::size_t block_size = file_str.size()/10;
212 for(i = 0; i+block_size <= file_str.size(); i+=block_size){
213 hasher.process(file_str.begin()+i, file_str.begin()+i+block_size);
215 hasher.process(file_str.begin()+i, file_str.end());
217 std::string one_by_one_hex_string;
218 get_hash_hex_string(hasher, one_by_one_hex_string);
220 std::string hex_string;
221 picosha2::hash256_hex_string(file_str.begin(), file_str.end(), hex_string);
222 PICOSHA2_CHECK_EQUAL(one_by_one_hex_string, hex_string);
226 std::string one_by_one_hex_string; {
228 std::ifstream ifs(
"test.cpp");
229 std::string file_str((std::istreambuf_iterator<char>(ifs)),
230 std::istreambuf_iterator<char>());
232 std::size_t block_size = file_str.size()/10;
233 for(i = 0; i+block_size <= file_str.size(); i+=block_size){
234 hasher.process(file_str.begin()+i, file_str.begin()+i+block_size);
236 hasher.process(file_str.begin()+i, file_str.end());
238 get_hash_hex_string(hasher, one_by_one_hex_string);
241 std::ifstream ifs(
"test.cpp");
242 auto first = std::istreambuf_iterator<char>(ifs);
243 auto last = std::istreambuf_iterator<char>();
244 auto hex_string = picosha2::hash256_hex_string(first, last);
245 PICOSHA2_CHECK_EQUAL(one_by_one_hex_string, hex_string);
249int main(
int argc,
char* argv[])