// --------------------------------------------------------------------------- // jWebSocket - WebSocket Tools // Copyright (c) 2010 Alexander Schulze, Innotrade GmbH // --------------------------------------------------------------------------- // This program is free software; you can redistribute it and/or modify it // under the terms of the GNU Lesser General Public License as published by the // Free Software Foundation; either version 3 of the License, or (at your // option) any later version. // This program is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for // more details. // You should have received a copy of the GNU Lesser General Public License along // with this program; if not, see <http://www.gnu.org/licenses/lgpl.html>. // --------------------------------------------------------------------------- package org.jwebsocket.util; import java.security.MessageDigest; import java.util.Formatter; /** * Provides some convenience methods to support the web socket * development. * @author aschulze */ public class Tools { /** * Returns the MD5 sum of the given string. The output always has 32 digits. * @param aMsg String the string to calculate the MD5 sum for. * @return MD5 sum of the given string. */ public static String getMD5(String aMsg) { MessageDigest md = null; try { md = MessageDigest.getInstance("MD5"); byte[] lBufSource = aMsg.getBytes("UTF-8"); byte[] lBufTarget = md.digest(lBufSource); Formatter formatter = new Formatter(); for (byte b : lBufTarget) { formatter.format("%02x", b); } return (formatter.toString()); } catch (Exception ex) { // log.error("getMD5: " + ex.getMessage()); System.out.println("getMD5: " + ex.getMessage()); } return null; } /** * Returns the hex value of the given int as a string. If {@code aLen} is * greater than zero the output is cut or filled to the given length * otherwise the exact number of digits is returned. * @param aInt Integer to be converted into a hex-string. * @param aLen Number of hex digits (optionally filled or cut if needed) * @return Hex-string of the given integer. */ public static String intToHex(int aInt, int aLen) { String lRes = Integer.toHexString(aInt); if (aLen > 0 && lRes.length() > aLen ) { lRes = lRes.substring(0, aLen); } else { while(lRes.length() < aLen) { lRes = "0" + lRes.substring(0, aLen); } } return lRes; } }