| 778 | } |
| 779 | |
| 780 | bool CSHA256::Load(const std::vector<unsigned char>& vch) { |
| 781 | if (vch.size() < 40) return false; |
| 782 | |
| 783 | uint64_t bits = ReadLE64(&vch[32]); |
| 784 | size_t buf_size = (bits >> 3) % 64; |
| 785 | |
| 786 | if ((bits & 0x07) != 0 || vch.size() != 40 + buf_size) return false; |
| 787 | |
| 788 | // We want to leave the internal state of the object unchanged if false is returned. |
| 789 | // So no member variables can be modified until now. |
| 790 | |
| 791 | s[0] = ReadBE32(&vch[ 0]); |
| 792 | s[1] = ReadBE32(&vch[ 4]); |
| 793 | s[2] = ReadBE32(&vch[ 8]); |
| 794 | s[3] = ReadBE32(&vch[12]); |
| 795 | s[4] = ReadBE32(&vch[16]); |
| 796 | s[5] = ReadBE32(&vch[20]); |
| 797 | s[6] = ReadBE32(&vch[24]); |
| 798 | s[7] = ReadBE32(&vch[28]); |
| 799 | |
| 800 | bytes = bits >> 3; |
| 801 | if (buf_size) memcpy(buf, &vch[40], buf_size); |
| 802 | |
| 803 | return true; |
| 804 | } |
| 805 | |
| 806 | bool CSHA256::SafeWrite(const unsigned char* data, size_t len) { |
| 807 | const uint64_t SHA256_MAX = 0x1FFFFFFFFFFFFFFF; // SHA256's maximum allowed message length in bytes. |