001 /*
002 * This file is part of muCommander, http://www.mucommander.com
003 * Copyright (C) 2002-2008 Maxence Bernard
004 *
005 * muCommander is free software; you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation; either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * muCommander is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program. If not, see <http://www.gnu.org/licenses/>.
017 */
018
019 package com.mucommander.io.security;
020
021 import java.security.MessageDigest;
022 import java.util.zip.Checksum;
023
024 /**
025 * This class turns a <code>java.util.zip.Checksum</code> into a <code>java.security.MessageDigest</code>, allowing
026 * <code>Checksum</code> implementations to be used with the Java Cryptography Extension.
027 *
028 * @author Maxence Bernard
029 */
030 public class ChecksumMessageDigest extends MessageDigest {
031
032 /** The Checksum instance that performs all of the checksumming work */
033 private Checksum checksum;
034
035 /**
036 * Creates a new <code>ChecksumMessageDigest</code> that delegates all the checksumming work to the given
037 * <code>Checksum</code> instance.
038 *
039 * @param checksum the Checksum responsible for calculating the checksum
040 * @param algorithm the name of the checksum algorithm implemented by the Checksum
041 */
042 public ChecksumMessageDigest(Checksum checksum, String algorithm) {
043 super(algorithm);
044
045 this.checksum = checksum;
046 }
047
048
049 //////////////////////////////////
050 // MessageDigest implementation //
051 //////////////////////////////////
052
053 /**
054 * This method delegates to the underlying <code>java.util.zip.Checksum</code> instance.
055 */
056 protected void engineReset() {
057 checksum.reset();
058 }
059
060 /**
061 * This method delegates to the underlying <code>java.util.zip.Checksum</code> instance.
062 */
063 protected void engineUpdate(byte input) {
064 checksum.update(input);
065 }
066
067 /**
068 * This method delegates to the underlying <code>java.util.zip.Checksum</code> instance.
069 */
070 protected void engineUpdate(byte[] input, int offset, int len) {
071 checksum.update(input, offset, len);
072 }
073
074 /**
075 * This method delegates to the underlying <code>java.util.zip.Checksum</code> instance.
076 */
077 protected byte[] engineDigest() {
078 long crcLong = checksum.getValue();
079
080 byte[] crcBytes = new byte[4];
081 crcBytes[0] = (byte)((crcLong>>24) & 0xFF);
082 crcBytes[1] = (byte)((crcLong>>16) & 0xFF);
083 crcBytes[2] = (byte)((crcLong>>8) & 0xFF);
084 crcBytes[3] = (byte)(crcLong & 0xFF);
085
086 return crcBytes;
087 }
088 }