char [] - >字符串 - > bytes [] - >字符串......我迷路了

时间:2012-05-29 16:04:24

标签: java hash sha

我要做的是将键入jPasswordField的密码转换为SHA-256哈希。我在四处闲逛,发现如果我将密码保存为字符串但是我正在使用的字段返回char []所以我发现如何做到这一点所以我最后猜测该做什么...起初我已经有了不同的结果,即使密码是相同的,但现在我相信我更接近,因为它是一个常数;但它仍然不是

的输出
echo -n 'abc' | sha256sum

  

ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

虽然我的动作输出(对于相同的输入)是

  

86900f25bd2ee285bc6c22800cfb8f2c3411e45c9f53b3ba5a8017af9d6b6b05

我的行动如下:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        NoSuchAlgorithmException noSuchAlgorithmException = null;
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");

        } catch (NoSuchAlgorithmException ex) {
            noSuchAlgorithmException = ex;
        }
        if (noSuchAlgorithmException != null) {
            System.out.println(noSuchAlgorithmException.toString());
        }
        else {
            UnsupportedEncodingException unsupportedEncodingException = null;
            byte[] hash = null;
            char[] password = jPasswordField1.getPassword();
            StringBuffer stringBuffer = new StringBuffer();
            for (char c : password) {
                if (c > 0 && c < 16) {
                    stringBuffer.append("0");
                }
                stringBuffer.append(Integer.toHexString(c & 0xff));
            }
            String passwordString = stringBuffer.toString();
            try {
                hash = messageDigest.digest(passwordString.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException ex) {
                unsupportedEncodingException = ex;
            }
            if (unsupportedEncodingException != null) {
                System.out.println(unsupportedEncodingException.toString());
            }
            else {
                stringBuffer = new StringBuffer();
                for (byte b : hash) {
                    stringBuffer.append(String.format("%02x", b));
                }
                String passwordHashed = stringBuffer.toString();
                System.out.println(passwordHashed);
            }
        }

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

你几乎已经钉了它。只是采取了从char[]转换为String - &gt;的硬/错方式你只需要new String(password)。 (提示,如果你发现自己在字节和字符之间手动转换,你可能做错了。)

作为旁注,出于某种原因,“抛出”异常。这样就可以轻松跳过以下代码,抛出异常时不应该执行该代码。通过捕获异常并将其转换为“if”块,可以使代码更加复杂。

答案 1 :(得分:0)

这会打印出与sha256sum

相同的哈希值
public static void main(String[] args) 
    throws NoSuchAlgorithmException, UnsupportedEncodingException {

    char[] password = new char[]{'a', 'b', 'c'};

    MessageDigest messageDigest = null;
    messageDigest = MessageDigest.getInstance("SHA-256");

    byte[] hash = null;

    // This is how you convert a char array into a String without reencoding it into a different set of characters.
    String passwordString = new String(password);

    hash = messageDigest.digest(passwordString.getBytes("UTF-8"));
    StringBuilder sb = new StringBuilder();
    for (byte b : hash) {
        sb.append(String.format("%02x", b));
    }
    String passwordHashed = sb.toString();
    System.out.println(passwordHashed);
}