This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.checksum;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.IOException;
023import java.nio.ByteBuffer;
024import java.nio.MappedByteBuffer;
025import java.nio.channels.FileChannel;
026import java.nio.file.Path;
027import java.nio.file.StandardOpenOption;
028import java.security.MessageDigest;
029import java.security.NoSuchAlgorithmException;
030
031/**
032 * Checksum - simple checksum hashing routines.
033 */
034public class Checksum
035{
036    private byte[] result = new byte[0];
037
038    private final MessageDigest md;
039
040    private ChecksumAlgorithm checksumAlgorithm;
041
042    public Checksum( ChecksumAlgorithm checksumAlgorithm )
043    {
044        this.checksumAlgorithm = checksumAlgorithm;
045        try
046        {
047            md = MessageDigest.getInstance( checksumAlgorithm.getAlgorithm() );
048        }
049        catch ( NoSuchAlgorithmException e )
050        {
051            // Not really possible, but here none-the-less
052            throw new IllegalStateException(
053                "Unable to initialize MessageDigest algorithm " + checksumAlgorithm.getAlgorithm() + " : "
054                    + e.getMessage(), e );
055        }
056    }
057
058    public String getChecksum()
059    {
060        if (this.result.length==0) {
061            finish();
062        }
063        return Hex.encode( this.result );
064    }
065
066    public byte[] getChecksumBytes() {
067        if (this.result.length==0) {
068            finish();
069        }
070        return this.result;
071    }
072
073    public ChecksumAlgorithm getAlgorithm()
074    {
075        return this.checksumAlgorithm;
076    }
077
078    public void reset()
079    {
080        md.reset();
081        this.result = new byte[0];
082    }
083
084    public Checksum update( byte[] buffer, int offset, int size )
085    {
086        if (this.result.length!=0) {
087            reset();
088        }
089        md.update( buffer, 0, size );
090        return this;
091    }
092
093    public Checksum update( ByteBuffer buffer)
094    {
095        if (this.result.length!=0) {
096            reset();
097        }
098        md.update( buffer );
099        return this;
100    }
101
102    public Checksum finish() {
103        this.result = md.digest();
104        return this;
105    }
106
107    public boolean compare(byte[] cmp) {
108        if (this.result == null || this.result.length==0) {
109            finish();
110        }
111        return md.isEqual( this.result, cmp );
112    }
113
114    public boolean compare(String hexString) {
115        if (this.result == null || this.result.length==0) {
116            finish();
117        }
118        return md.isEqual(this.result, Hex.decode( hexString ));
119    }
120}