帮忙转一下代码,从java到c#

3610 2016-12-19 03:58:55
帮忙转一下代码,从java到c#



//获取文件SHA1校验码
public static String getSha1Str(File file){
String str = "";
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = newbyte[2048];
int c;
while ((c = fis.read(buf)) > 0) {
baos.write(buf, 0, c);
}
fis.close();
str = SHA1(baos.toByteArray());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
return str;
}

//处理字节数组获取SHA1摘要
public static String SHA1(byte[] byteArray) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.update(byteArray);
byte messageDigest[] = digest.digest();
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString().toUpperCase();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return"";
}
...全文
138 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
3610 2017-05-13
  • 打赏
  • 举报
回复
没有解决,就这样把
  • 打赏
  • 举报
回复
看起来就是SHA1摘要啊,大致就这样
var buffer = File.ReadAllBytes(path);
SHA1CryptoServiceProvider cp = new SHA1CryptoServiceProvider();
return BitConverter.ToString(cp.ComputeHash(buffer)).Replace("-", "");

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧