picosha2.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. The MIT License (MIT)
  3. Copyright (C) 2014 okdshin
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #ifndef PICOSHA2_H
  21. #define PICOSHA2_H
  22. //picosha2:20140213
  23. #include <cstdint>
  24. #include <iostream>
  25. #include <vector>
  26. #include <iterator>
  27. #include <cassert>
  28. #include <sstream>
  29. #include <algorithm>
  30. namespace picosha2
  31. {
  32. namespace detail
  33. {
  34. inline uint8_t mask_8bit(uint8_t x){
  35. return x&0xff;
  36. }
  37. inline uint32_t mask_32bit(uint32_t x){
  38. return x&0xffffffff;
  39. }
  40. static const uint32_t add_constant[64] = {
  41. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  42. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  43. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  44. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  45. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  46. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  47. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  48. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  49. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  50. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  51. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  52. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  53. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  54. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  55. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  56. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  57. };
  58. static const uint32_t initial_message_digest[8] = {
  59. 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  60. 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
  61. };
  62. inline uint32_t ch(uint32_t x, uint32_t y, uint32_t z){
  63. return (x&y)^((~x)&z);
  64. }
  65. inline uint32_t maj(uint32_t x, uint32_t y, uint32_t z){
  66. return (x&y)^(x&z)^(y&z);
  67. }
  68. inline uint32_t rotr(uint32_t x, std::size_t n){
  69. assert(n < 32);
  70. return mask_32bit((x>>n)|(x<<(32-n)));
  71. }
  72. inline uint32_t bsig0(uint32_t x){
  73. return rotr(x, 2)^rotr(x, 13)^rotr(x, 22);
  74. }
  75. inline uint32_t bsig1(uint32_t x){
  76. return rotr(x, 6)^rotr(x, 11)^rotr(x, 25);
  77. }
  78. inline uint32_t shr(uint32_t x, std::size_t n){
  79. assert(n < 32);
  80. return x >> n;
  81. }
  82. inline uint32_t ssig0(uint32_t x){
  83. return rotr(x, 7)^rotr(x, 18)^shr(x, 3);
  84. }
  85. inline uint32_t ssig1(uint32_t x){
  86. return rotr(x, 17)^rotr(x, 19)^shr(x, 10);
  87. }
  88. template<typename RaIter1, typename RaIter2>
  89. void hash256_block(RaIter1 message_digest, RaIter2 first, RaIter2 last){
  90. (void)last; // FIXME: check this is valid
  91. uint32_t w[64];
  92. std::fill(w, w+64, 0);
  93. for(std::size_t i = 0; i < 16; ++i){
  94. w[i] = (static_cast<uint32_t>(mask_8bit(*(first+i*4)))<<24)
  95. |(static_cast<uint32_t>(mask_8bit(*(first+i*4+1)))<<16)
  96. |(static_cast<uint32_t>(mask_8bit(*(first+i*4+2)))<<8)
  97. |(static_cast<uint32_t>(mask_8bit(*(first+i*4+3))));
  98. }
  99. for(std::size_t i = 16; i < 64; ++i){
  100. w[i] = mask_32bit(ssig1(w[i-2])+w[i-7]+ssig0(w[i-15])+w[i-16]);
  101. }
  102. uint32_t a = *message_digest;
  103. uint32_t b = *(message_digest+1);
  104. uint32_t c = *(message_digest+2);
  105. uint32_t d = *(message_digest+3);
  106. uint32_t e = *(message_digest+4);
  107. uint32_t f = *(message_digest+5);
  108. uint32_t g = *(message_digest+6);
  109. uint32_t h = *(message_digest+7);
  110. for(std::size_t i = 0; i < 64; ++i){
  111. uint32_t temp1 = h+bsig1(e)+ch(e,f,g)+add_constant[i]+w[i];
  112. uint32_t temp2 = bsig0(a)+maj(a,b,c);
  113. h = g;
  114. g = f;
  115. f = e;
  116. e = mask_32bit(d+temp1);
  117. d = c;
  118. c = b;
  119. b = a;
  120. a = mask_32bit(temp1+temp2);
  121. }
  122. *message_digest += a;
  123. *(message_digest+1) += b;
  124. *(message_digest+2) += c;
  125. *(message_digest+3) += d;
  126. *(message_digest+4) += e;
  127. *(message_digest+5) += f;
  128. *(message_digest+6) += g;
  129. *(message_digest+7) += h;
  130. for(std::size_t i = 0; i < 8; ++i){
  131. *(message_digest+i) = mask_32bit(*(message_digest+i));
  132. }
  133. }
  134. }//namespace detail
  135. template<typename InIter>
  136. void output_hex(InIter first, InIter last, std::ostream& os){
  137. os.setf(std::ios::hex, std::ios::basefield);
  138. while(first != last){
  139. os.width(2);
  140. os.fill('0');
  141. os << static_cast<unsigned int>(*first);
  142. ++first;
  143. }
  144. os.setf(std::ios::dec, std::ios::basefield);
  145. }
  146. template<typename InIter>
  147. void bytes_to_hex_string(InIter first, InIter last, std::string& hex_str){
  148. std::ostringstream oss;
  149. output_hex(first, last, oss);
  150. hex_str.assign(oss.str());
  151. }
  152. template<typename InContainer>
  153. void bytes_to_hex_string(const InContainer& bytes, std::string& hex_str){
  154. bytes_to_hex_string(bytes.begin(), bytes.end(), hex_str);
  155. }
  156. template<typename InIter>
  157. std::string bytes_to_hex_string(InIter first, InIter last){
  158. std::string hex_str;
  159. bytes_to_hex_string(first, last, hex_str);
  160. return hex_str;
  161. }
  162. template<typename InContainer>
  163. std::string bytes_to_hex_string(const InContainer& bytes){
  164. std::string hex_str;
  165. bytes_to_hex_string(bytes, hex_str);
  166. return hex_str;
  167. }
  168. class hash256_one_by_one {
  169. public:
  170. hash256_one_by_one(){
  171. init();
  172. }
  173. void init(){
  174. buffer_.clear();
  175. std::fill(data_length_digits_, data_length_digits_+4, 0);
  176. std::copy(detail::initial_message_digest, detail::initial_message_digest+8, h_);
  177. }
  178. template<typename RaIter>
  179. void process(RaIter first, RaIter last){
  180. add_to_data_length(std::distance(first, last));
  181. std::copy(first, last, std::back_inserter(buffer_));
  182. std::size_t i = 0;
  183. for(;i+64 <= buffer_.size(); i+=64){
  184. detail::hash256_block(h_, buffer_.begin()+i, buffer_.begin()+i+64);
  185. }
  186. buffer_.erase(buffer_.begin(), buffer_.begin()+i);
  187. }
  188. void finish(){
  189. uint8_t temp[64];
  190. std::fill(temp, temp+64, 0);
  191. std::size_t remains = buffer_.size();
  192. std::copy(buffer_.begin(), buffer_.end(), temp);
  193. temp[remains] = 0x80;
  194. if(remains > 55){
  195. std::fill(temp+remains+1, temp+64, 0);
  196. detail::hash256_block(h_, temp, temp+64);
  197. std::fill(temp, temp+64-4, 0);
  198. }
  199. else {
  200. std::fill(temp+remains+1, temp+64-4, 0);
  201. }
  202. write_data_bit_length(&(temp[56]));
  203. detail::hash256_block(h_, temp, temp+64);
  204. }
  205. template<typename OutIter>
  206. void get_hash_bytes(OutIter first, OutIter last)const{
  207. for(const uint32_t* iter = h_; iter != h_+8; ++iter){
  208. for(std::size_t i = 0; i < 4 && first != last; ++i){
  209. *(first++) = detail::mask_8bit(static_cast<uint8_t>((*iter >> (24-8*i))));
  210. }
  211. }
  212. }
  213. private:
  214. void add_to_data_length(uint32_t n) {
  215. uint32_t carry = 0;
  216. data_length_digits_[0] += n;
  217. for(std::size_t i = 0; i < 4; ++i) {
  218. data_length_digits_[i] += carry;
  219. if(data_length_digits_[i] >= 65536u) {
  220. data_length_digits_[i] -= 65536u;
  221. carry = 1;
  222. }
  223. else {
  224. break;
  225. }
  226. }
  227. }
  228. void write_data_bit_length(uint8_t* begin) {
  229. uint32_t data_bit_length_digits[4];
  230. std::copy(
  231. data_length_digits_, data_length_digits_+4,
  232. data_bit_length_digits
  233. );
  234. // convert byte length to bit length (multiply 8 or shift 3 times left)
  235. uint32_t carry = 0;
  236. for(std::size_t i = 0; i < 4; ++i) {
  237. uint32_t before_val = data_bit_length_digits[i];
  238. data_bit_length_digits[i] <<= 3;
  239. data_bit_length_digits[i] |= carry;
  240. data_bit_length_digits[i] &= 65535u;
  241. carry = (before_val >> (16-3)) & 65535u;
  242. }
  243. // write data_bit_length
  244. for(int i = 3; i >= 0; --i) {
  245. (*begin++) = static_cast<uint8_t>(data_bit_length_digits[i] >> 8);
  246. (*begin++) = static_cast<uint8_t>(data_bit_length_digits[i]);
  247. }
  248. }
  249. std::vector<uint8_t> buffer_;
  250. uint32_t data_length_digits_[4]; //as 64bit integer (16bit x 4 integer)
  251. uint32_t h_[8];
  252. };
  253. inline void get_hash_hex_string(const hash256_one_by_one& hasher, std::string& hex_str){
  254. uint8_t hash[32];
  255. hasher.get_hash_bytes(hash, hash+32);
  256. return bytes_to_hex_string(hash, hash+32, hex_str);
  257. }
  258. inline std::string get_hash_hex_string(const hash256_one_by_one& hasher){
  259. std::string hex_str;
  260. get_hash_hex_string(hasher, hex_str);
  261. return hex_str;
  262. }
  263. template<typename RaIter, typename OutIter>
  264. void hash256(RaIter first, RaIter last, OutIter first2, OutIter last2){
  265. hash256_one_by_one hasher;
  266. //hasher.init();
  267. hasher.process(first, last);
  268. hasher.finish();
  269. hasher.get_hash_bytes(first2, last2);
  270. }
  271. template<typename RaIter, typename OutContainer>
  272. void hash256(RaIter first, RaIter last, OutContainer& dst){
  273. hash256(first, last, dst.begin(), dst.end());
  274. }
  275. template<typename RaContainer, typename OutIter>
  276. void hash256(const RaContainer& src, OutIter first, OutIter last){
  277. hash256(src.begin(), src.end(), first, last);
  278. }
  279. template<typename RaContainer, typename OutContainer>
  280. void hash256(const RaContainer& src, OutContainer& dst){
  281. hash256(src.begin(), src.end(), dst.begin(), dst.end());
  282. }
  283. template<typename RaIter>
  284. void hash256_hex_string(RaIter first, RaIter last, std::string& hex_str){
  285. uint8_t hashed[32];
  286. hash256(first, last, hashed, hashed+32);
  287. std::ostringstream oss;
  288. output_hex(hashed, hashed+32, oss);
  289. hex_str.assign(oss.str());
  290. }
  291. template<typename RaIter>
  292. std::string hash256_hex_string(RaIter first, RaIter last){
  293. std::string hex_str;
  294. hash256_hex_string(first, last, hex_str);
  295. return hex_str;
  296. }
  297. inline void hash256_hex_string(const std::string& src, std::string& hex_str){
  298. hash256_hex_string(src.begin(), src.end(), hex_str);
  299. }
  300. template<typename RaContainer>
  301. void hash256_hex_string(const RaContainer& src, std::string& hex_str){
  302. hash256_hex_string(src.begin(), src.end(), hex_str);
  303. }
  304. template<typename RaContainer>
  305. std::string hash256_hex_string(const RaContainer& src){
  306. return hash256_hex_string(src.begin(), src.end());
  307. }
  308. }//namespace picosha2
  309. #endif //PICOSHA2_H