SHA256 on Android

1.代码:


public class SHA256Test {
/**
 * 对字符串加密,加密算法使用MD5,SHA-1,SHA-256,默认使用SHA-256
 *
 * @param strSrc
 *            要加密的字符串
 * @param encName
 *            加密类型
 * @return
 */
public static String Encrypt(String strSrc, String encName) {
    MessageDigest md = null;
    StringBuilder sb = new StringBuilder();

    byte[] bt = strSrc.getBytes();
    try {
        md = MessageDigest.getInstance(encName);
        byte[] result = md.digest(bt);
        for (byte b : result) {
            sb.append(String.format("%02x", b));
        }
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
    return sb.toString();
}


public static void main(String args[]){
    String s = SHA256Test.Encrypt("password", "SHA-256");
    System.out.println(s);

}

}

liunx中命令是 echo -n “password” | sha256sum

2.参考:
wiki
stackoverflow

你可能感兴趣的:(java,SHA256,andrdoi)