c#中的Java SHA1等价
本文关键字:等价 SHA1 Java 中的 | 更新日期: 2023-09-27 18:02:21
在Java Android应用程序中,我得到了以下代码来获取字符串的SHA1。我如何在c#中拥有等效的功能?
/**
* Utility method to convert an array of bytes to hex.
*
* @param data
* @return
*/
private static String convertToHex(byte[] data)
{
StringBuilder buf = new StringBuilder();
for (byte b : data)
{
int halfbyte = (b >>> 4) & 0x0F;
int two_halfs = 0;
do
{
buf.append((0 <= halfbyte) && (halfbyte <= 9) ? (char) ('0' + halfbyte) : (char) ('a' + (halfbyte - 10)));
halfbyte = b & 0x0F;
}
while (two_halfs++ < 1);
}
return buf.toString();
}
/**
* Hash a String using SHA1.
* @param text
* @return
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
*/
public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException
{
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(text.getBytes("iso-8859-1"), 0, text.length());
byte[] sha1hash = md.digest();
return convertToHex(sha1hash);
}
你可以在。net中使用SHA1类