Java Hash Code Calculate generateHash(final String data)

Here you can find the source of generateHash(final String data)

Description

generate Hash

License

Apache License

Declaration

public static String generateHash(final String data) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    private static final String SECRET_SALT = "geheim";

    public static String generateHash(final String data) {
        String result = null;//from   www  .j  a v a 2  s  .  c om
        StringBuffer toHash = new StringBuffer();
        toHash.append(data);
        toHash.append(SECRET_SALT);

        try {
            byte[] bytesOfMessage = toHash.toString().getBytes("UTF-8");
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] thedigest = md.digest(bytesOfMessage);
            result = new BigInteger(1, thedigest).toString(16);
            while (result.length() < 32) {
                result = "0" + result;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return result;
    }
}

Related

  1. combineHashesOld(int hash1, int hash2)
  2. combineHashesUnsorted(final int a, final int b)
  3. generateHash(byte[] data)
  4. generateHash(char[] password, byte[] salt)
  5. generateHash(File file)
  6. generateHash(final String input)
  7. generateHash(final String msg, final String hashAlgorithm)
  8. generateHash(String algo, byte[]... bytes)
  9. generateHash(String input)