逆向工程String.GetHashCode [英] Reverse Engineering String.GetHashCode

查看:93
本文介绍了逆向工程String.GetHashCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String.GetHashCode的行为取决于程序体系结构.因此它将在x86中返回一个值,在x64中返回一个值.我有一个必须在x86上运行的测试应用程序,它必须预测从必须在x64上运行的应用程序输出的哈希码.

String.GetHashCode's behavior is depend on the program architecture. So it will return one value in x86 and one value on x64. I have a test application which must run in x86 and it must predict the hash code output from an application which must run on x64.

下面是从mscorwks中反汇编String.GetHashCode实现.

Below is the disassembly of the String.GetHashCode implementation from mscorwks.

public override unsafe int GetHashCode()
{
      fixed (char* text1 = ((char*) this))
      {
            char* chPtr1 = text1;
            int num1 = 0x15051505;
            int num2 = num1;
            int* numPtr1 = (int*) chPtr1;
            for (int num3 = this.Length; num3 > 0; num3 -= 4)
            {
                  num1 = (((num1 << 5) + num1) + (num1 >≫ 0x1b)) ^ numPtr1[0];
                  if (num3 <= 2)
                  {
                        break;
                  }
                  num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr1[1];
                  numPtr1 += 2;
            }
            return (num1 + (num2 * 0x5d588b65));
      }
}

任何人都可以将此功能移植到安全的实现中吗?

Can anybody port this function to a safe implementation??

推荐答案

哈希代码不可在平台之间重复,甚至不能在同一系统上多次运行同一程序.您走错了路.如果您不改变路线,那么您的道路将会很困难,有一天可能会流下眼泪.

Hash codes are not intended to be repeatable across platforms, or even multiple runs of the same program on the same system. You are going the wrong way. If you don't change course, your path will be difficult and one day it may end in tears.

您要解决的 real 问题是什么?可以编写自己的哈希函数,作为扩展方法还是包装类的 GetHashCode 实现,并改用该哈希函数?

What is the real problem you want to solve? Would it be possible to write your own hash function, either as an extension method or as the GetHashCode implementation of a wrapper class and use that one instead?

这篇关于逆向工程String.GetHashCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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