SHA256哈希类似于.NET哈希。

huangapple go评论76阅读模式
英文:

SHA256 hash smillar to .Net hash

问题

以下是Android中的Java代码,用于计算与.NET版本相同的哈希值:

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

public class Main {
    public static void main(String[] args) {
        String str = "str";
        String outHash = "95e5f0b6988ec703e832172f70ce7dc7";

        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            byte[] challengeBytes = md.digest(str.getBytes(StandardCharsets.UTF_8));

            StringBuilder sb = new StringBuilder();
            for (byte b : challengeBytes) {
                sb.append(String.format("%02X", b));
            }

            String result = sb.toString();
            result = result.substring(0, result.length() - 2); // Remove the trailing "0A"
            result = result.replace("+", "-").replace("/", "_");

            System.out.println(result);
            System.out.println(result.equalsIgnoreCase(outHash));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这段Java代码将计算与.NET版本相同的哈希值。希望这对你有帮助!

英文:

I have am doing an Oauth App, where in few parts the hash is calculated via a .Net application, following is the .Net code to compute the hash

public static string GetBase64CodeChallange(string str)
{
    using (SHA256 sha256Hash = SHA256.Create())
    {
        byte[] challengeBytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(str));

        string s = Convert.ToBase64String(challengeBytes);
        s = s.Split('=')[0];
        s = s.Replace('+', '-');
        s = s.Replace('/', '_');
        return s;
    }
}

I want a similar version in android. I have R&D around the web and no solution yet of the same. Following the java code which is producing a different result. The android version string needs to be same as .Net version.

String str1 = "str";
            String outHash = "95e5f0b6988ec703e832172f70ce7dc7";
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                byte[] array = md.digest(str1.getBytes("UTF-8"));
                StringBuilder sb = new StringBuilder();
                for (byte b : array) {
                    sb.append(String.format("%02X", b));
                }
                System.out.println(sb.toString());
                System.out.println(sb.toString().equalsIgnoreCase(outHash));
            } catch (Exception e) {
                e.printStackTrace();
            }

Android - Java code

Please help

答案1

得分: 2

以下是翻译好的部分:

"因为你只需要做一件事情错了,就很难测试,而且哈希值会完全不同,因为它们是这样设计的。直到完全正确之前,你永远无法认为自己“越来越接近”。

确保在字节数组上使用相同的编码,使用相同的哈希算法(这里的.NET示例是SHA-256,而你的Java示例是MD5)"

英文:

It's hard to test because you only need to get one thing wrong, and the hash is completely different because they are designed that way. You never get to think "I'm getting closer" until it's exactly right.

Make sure you use the same encoding on the byteArray, use the same hashing algorithm (the .NET example here is SHA-256 and your Java one is MD5)

huangapple
  • 本文由 发表于 2020年7月31日 23:44:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63195014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定