c# - C# 中的 Java SHA1 等效项

标签 c# java xamarin.ios sha1

在 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);
  }

最佳答案

有一个SHA1 .NET 中的类可供您使用。

关于c# - C# 中的 Java SHA1 等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15218885/

相关文章:

xamarin.ios - 在绑定(bind)项目中找不到 BaseTypeAttribute

ios - 为多个应用重用项目代码

c# - 调度方法调用 C#

c# - 同时调用基本构造函数和无参数构造函数?

java - 将带有单引号的字符串从 Java 插入到 Postgresql 中

Javafx 监听面板中的控制更改

c# - 在我的 C# 应用程序中嵌入 lua : require "luainterface" fails

c# - Return value, Ref, Out 有什么区别吗?

java - 编辑文本属性不显示日期

uitableview - Monotouch.Dialog : Sectioned UITableViews with alphabetic navigation