Java MD5 newMD5HashUri(String value)

Here you can find the source of newMD5HashUri(String value)

Description

new MD Hash Uri

License

Apache License

Declaration

public final static String newMD5HashUri(String value) 

Method Source Code

//package com.java2s;
/**/*from w ww  .  j a v a 2  s. c  o  m*/
 * Copyright (C) 2010 University of Washington
 *
 * 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.
 */

import java.io.UnsupportedEncodingException;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public final static String newMD5HashUri(String value) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] asBytes;
            try {
                asBytes = value.getBytes("UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                throw new IllegalStateException("unexpected", e);
            }
            md.update(asBytes);

            byte[] messageDigest = md.digest();

            BigInteger number = new BigInteger(1, messageDigest);
            String md5 = number.toString(16);
            while (md5.length() < 32)
                md5 = "0" + md5;
            return "md5:" + md5;
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("Unexpected problem computing md5 hash", e);
        }
    }

    public final static String newMD5HashUri(byte[] asBytes) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(asBytes);

            byte[] messageDigest = md.digest();

            BigInteger number = new BigInteger(1, messageDigest);
            String md5 = number.toString(16);
            while (md5.length() < 32)
                md5 = "0" + md5;
            return "md5:" + md5;
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("Unexpected problem computing md5 hash", e);
        }
    }
}

Related

  1. md5Memcpy(byte[] output, byte[] input, int outpos, int inpos, int len)
  2. md5ToHex(byte[] hash)
  3. md5ToString(byte[] md5sum)
  4. md5Update(byte[] inbuf, int inputLen)
  5. newMd5()
  6. newMd5MessageDigest()
  7. validateMD5(String md5String)