View Javadoc

1   /*
2    * Crypto.java
3    *
4    * Created on October 19, 2006, 10:08 PM
5    *
6    *
7    */
8   
9   package org.sourceforge.vlibrary.util;
10  
11  /**
12   * @version $Revision$ $Date$
13   */
14  
15  
16  import java.security.MessageDigest;
17  
18  import org.apache.log4j.Logger;
19  
20  
21  public class CryptoImpl implements Crypto {
22      /** log4j Logger */
23      private static Logger logger =
24       Logger.getLogger(Crypto.class.getName());
25      
26      private String algorithm = "MD5";
27      
28      public void setAlgorithm( String algorithm) {
29          this.algorithm = algorithm;
30      }
31      
32      public final String encrypt( String input ) throws Exception {
33          try {
34              MessageDigest messageDigest = (MessageDigest)MessageDigest.getInstance(algorithm).clone();
35              
36              messageDigest.reset();
37              
38              messageDigest.update( input.getBytes());
39              
40              String output = convert( messageDigest.digest());
41              
42              return output;
43          } catch(Throwable ex) {
44              if (logger.isDebugEnabled()) {
45                  logger.debug( "Fatal Error while digesting input string", ex);
46              }
47          }
48          
49          return input;
50      }
51      
52      /**
53       * From org.apache.catalina.util.HexUtils (c)Apache Software Foundation ASL 2.0
54       *
55       * Convert a byte array into a printable format containing a
56       * String of hexadecimal digit characters (two per byte).
57       *
58       * @param bytes Byte array representation
59       */
60      public String convert(byte bytes[]) {
61          StringBuffer sb = new StringBuffer(bytes.length * 2);
62          
63          for (int i = 0; i < bytes.length; i++) {
64              sb.append(convertDigit((int) (bytes[i] >> 4)));
65              
66              sb.append(convertDigit((int) (bytes[i] & 0x0f)));
67          }
68          
69          return (sb.toString());
70      }
71      
72      
73      /**
74       * From org.apache.catalina.util.HexUtils (c)Apache Software Foundation ASL 2.0
75       *
76       * [Private] Convert the specified value (0 .. 15) to the corresponding
77       * hexadecimal digit.
78       *
79       * @param value Value to be converted
80       */
81      private static char convertDigit(int value) {
82          value &= 0x0f;
83          
84          if (value >= 10) {
85              return ((char) (value - 10 + 'a'));
86          } else {
87              return ((char) (value + '0'));
88          }
89      }
90      
91      public static void main(String[] args) throws Exception {
92          CryptoImpl crypto = new CryptoImpl();
93          
94          if (args.length < 1) {
95              System.out.println("Insufficient number of parameters.  Usage> CryptoImpl inputString [algorithm]");
96              
97              System.exit(-1);
98          }
99          
100         if (args.length < 2) {
101             crypto.setAlgorithm("MD5");
102         }
103         
104         System.out.println("InputString=" + args[0]);
105         
106         System.out.println("Digested string=" + crypto.encrypt(args[0]));
107     }
108 }