com.wjholden.md5test
Class Md5Test

java.lang.Object
  extended by com.wjholden.md5test.Md5Test

public class Md5Test
extends java.lang.Object

  <?xml version='1.0' encoding='ISO-8859-1' standalone='yes'?>
  <SecureDownload>
    <file name='abc.txt'>
      <chunk>26b6350aacc2c3105b7c5628080bae0b</chunk>
      <chunk>ab070fbcc4aeb75e742a3d0e71ab03db</chunk>
    </file>
  </SecureDownload>

Version:
1
Author:
William John Holden (wjholden@gmail.com)

Field Summary
private static java.lang.String USAGE
           
 
Constructor Summary
Md5Test()
           
 
Method Summary
private static void createXmlPackage(java.lang.String inputFile, java.lang.String outputFile, int chunksize, boolean showOutput)
          Build an XML file AND split inputFile into files where filename is the same as their MD5 hash.
private static java.lang.String hashByteArray(byte[] array)
          Calculate the MD5 sum of a byte array.

Warning: this method has a very tricky bug.
private static java.lang.String hashFile(java.lang.String filename, boolean showOutput)
          Calculate the MD5 hash of a file.
private static java.lang.String hashHttp(java.lang.String uri, boolean writeFile, boolean showOutput)
          Grabs a single file from the world wide web and calculates it's hash as it downloads.
private static void hashXml(java.lang.String filename, boolean showOutput)
          Doesn't actually perform the hashes, this was just me developing a method to parse the XML.
static void main(java.lang.String[] args)
           
private static void reassembleXmlPkg(java.lang.String xmlpkg, boolean showOutput)
          On the local filesystem, reassemble a file that was previously broken up by createXmlPackage, where xmlpkg is the filename of the XML file.
private static void reassembleXmlPkgFromURI(java.lang.String uri, boolean showOutput)
          This is mostly a copy-and-paste job from my earlier hashHttp and reassembleXmlPkg methods.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

USAGE

private static final java.lang.String USAGE
See Also:
Constant Field Values
Constructor Detail

Md5Test

public Md5Test()
Method Detail

createXmlPackage

private static void createXmlPackage(java.lang.String inputFile,
                                     java.lang.String outputFile,
                                     int chunksize,
                                     boolean showOutput)
                              throws java.io.IOException,
                                     java.security.NoSuchAlgorithmException
Build an XML file AND split inputFile into files where filename is the same as their MD5 hash.

Parameters:
inputFile -
outputFile -
chunksize -
showOutput -
Throws:
java.io.IOException
java.security.NoSuchAlgorithmException

hashByteArray

private static java.lang.String hashByteArray(byte[] array)
                                       throws java.security.NoSuchAlgorithmException
Calculate the MD5 sum of a byte array.

Warning: this method has a very tricky bug. If you're using this before writing a byte array to file/network, make sure you don't give this method a longer array than you need or the hash will be different.
   byte tooBig[8] = "Hello".getBytes()
   byte justRight[6] = "Hello".getBytes()
Hashing the two arrays shown above will result in different results. Stay safe.

Parameters:
array -
Returns:
Throws:
java.security.NoSuchAlgorithmException

hashFile

private static java.lang.String hashFile(java.lang.String filename,
                                         boolean showOutput)
                                  throws java.security.NoSuchAlgorithmException,
                                         java.io.IOException
Calculate the MD5 hash of a file.

Parameters:
filename -
showOutput -
Returns:
Throws:
java.security.NoSuchAlgorithmException
java.io.IOException

hashHttp

private static java.lang.String hashHttp(java.lang.String uri,
                                         boolean writeFile,
                                         boolean showOutput)
                                  throws java.security.NoSuchAlgorithmException,
                                         java.io.IOException
Grabs a single file from the world wide web and calculates it's hash as it downloads.

Parameters:
uri -
writeFile - If true, actually save the file to the current working directory, otherwise discard data.
showOutput -
Throws:
java.security.NoSuchAlgorithmException
java.io.IOException

hashXml

private static void hashXml(java.lang.String filename,
                            boolean showOutput)
                     throws javax.xml.parsers.ParserConfigurationException,
                            org.xml.sax.SAXException,
                            java.io.IOException
Doesn't actually perform the hashes, this was just me developing a method to parse the XML. Use reassembleXmlPkg.

Parameters:
filename -
showOutput -
Throws:
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException
java.io.IOException

main

public static void main(java.lang.String[] args)

reassembleXmlPkg

private static void reassembleXmlPkg(java.lang.String xmlpkg,
                                     boolean showOutput)
                              throws javax.xml.parsers.ParserConfigurationException,
                                     org.xml.sax.SAXException,
                                     java.io.IOException,
                                     java.security.NoSuchAlgorithmException
On the local filesystem, reassemble a file that was previously broken up by createXmlPackage, where xmlpkg is the filename of the XML file.

Parameters:
xmlpkg -
showOutput -
Throws:
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException
java.io.IOException
java.security.NoSuchAlgorithmException

reassembleXmlPkgFromURI

private static void reassembleXmlPkgFromURI(java.lang.String uri,
                                            boolean showOutput)
                                     throws javax.xml.parsers.ParserConfigurationException,
                                            org.xml.sax.SAXException,
                                            java.io.IOException,
                                            java.security.NoSuchAlgorithmException
This is mostly a copy-and-paste job from my earlier hashHttp and reassembleXmlPkg methods.

Parameters:
uri -
showOutput -
Throws:
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException
java.io.IOException
java.security.NoSuchAlgorithmException