Java SHA sha(String text, String encoding)

Here you can find the source of sha(String text, String encoding)

Description

sha

License

Apache License

Declaration

public static String sha(String text, String encoding) 

Method Source Code


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

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

public class Main {
    public static String sha(String text, String encoding) {
        return encryptPassword(text, encoding, "sha");
    }/*from  www .  java2  s  .c  o  m*/

    private static String encryptPassword(String password, String encoding, String algorithm) {
        try {
            byte[] unencodedPassword = password.getBytes(encoding);
            return encryptPassword(unencodedPassword, algorithm);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    private static String encryptPassword(byte[] unencodedPassword, String algorithm) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance(algorithm);
        } catch (Exception e) {
            return null;
        }
        md.reset();
        md.update(unencodedPassword);
        byte[] encodedPassword = md.digest();
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < encodedPassword.length; i++) {
            if ((encodedPassword[i] & 0xff) < 0x10) {
                buf.append("0");
            }
            buf.append(Long.toString(encodedPassword[i] & 0xff, 16));
        }
        return buf.toString();
    }
}

Related

  1. sha(byte[] data)
  2. SHA(String s)
  3. sha(String source)
  4. sha(String str)
  5. sha(String strPlain)
  6. sha2(String content)
  7. sha2(String input)
  8. sha2(String password)
  9. sha2(String... data)