Md5 String : MD5.xml « Security « Android






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);
      }
    }

}

   
  








Related examples in the same category

1.Md5 sum
2.MD5 hash generator.
3.md5 sum and hmac Sha1 Digest
4.Create Md5 Checksum
5.MD5 Hash
6.MD5 String Util
7.Download from URL and MD5 check
8.Md5 Hash and MessageDigest