@@ -0,0 +1,2713 @@
+package checksum;%0A%0Aimport java.io.BufferedInputStream;%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.InputStream;%0Aimport java.io.IOException;%0Aimport java.security.DigestInputStream;%0Aimport java.security.MessageDigest;%0Aimport java.security.NoSuchAlgorithmException;%0A%0Apublic class ChecksumGenerator%0A%7B%0A  private MessageDigest digester;%0A%0A  /**%0A   *  Creates a new ChecksumGenerator-object which uses the given algorithm.%0A   *%0A   *  @param algorithm algorithm to use%0A   */%0A  public ChecksumGenerator(String algorithm) throws NoSuchAlgorithmException%0A  %7B%0A    digester = MessageDigest.getInstance(algorithm.toUpperCase());%0A  %7D%0A%0A  /**%0A   *  Returns the checksum for data in given input stream.%0A   *%0A   *  @param input  input stream%0A   *  @param length length of data%0A   *  @return       checksum for data in given input stream%0A   */%0A  public byte%5B%5D getChecksum(InputStream in, long length) throws IOException%0A  %7B%0A    DigestInputStream input = new DigestInputStream(in, digester); %0A%0A    byte%5B%5D buffer = new byte%5B262144%5D;%0A    long bytesRead = 0; %0A%0A    while (bytesRead %3C length)%0A      bytesRead += input.read(buffer);%0A%0A    return digester.digest();%0A  %7D%0A%0A  /**%0A   *  Prints help text.%0A   */%0A  private static void printHelpText()%0A  %7B%0A    System.out.println(%22Usage:%5Cn%22 +%0A                       %22java checksum.ChecksumGenerator ALGORITHM FILE%5Cn%22 +%0A                       %22%5CtALGORITHM - The Hash-algorithm to use (%5C%22SHA-1%5C%22, %5C%22SHA-256%5C%22, %5C%22MD5%5C%22)%5Cn%22 +%0A                       %22%5CtFILE - The file to hash%22);%0A  %7D%0A%0A  /**%0A   *  Returns a String-representation of given checksum.%0A   *%0A   *  @param checksum checksum byte-array%0A   *  @return         String-representation of given checksum%0A   */%0A  public static String getChecksumString(byte%5B%5D checksum)%0A  %7B%0A    String checksumString = %22%22;%0A    for (byte b : checksum)%0A      checksumString += String.format(%22%25x%22, b);%0A      %0A    return checksumString;%0A  %7D%0A%0A  public static void main(String%5B%5D args)%0A  %7B%0A    if (args.length %3E 1)%0A    %7B%0A      String algorithm = args%5B0%5D,%0A             fileName  = args%5B1%5D;%0A%0A      File file = new File(fileName);%0A%0A      if (file.exists())%0A      %7B%0A        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)))%0A        %7B%0A          ChecksumGenerator generator = new ChecksumGenerator(algorithm);%0A          String checksum = getChecksumString(generator.getChecksum(in, file.length()));%0A%0A          System.out.println(checksum);%0A        %7D%0A        catch (Exception e)%0A        %7B%0A          System.err.println(e.getMessage());%0A          e.printStackTrace();%0A        %7D%0A      %7D%0A      else%0A      %7B%0A        System.err.printf(%22The file %5C%22%25s%5C%22 does not exist!%5Cn%22, fileName);%0A      %7D%0A    %7D%0A    else%0A      printHelpText();%0A  %7D%0A%7D%0A

Implement a simple checksum generator