Cryptography Tutorials - Herong's Tutorial Examples - v5.42, by Herong Yang
Using SHA1 Message Digest in Java
This section provides a tutorial example on how to use SHA1 message digest algorithm in Java. The JDK JCE package offers the SHA1 algorithm through a generic message digest class, javax.security.MessageDigest.
Sun provides SHA1 algorithm in Java under their JCE (Java Cryptography Extension) package, which is included in JDK 1.5 and newer versions.
Sun's implementation of SHA1 can be accessed through a generic class called MessageDigest. Here are the main methods of MessageDigest class:
Here is a sample Java program to show you how to use the MessageDigest class to perform some tests on SHA1 algorithms.
/* JceSha1Test.java * Copyright (c) HerongYang.com. All Rights Reserved. */ import java.security.*; class JceSha1Test { public static void main(String[] a) { try { MessageDigest md = MessageDigest.getInstance("SHA1"); System.out.println("Message digest object info: "); System.out.println(" Algorithm = "+md.getAlgorithm()); System.out.println(" Provider = "+md.getProvider()); System.out.println(" toString = "+md.toString()); String input = ""; md.update(input.getBytes()); byte[] output = md.digest(); System.out.println(); System.out.println("SHA1(\""+input+"\") ="); System.out.println(" "+bytesToHex(output)); input = "abc"; md.update(input.getBytes()); output = md.digest(); System.out.println(); System.out.println("SHA1(\""+input+"\") ="); System.out.println(" "+bytesToHex(output)); input = "abcdefghijklmnopqrstuvwxyz"; md.update(input.getBytes()); output = md.digest(); System.out.println(); System.out.println("SHA1(\""+input+"\") ="); System.out.println(" "+bytesToHex(output)); } catch (Exception e) { System.out.println("Exception: "+e); } } public static String bytesToHex(byte[] b) { char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; StringBuffer buf = new StringBuffer(); for (int j=0; j<b.length; j++) { buf.append(hexDigit[(b[j] >> 4) & 0x0f]); buf.append(hexDigit[b[j] & 0x0f]); } return buf.toString(); } }
If you run this sample program, you should get the following output:
herong> javac JceSha1Test.java herong> java JceSha1Test Message digest object info: Algorithm = SHA1 Provider = SUN version 1.5 toString = SHA1 Message Digest from SUN, <initialized> SHA1("") = DA39A3EE5E6B4B0D3255BFEF95601890AFD80709 SHA1("abc") = A9993E364706816ABA3E25717850C26C9CD0D89D SHA1("abcdefghijklmnopqrstuvwxyz") = 32D10C7B8CF96570CA04CE37F2A19D84240D3A89
The output matches the testing result listed in FreeBSD libmd makefile: http://www.opensource.apple.com/source/libmd/libmd-2/Makefile.
Table of Contents
Introduction to AES (Advanced Encryption Standard)
DES Algorithm - Illustrated with Java Programs
DES Algorithm Java Implementation
DES Algorithm - Java Implementation in JDK JCE
DES Encryption Operation Modes
PHP Implementation of DES - mcrypt
Blowfish - 8-Byte Block Cipher
Secret Key Generation and Management
Cipher - Secret Key Encryption and Decryption
RSA Implementation using java.math.BigInteger Class
Introduction of DSA (Digital Signature Algorithm)
Java Default Implementation of DSA
Private key and Public Key Pair Generation
PKCS#8/X.509 Private/Public Encoding Standards
Cipher - Public Key Encryption and Decryption
►SHA1 Mesasge Digest Algorithm
What Is SHA1 Message Digest Algorithm?
SHA1 Message Digest Algorithm Overview
►Using SHA1 Message Digest in Java
Using SHA1 Message Digest in PHP
Using SHA1 Message Digest in Perl
OpenSSL Introduction and Installation
OpenSSL Generating and Managing RSA Keys
OpenSSL Generating and Signing CSR
OpenSSL Validating Certificate Path
"keytool" and "keystore" from JDK
"OpenSSL" Signing CSR Generated by "keytool"
Migrating Keys from "keystore" to "OpenSSL" Key Files
Certificate X.509 Standard and DER/PEM Formats
Migrating Keys from "OpenSSL" Key Files to "keystore"