Java Hash Code Calculate generateHash(String target)

Here you can find the source of generateHash(String target)

Description

generate Hash

License

MIT License

Declaration

public static String generateHash(String target) 

Method Source Code

//package com.java2s;
/*// w w w  . j a va2 s.c  om
 * Protorabbit
 *
 * Copyright (c) 2009 Greg Murray (protorabbit.org)
 * 
 * Licensed under the MIT License:
 * 
 *  http://www.opensource.org/licenses/mit-license.php
 *
 */

import java.security.MessageDigest;

public class Main {
    public static String generateHash(String target) {
        if (target != null) {
            String hash = null;
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                byte[] bytes = md.digest(target.getBytes());
                // buffer to write the md5 hash to
                StringBuffer buff = new StringBuffer();
                for (int l = 0; l < bytes.length; l++) {
                    String hx = Integer.toHexString(0xFF & bytes[l]);
                    // make sure the hex string is correct if 1 character
                    if (hx.length() == 1) {
                        buff.append("0");
                    }
                    buff.append(hx);
                }
                hash = buff.toString().trim();
            } catch (java.security.NoSuchAlgorithmException e) {
            }
            return hash;
        }
        return null;
    }
}

Related

  1. generateHash(String input, String salt)
  2. generateHash(String item)
  3. generateHash(String plaintext)
  4. generateHash(String plainText, String hashType)
  5. generateHash(String serviceUri, String username, String password)
  6. generateHash(String tcString)
  7. generateHashSalt(int length)
  8. generateHashUUID(String digestData)
  9. hashCode(boolean b)