|
JCA - Message Digest
Part:
1
2
This chapter describes some parts of the JCA (Java Cryptography Architecture)
which has been included in JDK since 1.1:
- What is message digest?
- The MessageDigest class in JDK.
- A sample program on MessageDigest class.
What Is Message Digest
Message digest is a one-way hash function that takes arbitrary-sized data
and outputs a fixed-length hash value.
Known message digest algorithms are:
- SHA - The Secure Hash Algorithm, as defined in Secure Hash Standard, NIST FIPS 180-1.
- MD2 - The MD2 message digest algorithm as defined in RFC 1319.
- MD5 - The MD5 message digest algorithm as defined in RFC 1321.
The MessageDigest Class
java.security.MessageDigest is an abstract class providing a link to implementation classes
of message digest algorithms provided by various security package providers.
Major methods in the MessageDigest class:
getInstance() - Returns a message digest object of the specified algorithm from
the implementation of the specified provider. If provider is not specified,
the default implementation is used. This is a static method.
update() - Adds more data to the current message digest object for processing.
digest() - Returns the digest data of the input data processed so far in the
current message digest object.
It also removes the input data received in the object.
getAlgorithm() - Returns the algorithm name of the current message digest object.
getProvider() - Returns the provider as a provider object of the current message digest object.
Message Digest Sample Program - JcaMessageDigest.java
The following sample program shows you how to invoke the message digest algorithms
implemented by the default provider, Sun, and digest the data from the input file.
/**
* JcaMessageDigest.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import java.io.*;
import java.security.*;
class JcaMessageDigest {
public static void main(String[] a) {
if (a.length<3) {
System.out.println("Usage:");
System.out.println("java JcaMessageDigest input output"
+" algorithm");
return;
}
String input = a[0];
String output = a[1];
String algorithm = a[2]; // SHA, MD5
try {
digest(input,output,algorithm);
} catch (Exception e) {
System.out.println("Exception: "+e);
return;
}
}
private static void digest(String input, String output,
String algorithm) throws Exception {
MessageDigest md = MessageDigest.getInstance(algorithm);
System.out.println();
System.out.println("MessageDigest Object Info: ");
System.out.println("Algorithm = "+md.getAlgorithm());
System.out.println("Provider = "+md.getProvider());
System.out.println("Digest Length = "+md.getDigestLength());
System.out.println("toString = "+md.toString());
FileInputStream in = new FileInputStream(input);
int bufSize = 1024;
byte[] buffer = new byte[bufSize];
int n = in.read(buffer,0,bufSize);
int count = 0;
while (n!=-1) {
count += n;
md.update(buffer,0,n);
n = in.read(buffer,0,bufSize);
}
in.close();
FileOutputStream out = new FileOutputStream(output);
byte[] digest = md.digest();
out.write(digest);
out.close();
System.out.println();
System.out.println("Message Digest Processing Info: ");
System.out.println("Number of input bytes = "+count);
System.out.println("Number of output bytes = "+digest.length);
}
}
(Continued on next part...)
Part:
1
2
|