Mega Code Archive

 
Categories / Android / Security
 

Md5 String

/*  * Copyright 2010 Renren, Inc.  *  * Licensed under the Apache License, Version 2.0 (the "License");  * you may not use this file except in compliance with the License.  * You may obtain a copy of the License at  *   *    http://www.apache.org/licenses/LICENSE-2.0  *  * Unless required by applicable law or agreed to in writing, software  * distributed under the License is distributed on an "AS IS" BASIS,  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  * See the License for the specific language governing permissions and  * limitations under the License.  */ //package com.renren.api.connect.android; import java.io.UnsupportedEncodingException; import java.security.MessageDigest;  class Util {     public static String md5(String string) {       if (string == null || string.trim().length() < 1) {         return null;       }       try {         return getMD5(string.getBytes("UTF-8"));       } catch (UnsupportedEncodingException e) {         throw new RuntimeException(e.getMessage(), e);       }     }     private static String getMD5(byte[] source) {       try {         MessageDigest md5 = MessageDigest.getInstance("MD5");         StringBuffer result = new StringBuffer();         for (byte b : md5.digest(source)) {           result.append(Integer.toHexString((b & 0xf0) >>> 4));           result.append(Integer.toHexString(b & 0x0f));         }         return result.toString();       } catch (Exception e) {         throw new RuntimeException(e.getMessage(), e);       }     } }