SHA1 返回不同的摘要

发布于 2024-12-01 18:33:05 字数 1074 浏览 3 评论 0原文

    import java.security.MessageDigest;
    class Enc{

            public String encryptPassword(String password) throws Exception{
                    byte[] bArray=password.getBytes();
                    MessageDigest md=MessageDigest.getInstance("SHA-1");
                    md.reset();
                    md.update(bArray);
                    byte[] encoded=md.digest();
                    System.out.println(encoded.toString());

                    return "";
            }
            public static void main(String args[]){
                    try{
                    Enc e=new Enc();
                    e.encryptPassword("secret");
                    }catch(Exception e){e.printStackTrace();}
            }
    }

/*

jabira-whosechild-lm.local 12:40:35 % while (true); do java Enc; done 
[B@77df38fd
[B@77df38fd
[B@60072ffb
[B@77df38fd
[B@6016a786
[B@60072ffb
[B@77df38fd
[B@77df38fd
[B@77df38fd
[B@77df38fd
[B@77df38fd
[B@77df38fd
[B@77df38fd
[B@6016a786
[B@6f507fb2
[B@77df38fd
[B@6016a786
[B@77df38fd
[B@77df38fd
[B@6016a786
*/
    import java.security.MessageDigest;
    class Enc{

            public String encryptPassword(String password) throws Exception{
                    byte[] bArray=password.getBytes();
                    MessageDigest md=MessageDigest.getInstance("SHA-1");
                    md.reset();
                    md.update(bArray);
                    byte[] encoded=md.digest();
                    System.out.println(encoded.toString());

                    return "";
            }
            public static void main(String args[]){
                    try{
                    Enc e=new Enc();
                    e.encryptPassword("secret");
                    }catch(Exception e){e.printStackTrace();}
            }
    }

/*

jabira-whosechild-lm.local 12:40:35 % while (true); do java Enc; done 
[B@77df38fd
[B@77df38fd
[B@60072ffb
[B@77df38fd
[B@6016a786
[B@60072ffb
[B@77df38fd
[B@77df38fd
[B@77df38fd
[B@77df38fd
[B@77df38fd
[B@77df38fd
[B@77df38fd
[B@6016a786
[B@6f507fb2
[B@77df38fd
[B@6016a786
[B@77df38fd
[B@77df38fd
[B@6016a786
*/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风为裳 2024-12-08 18:33:05

您只是打印出 byte[].toString ,这不是哈希的内容

System.out.println(encoded.toString());

要将哈希显示为文本,您应该将字节数组转换为十六进制或 base64 - Stack Overflow 上有大量片段可以实现此目的(例如使用 Apache Commons 编解码器)。如果您不需要将哈希值作为文本,则可以将其保留为字节数组。

另请注意,您不应该使用此代码:

byte[] bArray=password.getBytes()

它将使用系统默认字符编码,该编码可能因系统而异,并且可能无法对所有 Unicode 进行编码。使用固定编码(例如 UTF-8),无论系统默认设置如何,对于相同的输入,它始终会给出相同的结果,并且可以对所有 Unicode 进行编码。

You're just printing out byte[].toString which isn't the contents of the hash.

System.out.println(encoded.toString());

To display the hash as text, you should convert the byte array to hex or base64 - there are loads of snippets on Stack Overflow to accomplish that (e.g. using Apache Commons Codec). If you don't need the hash as text, you can just leave it as a byte array though.

Also note that you shouldn't use this code:

byte[] bArray=password.getBytes()

That will use the system default character encoding, which can vary from system to system, and may not be able to encode all of Unicode. Use a fixed encoding such as UTF-8, which will always give the same results for the same input regardless of system defaults, and which can encode all of Unicode.

画离情绘悲伤 2024-12-08 18:33:05

这是我对整个文件进行 MD5 的代码片段。当我对要发送的文件进行 MD5 处理以查看他们的客户是否已经拥有相同的文件时,它对我有用。如果需要,可以在 Github 上找到完整源代码< /a>

private static String getMD5Digest(File file) {
    BufferedInputStream reader = null;
    String hexDigest = new String();
    try {
        reader = new BufferedInputStream( new FileInputStream(file));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    byte[] buffer = new byte[4096];
    long fileLength = file.length();
    long bytesLeft = fileLength;
    int  read = 0;
    //Read our file into the md buffer
    while(bytesLeft > 0){
        try {
            read = reader.read(buffer,0, bytesLeft < buffer.length ? (int)bytesLeft : buffer.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        md.update(buffer,0,read);
        bytesLeft -= read;
    }
    byte[] digest = md.digest();
    for (int i = 0; i < digest.length;i++) {
        hexDigest += String.format("%02x" ,0xFF & digest[i]);
    }
    try {
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return hexDigest;
}

Here is a code snippet I to MD5 an entire file. It worked for me when I MD5ed a file I wanted to sent to see if they client already had the same file. Complete source if needed can be found here on Github

private static String getMD5Digest(File file) {
    BufferedInputStream reader = null;
    String hexDigest = new String();
    try {
        reader = new BufferedInputStream( new FileInputStream(file));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    byte[] buffer = new byte[4096];
    long fileLength = file.length();
    long bytesLeft = fileLength;
    int  read = 0;
    //Read our file into the md buffer
    while(bytesLeft > 0){
        try {
            read = reader.read(buffer,0, bytesLeft < buffer.length ? (int)bytesLeft : buffer.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        md.update(buffer,0,read);
        bytesLeft -= read;
    }
    byte[] digest = md.digest();
    for (int i = 0; i < digest.length;i++) {
        hexDigest += String.format("%02x" ,0xFF & digest[i]);
    }
    try {
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return hexDigest;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文