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]
*/
-1
A
回答
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;
}
相关问题
- 1. 在Node.js的计算SHA1返回不同的结果比在PHP
- 2. OpenId总是返回HMAC-SHA1
- 3. Java的MessageDigest SHA1算法返回的结果不是SHA1的函数php
- 4. 红宝石SHA1标志返回不同的结果比OpenSSL签名
- 5. 不同的SHA1但相同的SHA256
- 6. 如何将github回购中的文件转换为精华
- 7. HMAC-SHA1不返回所需散列的示例?
- 8. iOS'dateFromString'返回不同?
- 9. FileUpload.getFilename()返回的值不同
- 10. 返回不同的表格
- 11. spl_object_hash()返回不同的ID
- 12. Html.Hidden返回不同的值
- 13. getRGB返回不同的值
- 14. Date.getDay()返回不同的值
- 15. TextClock返回不同的值
- 16. toString返回不同的值
- 17. UserDefault返回不同的值
- 18. Netbeans不同的返回值
- 19. 返回不同的日期
- 20. 双精度值返回0
- 21. double.parse返回精度差
- 22. 短小精悍 - 返回INT
- 23. PHP HMAC-SHA1函数不返回“=”作为填充字符?
- 24. 错误:精华正在进行
- 25. 精华内容,包括与BeautifulSoup 4
- 26. sha1编码参数的精确值而不是变量值
- 27. 为不同角色返回不同siteMapNodes
- 28. 为什么node.js SHA1正文不同crom GitHub webhook SHA1值?
- 29. 使用shell脚本设置路径精确 - 使用shell精华
- 30. 返回不同类型
?另外,为什么要创建一个新的空字符串而不是使用“”?另外,如果发生IOException异常,就像没有任何事情发生一样......并且如果没有经过检查的异常,那么您将让读者打开...(您应该使用finally块)。 –
我从来没有说过它是好代码。我说这是代码工作:) 阅读while bytesLeft> 0是更确定性的。它可能会捕获/ dev/zero的边缘情况,但我没有尝试。 在Ruby中String.new和“”是相等的,我认为Java也是一样。 这些调用被封装在服务器/客户端的try catch上,如果我正确记得清理套接字。 是的,这不是最好的代码。 – EnabrenTane
不,在Java中“”和“new String()”不一样 - 后者总是创建一个新对象,前者不会。 (无论如何,最好使用StringBuilder)。无论套接字是什么,由于缺少finally块,* file *句柄可能会在上面的代码中泄露 - 并且它只是在出错时停止而不是指示给调用者一些问题是非常讨厌的,IMO。 (我只是让这个方法抛出IOException。) –