Java Hash Calculate getHash(String var)

Here you can find the source of getHash(String var)

Description

get Hash

License

Apache License

Declaration

public static String getHash(String var) 

Method Source Code


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

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

public class Main {
    public static String getHash(String var) {
        try {//from  w ww . j  a v  a2  s  .  com
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            byte[] varbytes = var.getBytes("UTF-8");
            byte[] result = md.digest(varbytes);
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < result.length; ++i) {
                sb.append(Integer.toHexString(0xFF & result[i]));
            }
            String hash = sb.toString();
            return hash;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. getHash(String s)
  2. getHash(String str, String algorithm)
  3. getHash(String strDate)
  4. getHash(String text)
  5. getHash(String valueToHash)
  6. getHashChars(byte[] bytes)
  7. getHashedPassword(String plainPassword)
  8. getHashFromString(String str)
  9. getHashInstance(final String alg)