计算SHA256散列的Andr​​oid / Java和C# [英] Compute SHA256 Hash in Android/Java and C#

查看:261
本文介绍了计算SHA256散列的Andr​​oid / Java和C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成的android一个SHA256散列,我再传递到一个ASP.NET Web API,Web服务和比较哈希那里。因此,我需要建立在Android的哈希,在ASP.NET提供的相同的输入会产生相当的哈希值。我拉我的头发试图找出什么我做错了。

下面是Android的code:

 公共字符串computeHash(字符串输入)抛出抛出:NoSuchAlgorithmException {
    消息摘要摘要= MessageDigest.getInstance(SHA-256);
    digest.reset();
    尝试{
      digest.update(input.getBytes(UTF-8));
    }赶上(UnsupportedEncodingException E){
      e.printStackTrace();
    }

    byte []的byteData = digest.digest(input.getBytes());
    StringBuffer的SB =新的StringBuffer();

    的for(int i = 0; I< byteData.length;我++){
      sb.append(Integer.toString((byteData [1]  - 安培; 0xff的)+ 0x100的,16).substring(1));
    }
    返回sb.toString();
}
 

而这里的code在服务器(C#):

 私有静态字符串ComputeHash(字符串输入,HashAlgorithm算法)
    {

        字节[] inputBytes = Encoding.UTF8.GetBytes(输入);
        字节[] hashedBytes = algorithm.ComputeHash(inputBytes);

        StringBuilder的SB =新的StringBuilder();

        的for(int i = 0; I< hashedBytes.Length;我++)
        {
            sb.Append(的String.Format({0:X2},hashedBytes [I]));
        }

        返回sb.ToString();
    }
 

更新: 以下是更正后的And​​r​​oid / Java实现(谢谢尼古拉Elenkov):

 公共字符串computeHash(字符串输入)抛出抛出:NoSuchAlgorithmException,UnsupportedEncodingException {
    消息摘要摘要= MessageDigest.getInstance(SHA-256);
    digest.reset();

    byte []的byteData = digest.digest(input.getBytes(UTF-8));
    StringBuffer的SB =新的StringBuffer();

    的for(int i = 0; I< byteData.length;我++){
      sb.append(Integer.toString((byteData [1]  - 安培; 0xff的)+ 0x100的,16).substring(1));
    }
    返回sb.toString();
}
 

解决方案

您的Java code是错误的:您要添加的输入字节的两倍。如果你正在计算这一气呵成,你需要或者只能拨打消化(字节)或呼叫消化()更新(字节);

I am trying to generate a SHA256 hash in android, that I then pass to an ASP.NET Web API web service and compare the hash there. As such, I need to construct a hash in Android, that given the same inputs in ASP.NET will generate an equivalent hash. I'm pulling my hair out trying to figure out what I'm doing wrong.

Here's the Android code:

public String computeHash(String input) throws NoSuchAlgorithmException{
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.reset();
    try{
      digest.update(input.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e){
      e.printStackTrace();
    }

    byte[] byteData = digest.digest(input.getBytes());
    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < byteData.length; i++){
      sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
    }
    return sb.toString();
}

AND HERE's THE CODE ON THE SERVER (c#):

    private static string ComputeHash(string input, HashAlgorithm algorithm)
    {

        Byte[] inputBytes = Encoding.UTF8.GetBytes(input);
        Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < hashedBytes.Length; i++)
        {
            sb.Append(String.Format("{0:x2}", hashedBytes[i]));
        }

        return sb.ToString();
    }

UPDATE: Here is the corrected Android/Java implementation (thank you Nikolay Elenkov):

public String computeHash(String input) throws NoSuchAlgorithmException, UnsupportedEncodingException{
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.reset();

    byte[] byteData = digest.digest(input.getBytes("UTF-8"));
    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < byteData.length; i++){
      sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
    }
    return sb.toString();
}

解决方案

Your Java code is wrong: you are adding the input bytes twice. If you are calculating this in one go, you need to either call only digest(bytes) or call digest() after update(bytes);

这篇关于计算SHA256散列的Andr​​oid / Java和C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆