You are here

You are here

Download Document - Verify Download with MD5 Hash

With native and PDF downloads it is possible to verify the document download was successful using an MD5 hash of the document.  The DocumentHash method will return an MD5 of the document stored in SpringCM.  An MD5 hash can then be calculated for the local document, and can be compared to the version in SpringCM to verify they match.

The following code samples show a native document download and then verification that the download was successful using the DocumentHash method.  Note that if the PDF rendition of the document was downloaded, PDF must specified for the DownloadFormat when calling the DocumentHash method.

Vertical Tabs

c#
string documentId = "<Document Id retrieved by other method calls>";
string localPathAndFileName = "<Local file system path and file name for downloaded file.>";
 
//First get the document, we need to do this to know the full file size 
//We can pass false for the last parameter since we won't need metadata 
SCMDocument document = springCMService.DocumentGetById(token, documentId, false);
 
//Create an in memory stream to write the downloaded document 
using (FileStream stream = new FileStream(localPathAndFileName, FileMode.Create))
{
 
	//Initialize the download variables 
	//position keeps track of where we are in downloading the file 
	//chunk size is how many bytes we ask SpringCM for at a time, in this 
	//case 1MB. 
	int position = 0;
	int chunkSize = 1048576;
 
	//The position increases until we have all the bytes 
	//so loop while there is still more to download 
	while (position < document.FileSize)
	{
		//The final chunk will likely be smaller than the chunksize 
		//so we check if the next 1MB chunk would take us past the 
		//bytes left to be downloaded. If this is the case, 
		//we change the chunksize to the final set of the bytes 
		//We have to check everytime in this loop to see when we get 
		//to the end. 
		if (position + chunkSize >= document.FileSize)
		{
			chunkSize = (int) (document.FileSize - position);
		}
 
		//Get the current chunk, and then write it to the stream 
		byte[] currentChunk = springCMService.DocumentDownload(token, document.Id, position, chunkSize, DownloadFormat.Native);
		stream.Write(currentChunk, 0, chunkSize);
 
		//Move the position to be ready for the next chunk 
		position += chunkSize;
	}
}
 
//Now verify the document local and on the server are the same
//and we don't have a corrupt document
var serverHash = springCMService.DocumentHash(token, documentId, DownloadFormat.Native);
using (var stream = File.Open(localPathAndFileName, FileMode.Open))
{
	byte[] localHash;
	using (MD5 md5 = new MD5CryptoServiceProvider())
	{
		localHash = md5.ComputeHash(stream);
	}
 
	Console.WriteLine(serverHash.SequenceEqual(localHash)
						  ? "Document downloaded successfully."
						  : "Downloaded document is corrupt.");
}
java
String documentId = "<Document Id retrieved by other method calls>";
String localPathAndFileName = "<Local file system path and file name for downloaded file.>";
 
//First get the document, we need to do this to know the full file size
//We can pass false for the last parameter since we won't need metadata
SCMDocument document = springCMService.documentGetById(token, documentId, false);
 
//Initialize the download variables
//position keeps track of where we are in downloading the file
//chunk size is how many bytes we ask SpringCM for at a time, in this
//case 1MB.  
int position = 0;
int chunkSize = 1048576;
 
//The position increases until we have all the bytes
//so loop while there is still more to download
ByteBuffer downloadedDocument = ByteBuffer.allocate((int)document.getFileSize());
 
while (position < document.getFileSize())
{
	//The final chunk will likely be smaller than the chunksize
	//so we check if the next 1MB chunk would take us past the
	//bytes left to be downloaded.  If this is the case,
	//we change the chunksize to the final set of the bytes
	//We have to check everytime in this loop to see when we get
	//to the end.
	if (position + chunkSize >= document.getFileSize())
	{
		chunkSize = (int)(document.getFileSize() - position);
	}
 
	//Get the current chunk, and then write it to the stream
	byte[] currentChunk = springCMService.documentDownload(token, document.getId(), position, chunkSize,DownloadFormat.Native);
	downloadedDocument.put(currentChunk, 0, chunkSize);
 
	//Move the position to be ready for the next chunk
	position += chunkSize;
 
}
 
FileOutputStream fileOutputStream = new FileOutputStream(localPathAndFileName);	   
fileOutputStream.write(downloadedDocument.array());
fileOutputStream.close();
 
//Now verify the document local and on the server are the same
//and we don't have a corrupt document
byte[] serverHash = springCMService.documentHash(token, documentId, DownloadFormat.Native);
 
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] localHash = md.digest(Files.readAllBytes(Paths.get(localPathAndFileName)));
 
System.out.println(Arrays.equals(serverHash,localHash)
						  ? "Document downloaded successfully."
						  : "Downloaded document is corrupt.");