1 17 18 package org.apache.james.security; 19 20 import javax.mail.MessagingException ; 21 import javax.mail.internet.MimeUtility ; 22 import java.io.*; 23 import java.security.MessageDigest ; 24 import java.security.NoSuchAlgorithmException ; 25 import java.util.Locale ; 26 27 28 34 public class DigestUtil { 35 36 41 public static void main(String [] args) { 42 43 String alg = "SHA"; 44 boolean file = false; 45 46 if (args.length == 0 || args.length > 4) { 47 printUsage(); 48 return; 49 } 50 51 for (int i = 0; i < args.length; i++) { 52 String currArg = args[i].toLowerCase(Locale.US); 53 if (currArg.equals("-help") 54 || currArg.equals("-usage")) { 55 printUsage(); 56 return; 57 } 58 if (currArg.equals("-alg")) { 59 alg = args[i+1]; 60 } 61 if (currArg.equals("-file")) { 62 file = true; 63 } 64 } 65 66 if (file) { 67 digestFile(args[args.length - 1], alg); 68 return ; 69 } else { 70 try { 71 String hash = digestString(args[args.length - 1], alg); 72 System.out.println("Hash is: " + hash); 73 return; 74 } catch (NoSuchAlgorithmException nsae) { 75 System.out.println("No such algorithm available"); 76 } 77 } 78 } 79 80 83 public static void printUsage() { 84 System.out.println("Usage: " 85 + "java org.apache.james.security.DigestUtil" 86 + " [-alg algorithm]" 87 + " [-file] filename|string"); 88 } 89 90 97 public static void digestFile(String filename, String algorithm) { 98 byte[] b = new byte[65536]; 99 int count = 0; 100 int read = 0; 101 FileInputStream fis = null; 102 FileOutputStream fos = null; 103 try { 104 MessageDigest md = MessageDigest.getInstance(algorithm); 105 fis = new FileInputStream(filename); 106 while (fis.available() > 0) { 107 read = fis.read(b); 108 md.update(b, 0, read); 109 count += read; 110 } 111 byte[] digest = md.digest(); 112 StringBuffer fileNameBuffer = 113 new StringBuffer (128) 114 .append(filename) 115 .append(".") 116 .append(algorithm); 117 fos = new FileOutputStream(fileNameBuffer.toString()); 118 OutputStream encodedStream = MimeUtility.encode(fos, "base64"); 119 encodedStream.write(digest); 120 fos.flush(); 121 } catch (Exception e) { 122 System.out.println("Error computing Digest: " + e); 123 } finally { 124 try { 125 fis.close(); 126 fos.close(); 127 } catch (Exception ignored) {} 128 } 129 } 130 131 141 public static String digestString(String pass, String algorithm ) 142 throws NoSuchAlgorithmException { 143 144 MessageDigest md; 145 ByteArrayOutputStream bos; 146 147 try { 148 md = MessageDigest.getInstance(algorithm); 149 byte[] digest = md.digest(pass.getBytes("iso-8859-1")); 150 bos = new ByteArrayOutputStream(); 151 OutputStream encodedStream = MimeUtility.encode(bos, "base64"); 152 encodedStream.write(digest); 153 return bos.toString("iso-8859-1"); 154 } catch (IOException ioe) { 155 throw new RuntimeException ("Fatal error: " + ioe); 156 } catch (MessagingException me) { 157 throw new RuntimeException ("Fatal error: " + me); 158 } 159 } 160 161 164 private DigestUtil() {} 165 } 166 | Popular Tags |