001 /* 002 * MD5Hash.java 003 * 004 * Created on July 27, 2004, 1:42 PM 005 * 006 * This file is part of the STAR Scheduler. 007 * Copyright (c) 2002-2006 STAR Collaboration - Brookhaven National Laboratory 008 * 009 * STAR Scheduler is free software; you can redistribute it and/or modify 010 * it under the terms of the GNU General Public License as published by 011 * the Free Software Foundation; either version 2 of the License, or 012 * (at your option) any later version. 013 * 014 * STAR Scheduler is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public License 020 * along with STAR Scheduler; if not, write to the Free Software 021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 022 */ 023 024 package gov.bnl.star.offline.scheduler.util; 025 026 import java.security.*; 027 028 029 /** 030 * Make MD5 hashs out of strings. 031 * @author Levente Hajdu 032 */ 033 public class MD5Hash extends Object { 034 035 036 /** 037 * Private function to turn md5 result to 32 hex-digit string 038 */ 039 private static String asHex (byte hash[]) { 040 StringBuffer buf = new StringBuffer(hash.length * 2); 041 int i; 042 043 for (i = 0; i < hash.length; i++) { 044 if (((int) hash[i] & 0xff) < 0x10) 045 buf.append("0"); 046 047 buf.append(Long.toString((int) hash[i] & 0xff, 16)); 048 } 049 050 return buf.toString().toUpperCase(); 051 } 052 053 /** 054 * Take a string and return its md5 hash as a hex digit string 055 */ 056 public static String hash(String arg) 057 { 058 return hash(arg.getBytes()); 059 } 060 061 /** 062 * Non static version for VB 063 */ 064 public String doHash(String arg) 065 { 066 return hash(arg.getBytes()); 067 } 068 069 /** 070 * Take a byte array and return its md5 hash as a hex digit string 071 */ 072 public static String hash(byte barray[]) 073 { 074 String restring = ""; 075 try{ 076 MessageDigest md = MessageDigest.getInstance("MD5"); 077 md.update(barray); 078 byte[] result = md.digest(); 079 restring = asHex(result); 080 } catch (NoSuchAlgorithmException nsa){ } 081 082 return restring; 083 } 084 085 086 }