How to save MessageDigest internal state in database?

Is it possible, and if, how, to preserve the internal state of the MessageDigest object? I want to store it in a database, so I need to use only primitive data, such as String , int , byte[] .

What I'm trying to achieve is to get a fragmented file (for a long period of time), save all the fragments in the database and after receiving the last fragment, check the SHA512 digest of the file without returning all the data previously stored in the database.

So basically I want something like this:

 MessageDigest md = MessageDigest.getInstance("SHA-512"); // restore previous internal state of md md.update(dataSegment); // save internal md state 
+6
source share
1 answer

you can serialize the object to String (XML format) and return it back.

Validation: http://x-stream.imtqy.com/tutorial.html

 public class DigestTest { private static final byte[] TEST_DATA = "Some test data for digest computations".getBytes(); @Test public void shouldStoreAndRestoreDigest() throws Exception { final MessageDigest referenceDigest = MessageDigest.getInstance("SHA-512"); MessageDigest testDigest = MessageDigest.getInstance("SHA-512"); referenceDigest.update(TEST_DATA); testDigest.update(TEST_DATA); // store state final XStream xs = new XStream(new StaxDriver()); xs.alias("md", MessageDigest.class); final String serializedMd = xs.toXML(testDigest); System.out.println(serializedMd); // restore state testDigest = (MessageDigest)xs.fromXML(serializedMd); // --- referenceDigest.update(TEST_DATA); testDigest.update(TEST_DATA); Assert.assertArrayEquals(referenceDigest.digest(), testDigest.digest()); } } 
+1
source

All Articles