2011-08-29 34 views
-1
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 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
*/ 

回答

3

你只是打印出byte[].toString这不是内容的散列

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

要显示散列作为文本,则应该字节数组为十六进制或Base64转换 - 有对堆栈溢出片段的负载来实现这一目标(例如,使用Apache Commons Codec)。如果你不需要散列作为文本,你可以将它保留为字节数组。

还要注意的是,你不应该使用这个代码:

byte[] bArray=password.getBytes() 

将使用系统默认的字符编码,它可以从系统而异,并且可能不能够编码所有的Unicode的。使用固定的编码,例如UTF-8,不管系统默认情况如何,这些编码将始终为相同的输入提供相同的结果,并且可以对所有的Unicode进行编码。

0

这里是一段代码片断我到MD5整个文件。它对我有用,当我MD5ed一个文件,我想发送,看看他们的客户端是否已经有相同的文件。如果需要完整的源可为什么还要颇费周折使用该文件的长度,而不是仅仅读,直到`read`调用返回-1找到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; 
} 
+0

?另外,为什么要创建一个新的空字符串而不是使用“”?另外,如果发生IOException异常,就像没有任何事情发生一样......并且如果没有经过检查的异常,那么您将让读者打开...(您应该使用finally块)。 –

+0

我从来没有说过它是好代码。我说这是代码工作:) 阅读while bytesLeft> 0是更确定性的。它可能会捕获/ dev/zero的边缘情况,但我没有尝试。 在Ruby中String.new和“”是相等的,我认为Java也是一样。 这些调用被封装在服务器/客户端的try catch上,如果我正确记得清理套接字。 是的,这不是最好的代码。 – EnabrenTane

+0

不,在Java中“”和“new String()”不一样 - 后者总是创建一个新对象,前者不会。 (无论如何,最好使用StringBuilder)。无论套接字是什么,由于缺少finally块,* file *句柄可能会在上面的代码中泄露 - 并且它只是在出错时停止而不是指示给调用者一些问题是非常讨厌的,IMO。 (我只是让这个方法抛出IOException。) –