Java Digest digest(String strSrc, String encName)

Here you can find the source of digest(String strSrc, String encName)

Description

digest

License

Apache License

Declaration

public static String digest(String strSrc, String encName) 

Method Source Code


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

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

public class Main {
    private static final char[] hexArray = "0123456789ABCDEF".toCharArray();

    public static String digest(String strSrc, String encName) {
        MessageDigest md = null;//w ww  .java2  s  . c om
        String strDes = null;
        byte[] bt = strSrc.getBytes();
        try {
            if (encName == null || encName.equals("")) {
                encName = "MD5";
            }
            md = MessageDigest.getInstance(encName);
            md.update(bt);
            strDes = bytesToHexString(md.digest()); // to HexString
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
        return strDes;
    }

    public static String bytesToHexString(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
}

Related

  1. digest(String s, String algorithm)
  2. digest(String source)
  3. digest(String source, byte[] salt)
  4. digest(String source, String algorythm)
  5. digest(String strSource)
  6. digest(String text)
  7. digest(String text, String algorithm)
  8. digest(String token)
  9. digest(String type, byte[] bytes)