Any reason the hash classes don't accept partial data?

When hashing stuff, I usually need to do something like this with the JUCE hash classes :

  MD5 getHash() const
{
	MemoryBlock mb;
	for (int i = 0; i < m_nodes.size(); ++i)
	{
		mb.append((void*)&m_nodes[i].Time, sizeof(double));
		mb.append((void*)&m_nodes[i].Value, sizeof(double));
		mb.append((void*)&m_nodes[i].ShapeParam1, sizeof(double));
		mb.append((void*)&m_nodes[i].ShapeParam2, sizeof(double));
	}
	return MD5(mb);
}

It would be great to avoid having to construct and fill the temporary MemoryBuffer. Like this :

MD5 getHash() const
{
	MD5 hash;
	for (int i = 0; i < m_nodes.size(); ++i)
	{
		hash.append((void*)&m_nodes[i].Time, sizeof(double));
		hash.append((void*)&m_nodes[i].Value, sizeof(double));
		hash.append((void*)&m_nodes[i].ShapeParam1, sizeof(double));
		hash.append((void*)&m_nodes[i].ShapeParam2, sizeof(double));
		// or even better, allow appending values directly using a function template template<typename T> MD5::append(const T& x)
		// hash.append(m_nodes[i].Time);
	}
	hash.finalizeOutput(); // maybe some algorithms would need to zero the rest of an internal buffer?
	return hash;
}

Is there some subtle or not so subtle reason why the hash classes couldn’t work like that?

2 Likes

Yes, good request. Having a quick look at the MD5 class, it already does have an internal class that can work that way, so could probably add this pretty easily. Will take a look if I get chance, or you could send me a PR if you’re in a hurry :wink:

2 Likes

No hurry. Glad to hear this was a reasonable request! :slight_smile:

I would also like to +1 this. Should I make a new thread as a Feature Request?

1 Like

+1’ing myself too…I’ve been patiently waiting… :wink:

Now posted as a feature request, please cast your votes!

1 Like