public class MessageDigestGenerator extends Object
MessageDigest and provides convenience methods making it easier to generate
hashes.
There are static methods in here that use MD5 as the default algorithm. If you want to use another
algorithm, instantiate an instance of this object and use its instance methods.| Modifier and Type | Field and Description |
|---|---|
static String |
MD5
you can pass this to the constructor to indicate you want to generate MD5 message digests
|
static String |
SHA_256
you can pass this to the constructor to indicate you want to generate SHA 256 message digests
|
| Constructor and Description |
|---|
MessageDigestGenerator()
Creates a new
MessageDigestGenerator object using MD5 as the default algorithm. |
MessageDigestGenerator(String algorithm)
Creates a new MessageDigestGenerator using the specified algorithm.
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(byte[] bytes)
Use this to add more data to the set of data used to calculate the hash.
|
void |
add(byte[] bytes,
int offset,
int length)
Use this to add more data to the set of data used to calculate the hash.
|
void |
add(InputStream is)
Use this to add more data to the set of data used to calculate the digest.
|
byte[] |
calcDigest(File file) |
byte[] |
calcDigest(InputStream is) |
byte[] |
calcDigest(String source_str) |
byte[] |
calcDigest(URL url) |
String |
calcDigestString(byte[] bytes) |
String |
calcDigestString(File file) |
String |
calcDigestString(InputStream is) |
String |
calcDigestString(String source_str) |
String |
calcDigestString(URL url) |
byte[] |
getDigest()
After all the data has been added to the message digest via add methods, this method is used to
finalize the digest calcualation and return the digest.
|
static byte[] |
getDigest(File file)
Calculates the digest for a given file.
|
static byte[] |
getDigest(InputStream is)
Returns the digest for the data found in the given stream.
|
static byte[] |
getDigest(String source_str)
Calculates a digest for a given string.
|
String |
getDigestString()
After all the data has been added to the message digest via add methods,
this method is used to finalize the digest calcualation and return the digest as a String.
|
static String |
getDigestString(File file)
Calculates the digest for a given file.
|
static String |
getDigestString(InputStream is)
Similar to
getDigest(InputStream), only this returns the digest as a String. |
static String |
getDigestString(String source_str)
Calculates a digest for a given string and returns the digest's String representation.
|
static String |
getDigestString(URL url)
Calculates the digest for a given file.
|
MessageDigest |
getMessageDigest()
Returns the
MessageDigest object that is used to compute the digest. |
static void |
main(String[] args)
This can be used to generate the digest hash from the command line.
|
public static final String MD5
public static final String SHA_256
public MessageDigestGenerator()
MessageDigestGenerator object using MD5 as the default algorithm.
MD5 is used as the default algorithm for backward compatibility. It originally only supported MD5 and has since
been refactored to support algortithms that are supported by your version of Java.IllegalStateException - if the MD5 algorithm cannot be computed by the VMpublic MessageDigestGenerator(String algorithm)
algorithm - The algorithm to use (e.g., MD5, SHA-256)IllegalStateException - if the algorithm is not supported by the VMpublic MessageDigest getMessageDigest()
MessageDigest object that is used to compute the digest.public void add(InputStream is) throws IOException
getDigest() to get the final value.is - the stream whose data is to be part of the set of data from which the digest is to be calculatedIOException - if there was a problem reading from the streampublic void add(byte[] bytes)
getDigest() to get the final digest value.
If bytes is null, this method is a no-op and simply returns.
bytes - data to be part of the set of data from which the digest is to be calculatedpublic void add(byte[] bytes,
int offset,
int length)
getDigest() to get the final digest value.
If bytes is null, this method is a no-op and simply returns.
bytes - data to be part of the set of data from which the digest is to be calculatedoffset - the offset to start from in the array of byteslength - the number of bytes to use, starting at offsetpublic byte[] getDigest()
getDigestString() instead.public String getDigestString()
getDigest() instead.public static byte[] getDigest(InputStream is) throws IOException
is - the stream whose data is to be used to calculate the digestIOException - if failed to read the stream for some reasonpublic byte[] calcDigest(InputStream is) throws IOException
IOExceptionpublic static String getDigestString(InputStream is) throws IOException
getDigest(InputStream), only this returns the digest as a String.is - the stream whose data is to be used to calculate the digestIOException - if failed to read the stream for some reasonpublic String calcDigestString(InputStream is) throws IOException
IOExceptionpublic static byte[] getDigest(String source_str)
source_str - the string whose contents will be used as the data to calculate the digestRuntimeException - if a system error occurred - should never really happenpublic byte[] calcDigest(String source_str)
public static String getDigestString(String source_str)
source_str - the string whose contents will be used as the data to calculate the digestpublic static byte[] getDigest(File file) throws IOException
file - the file whose contents are to be used to calculate the digest.IOException - if the file could not be read or accessedpublic byte[] calcDigest(File file) throws IOException
IOExceptionpublic byte[] calcDigest(URL url) throws IOException
IOExceptionpublic static String getDigestString(File file) throws IOException
file - the file whose contents are to be used to calculate the digest.IOException - if the file could not be read or accessedpublic String calcDigestString(File file) throws IOException
IOExceptionpublic String calcDigestString(byte[] bytes) throws IOException
IOExceptionpublic static String getDigestString(URL url) throws IOException
url - the URL whose contents are to be used to calculate the digest.IOException - if the URL could not be read or accessedpublic String calcDigestString(URL url) throws IOException
IOExceptionCopyright © 2008-2014 Red Hat, Inc.. All Rights Reserved.