【问题标题】:Android Hash not workingAndroid哈希不起作用
【发布时间】:2012-04-29 19:03:02
【问题描述】:

我正在尝试使用 Sha-256 对 url 进行哈希处理,但遇到了一些问题。我已将字节转换为字符串,当我记录该字符串时,它显示不正确,而不是 32 个字符长的 rand 字符,它显示如下:

04-18 11:46:00.427: V/myApp(797): �C�rE�������.mm"7�{���"��Q]m

任何帮助将不胜感激

这是我的代码:

public void hash() throws NoSuchAlgorithmException, UnsupportedEncodingException{

        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(fixturesFeedURL.getBytes("UTF-8"));
        byte[] digest = md.digest();
        String strhash = new String(digest);
        Log.v("myApp", strhash);
    }   

【问题讨论】:

标签: android hash character-encoding sha256


【解决方案1】:

这是我为 MD5 做的:

MessageDigest md = MessageDigest.getInstance("MD5");
byte[] b = md.digest(input.getBytes());
StringBuffer output = new StringBuffer();
for (int i = 0; i < b.length; i++) {
  String tmpStr = "0" + Integer.toHexString((0xff & b[i]));
  output.append(tmpStr.substring(tmpStr.length() - 2));
}
return output.toString();

可能只是更改 MessageDigest 算法的问题...

【讨论】:

    【解决方案2】:

    您的问题是将字节数组转换为字符串 - 您可以使用它来执行此操作:

    http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Hex.html#encodeHex(byte[])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      相关资源
      最近更新 更多