src/core/net/sf/basedb/util/MD5.java

Code
Comments
Other
Rev Date Author Line
30 23 Feb 05 nicklas 1 /*
192 17 Mar 05 jari 2   $Id$
30 23 Feb 05 nicklas 3
4889 06 Apr 09 nicklas 4   Copyright (C) 2005 Jari Häkkinen, Nicklas Nordborg
4889 06 Apr 09 nicklas 5   Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson
30 23 Feb 05 nicklas 6
2304 22 May 06 jari 7   This file is part of BASE - BioArray Software Environment.
2304 22 May 06 jari 8   Available at http://base.thep.lu.se/
30 23 Feb 05 nicklas 9
30 23 Feb 05 nicklas 10   BASE is free software; you can redistribute it and/or
30 23 Feb 05 nicklas 11   modify it under the terms of the GNU General Public License
4479 05 Sep 08 jari 12   as published by the Free Software Foundation; either version 3
30 23 Feb 05 nicklas 13   of the License, or (at your option) any later version.
30 23 Feb 05 nicklas 14
30 23 Feb 05 nicklas 15   BASE is distributed in the hope that it will be useful,
30 23 Feb 05 nicklas 16   but WITHOUT ANY WARRANTY; without even the implied warranty of
30 23 Feb 05 nicklas 17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30 23 Feb 05 nicklas 18   GNU General Public License for more details.
30 23 Feb 05 nicklas 19
30 23 Feb 05 nicklas 20   You should have received a copy of the GNU General Public License
4515 11 Sep 08 jari 21   along with BASE. If not, see <http://www.gnu.org/licenses/>.
30 23 Feb 05 nicklas 22 */
49 28 Feb 05 nicklas 23 package net.sf.basedb.util;
30 23 Feb 05 nicklas 24
2884 09 Nov 06 martin 25 import net.sf.basedb.core.BaseException;
2884 09 Nov 06 martin 26
30 23 Feb 05 nicklas 27 import java.security.MessageDigest;
30 23 Feb 05 nicklas 28 import java.math.BigInteger;
7714 21 May 19 nicklas 29 import java.nio.charset.StandardCharsets;
30 23 Feb 05 nicklas 30
30 23 Feb 05 nicklas 31 /**
30 23 Feb 05 nicklas 32   This class calculates the MD5 hash of a string and
30 23 Feb 05 nicklas 33   returns it represented as a hexadecimal string.
30 23 Feb 05 nicklas 34
30 23 Feb 05 nicklas 35   @author Nicklas
30 23 Feb 05 nicklas 36   @version 2.0
30 23 Feb 05 nicklas 37 */
30 23 Feb 05 nicklas 38 public class MD5
30 23 Feb 05 nicklas 39 {
4951 27 May 09 nicklas 40   /**
4951 27 May 09 nicklas 41      Calculates the MD5 hash of an UTF-8 encoded string. 
4951 27 May 09 nicklas 42   */
30 23 Feb 05 nicklas 43   public static String getHashString(String in)
30 23 Feb 05 nicklas 44   {
30 23 Feb 05 nicklas 45     try
30 23 Feb 05 nicklas 46     {
30 23 Feb 05 nicklas 47       MessageDigest md5 = MessageDigest.getInstance("MD5");
7714 21 May 19 nicklas 48       BigInteger result = new BigInteger(1, md5.digest(in.getBytes(StandardCharsets.UTF_8)));
2243 11 May 06 nicklas 49       return leftPad(result.toString(16), '0', 32);
30 23 Feb 05 nicklas 50     }
30 23 Feb 05 nicklas 51     catch (Exception ex)
30 23 Feb 05 nicklas 52     {
2884 09 Nov 06 martin 53       throw new BaseException(ex);
30 23 Feb 05 nicklas 54     }
30 23 Feb 05 nicklas 55   }
710 02 Jun 05 nicklas 56   
710 02 Jun 05 nicklas 57   public static MessageDigest newInstance()
710 02 Jun 05 nicklas 58   {
710 02 Jun 05 nicklas 59     try
710 02 Jun 05 nicklas 60     {
710 02 Jun 05 nicklas 61       return MessageDigest.getInstance("MD5");
710 02 Jun 05 nicklas 62     }
710 02 Jun 05 nicklas 63     catch (Exception ex)
710 02 Jun 05 nicklas 64     {
2884 09 Nov 06 martin 65       throw new BaseException(ex);
710 02 Jun 05 nicklas 66     }
710 02 Jun 05 nicklas 67   }
710 02 Jun 05 nicklas 68   
710 02 Jun 05 nicklas 69   public static String getHashString(MessageDigest md5)
710 02 Jun 05 nicklas 70   {
710 02 Jun 05 nicklas 71     BigInteger result = new BigInteger(1, md5.digest());
2243 11 May 06 nicklas 72     return leftPad(result.toString(16), '0', 32);
710 02 Jun 05 nicklas 73   }
710 02 Jun 05 nicklas 74   
2243 11 May 06 nicklas 75   /**
2243 11 May 06 nicklas 76     If the string is shorter than the specified length, it is left padded 
2243 11 May 06 nicklas 77     with the specified character. If the string is null or longer than the specified
2243 11 May 06 nicklas 78     length it is returned unchanged.
2243 11 May 06 nicklas 79
2243 11 May 06 nicklas 80     @param toPad The string to pad
2243 11 May 06 nicklas 81     @param c The character to pad with
2243 11 May 06 nicklas 82     @param length The length of the resulting string
2243 11 May 06 nicklas 83     @return The left-padded string
2243 11 May 06 nicklas 84    */
2243 11 May 06 nicklas 85   public static String leftPad(String toPad, char c, int length)
2243 11 May 06 nicklas 86   {
2243 11 May 06 nicklas 87     
2243 11 May 06 nicklas 88     if (toPad != null && toPad.length() < length)
2243 11 May 06 nicklas 89     {
2243 11 May 06 nicklas 90       int numToPad = length - toPad.length();
2243 11 May 06 nicklas 91       char[] padding = new char[numToPad];
2243 11 May 06 nicklas 92       for (int i = 0; i < numToPad; ++i)
2243 11 May 06 nicklas 93       {
2243 11 May 06 nicklas 94         padding[i] = c;
2243 11 May 06 nicklas 95       }
2243 11 May 06 nicklas 96       toPad = new String(padding) + toPad;
2243 11 May 06 nicklas 97     }
2243 11 May 06 nicklas 98     return toPad;
2243 11 May 06 nicklas 99   }
2243 11 May 06 nicklas 100   
30 23 Feb 05 nicklas 101 }