Java MD5 String md5(String sInput)

Here you can find the source of md5(String sInput)

Description

Equivalent to (JavaSE): MessageDigest.getInstance("MD5").digest(sInput.getBytes()); Source: http://www.anavi.org/article/107/

License

Open Source License

Declaration

public static byte[] md5(String sInput) throws NoSuchAlgorithmException, DigestException 

Method Source Code


//package com.java2s;
/*/*from   w w w  .  ja v  a  2s. c om*/
 * Copyright (C) 2008 onwards University of Deusto
 * 
 * All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.
 * 
 * This software consists of contributions made by many individuals, 
 * listed below:
 *
 * Author: Aitor G?mez Goiri <aitor.gomez@deusto.es>
 */

import java.security.DigestException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**
     * Equivalent to (JavaSE):
     *       MessageDigest.getInstance("MD5").digest(sInput.getBytes());
     * 
     * Source: http://www.anavi.org/article/107/
     */
    public static byte[] md5(String sInput) throws NoSuchAlgorithmException, DigestException {
        //MD5 digest length is 128b (32 characters)
        int nDigestLen = 16;
        byte[] PlainText = sInput.getBytes();
        //Allocate memory for the encrypted text
        byte[] encryptedText = new byte[nDigestLen];
        MessageDigest Cipher;
        Cipher = MessageDigest.getInstance("MD5");
        Cipher.update(PlainText, 0, PlainText.length);
        Cipher.digest(encryptedText, 0, nDigestLen);
        //Convert result to hexidecimal
        return encryptedText;
    }
}

Related

  1. md5(String s)
  2. md5(String s)
  3. md5(String s)
  4. md5(String s)
  5. md5(String s)
  6. md5(String source)
  7. md5(String source)
  8. md5(String source)
  9. md5(String source)