// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mozilla.apache.commons.codec.digest;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.mozilla.apache.commons.codec.binary.Hex;
import org.mozilla.apache.commons.codec.binary.StringUtils;
/**
* Operations to simplify common {@link java.security.MessageDigest} tasks. This class is thread safe.
*
* @author Apache Software Foundation
* @version $Id: DigestUtils.java 1064793 2011-01-28 17:42:55Z ggregory $
*/
public class DigestUtils {
private static final int STREAM_BUFFER_LENGTH = 1024;
/**
* Read through an InputStream and returns the digest for the data
*
* @param digest
* The MessageDigest to use (e.g. MD5)
* @param data
* Data to digest
* @return MD5 digest
* @throws IOException
* On error reading from the stream
*/
private static byte[] digest(MessageDigest digest, InputStream data) throws IOException {
byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
while (read > -1) {
digest.update(buffer, 0, read);
read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
}
return digest.digest();
}
/**
* Calls {@link StringUtils#getBytesUtf8(String)}
*
* @param data
* the String to encode
* @return encoded bytes
*/
private static byte[] getBytesUtf8(String data) {
return StringUtils.getBytesUtf8(data);
}
/**
* Returns a MessageDigest
for the given algorithm
.
*
* @param algorithm
* the name of the algorithm requested. See Appendix A in the Java
* Cryptography Architecture API Specification & Reference for information about standard algorithm
* names.
* @return An MD5 digest instance.
* @see MessageDigest#getInstance(String)
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
static MessageDigest getDigest(String algorithm) {
try {
return MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e.getMessage());
}
}
/**
* Returns an MD5 MessageDigest.
*
* @return An MD5 digest instance.
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
private static MessageDigest getMd5Digest() {
return getDigest("MD5");
}
/**
* Returns an SHA-256 digest.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
byte[]
.
*
* @param data
* Data to digest
* @return MD5 digest
*/
public static byte[] md5(byte[] data) {
return getMd5Digest().digest(data);
}
/**
* Calculates the MD5 digest and returns the value as a 16 element byte[]
.
*
* @param data
* Data to digest
* @return MD5 digest
* @throws IOException
* On error reading from the stream
* @since 1.4
*/
public static byte[] md5(InputStream data) throws IOException {
return digest(getMd5Digest(), data);
}
/**
* Calculates the MD5 digest and returns the value as a 16 element byte[]
.
*
* @param data
* Data to digest
* @return MD5 digest
*/
public static byte[] md5(String data) {
return md5(getBytesUtf8(data));
}
/**
* Calculates the MD5 digest and returns the value as a 32 character hex string.
*
* @param data
* Data to digest
* @return MD5 digest as a hex string
*/
public static String md5Hex(byte[] data) {
return Hex.encodeHexString(md5(data));
}
/**
* Calculates the MD5 digest and returns the value as a 32 character hex string.
*
* @param data
* Data to digest
* @return MD5 digest as a hex string
* @throws IOException
* On error reading from the stream
* @since 1.4
*/
public static String md5Hex(InputStream data) throws IOException {
return Hex.encodeHexString(md5(data));
}
/**
* Calculates the MD5 digest and returns the value as a 32 character hex string.
*
* @param data
* Data to digest
* @return MD5 digest as a hex string
*/
public static String md5Hex(String data) {
return Hex.encodeHexString(md5(data));
}
/**
* Calculates the SHA-1 digest and returns the value as a byte[]
.
*
* @param data
* Data to digest
* @return SHA-1 digest
*/
public static byte[] sha(byte[] data) {
return getShaDigest().digest(data);
}
/**
* Calculates the SHA-1 digest and returns the value as a byte[]
.
*
* @param data
* Data to digest
* @return SHA-1 digest
* @throws IOException
* On error reading from the stream
* @since 1.4
*/
public static byte[] sha(InputStream data) throws IOException {
return digest(getShaDigest(), data);
}
/**
* Calculates the SHA-1 digest and returns the value as a byte[]
.
*
* @param data
* Data to digest
* @return SHA-1 digest
*/
public static byte[] sha(String data) {
return sha(getBytesUtf8(data));
}
/**
* Calculates the SHA-256 digest and returns the value as a byte[]
.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
byte[]
.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
byte[]
.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
byte[]
.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
byte[]
.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
byte[]
.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
byte[]
.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
byte[]
.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
byte[]
.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*
* Throws a RuntimeException
on JRE versions prior to 1.4.0.
*