The mystery of the problem turns out to be the size of the data.
After experimenting with different size values, it was discovered that ostringstream
's internal buffer is around 65KB, 65504 bytes to be exact. When the size is bigger, strange shifts and crippled bytes occur.
The workaround is to use:
stream.rdbuf()->pubsetbuf((const char*)payload, payloadSize)
instead of write
method. But when this scope terminates, payload will be invalidated and stream
cannot be used anywhere else anymore. In my case, it needed to be used somewhere else.
This showed that:
- I was indeed right that the issue is with
ostringstream
but not with the hash or anything else. - String streams of STL apparently have a default buffer size limit. This is to be remembered in the future.