Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/traxel/crypto/Sha1Util.java


1   /* $Id: Sha1Util.java,v 1.1 2001/04/02 08:44:06 cvsbob Exp $ */
2   
3   /*
4    * Sha1Util.java, Generates secure hashes for message authentication.
5    * Copyright (C) 2001 Robert Bushman.
6    *
7    * I reserve the right to release this program under seperate license.
8    * If you require a special license grant contact Robert Bushman.
9    *
10   * This program is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU General Public License
12   * as published by the Free Software Foundation; either version 2
13   * of the License, or (at your option) any later version.
14   *
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Public License
21   * along with this program; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
23   * 02111-1307, USA.
24   */
25  
26  package com.traxel.crypto;
27  
28  import java.math.BigInteger;
29  import java.security.MessageDigest;
30  import java.security.NoSuchAlgorithmException;
31  
32  public class Sha1Util {
33      
34      // ------------------------------------------------------------
35      // CONSTANTS
36      // ------------------------------------------------------------
37      
38      public static final String SHA1 = "SHA1";
39      
40      // ------------------------------------------------------------
41      // VERIFICATION
42      // ------------------------------------------------------------
43      
44      /**
45       * @exception NumberFormatException When sha1Base16 is not base 16.
46       */
47      public static boolean verify( byte[] message, String sha1Base16 )
48          throws NumberFormatException {
49          
50          BigInteger sha1BigInt;
51          byte[] sha1Bytes;
52          
53          sha1BigInt = new BigInteger( sha1Base16, 16 );
54          sha1Bytes = sha1BigInt.toByteArray();
55          
56          return( verify( message, sha1Bytes ) );
57      }
58      
59      public static boolean verify( byte[] message, byte[] digest ) {
60          byte[] messageSha1;
61          boolean isVerified;
62          
63          messageSha1 = getSha1( message );
64          isVerified = MessageDigest.isEqual( messageSha1, digest );
65          
66          return( isVerified );
67      }
68      
69      
70      // -----------------------------------------------------------
71      // DIGESTING
72      // -----------------------------------------------------------
73      
74      public static byte[] getSha1( byte[] message ) {
75          MessageDigest digest = null;
76          byte[] sha1;
77          
78          try {
79              digest = MessageDigest.getInstance( SHA1 );
80          } catch( NoSuchAlgorithmException e ) {
81              e.printStackTrace();
82              System.out.println
83                  ( "Security provider not installed or SHA-1 unavailable." );
84              System.exit( 1 );
85          }
86          sha1 = digest.digest( message );
87          
88          return( sha1 );
89      }
90      
91      // ----------------------------------------------------------
92      // RUNTIME
93      // ----------------------------------------------------------
94      
95      public static void main( String[] args ) {
96          if( args == null || args.length != 1 ) {
97              System.out.println
98                  ( "Usage: java com.traxel.crypto.Sha1Util <message>" );
99          }
100         CryptoUtil.initializeProvider( CryptoUtil.DEFAULT_PROVIDER );
101         byte[] digest = getSha1( args[0].getBytes() );
102         BigInteger digestInt = new BigInteger( digest );
103         System.out.println( digestInt.toString( 16 ) );
104     }
105     
106 }
107