请帮我解释一下函数

发布于 2024-12-09 08:34:02 字数 1271 浏览 3 评论 0原文

我的任务是记录一个没有注释的应用程序。但是,我一直无法理解以下功能。

private uint GetVersionHash(int encver, int realver)
{
    int EncryptedVersionNumber = encver;
    int VersionNumber = realver;
    int VersionHash = 0;
    int DecryptedVersionNumber = 0;
    string VersionNumberStr;
    int a = 0, b = 0, c = 0, d = 0, l = 0;

    VersionNumberStr = VersionNumber.ToString();

    l = VersionNumberStr.Length;

    // I am specifically struggling with the purpose and intent of this loop.

    for (int i = 0; i < l; i++)
    {
        VersionHash = (32 * VersionHash) + (int)VersionNumberStr[i] + ;
    }

    a = (VersionHash >> 24) & 0xFF;
    b = (VersionHash >> 16) & 0xFF;
    c = (VersionHash >> 8) & 0xFF;
    d = VersionHash & 0xFF;
    DecryptedVersionNumber = (0xff ^ a ^ b ^ c ^ d);

    if (EncryptedVersionNumber == DecryptedVersionNumber)
    {
        return Convert.ToUInt32(VersionHash);
    }
    else
    {
        return 0;
    }
}

根据我目前的理解,它右移 5 并增加了一些价值。

还有一些进一步的信息:

  • encver 似乎是一个加密版本,作为 int (它是从文件中读取的)
  • realver 似乎是我们正在测试匹配的版本。它在另一个函数中循环到此函数,从 short.MinValueshort.MaxValue

此循环的目的是什么?代码是如何达到这个目的的呢?

I was given the task of documenting an application that has no comments. However, I have been unable to understand the following function.

private uint GetVersionHash(int encver, int realver)
{
    int EncryptedVersionNumber = encver;
    int VersionNumber = realver;
    int VersionHash = 0;
    int DecryptedVersionNumber = 0;
    string VersionNumberStr;
    int a = 0, b = 0, c = 0, d = 0, l = 0;

    VersionNumberStr = VersionNumber.ToString();

    l = VersionNumberStr.Length;

    // I am specifically struggling with the purpose and intent of this loop.

    for (int i = 0; i < l; i++)
    {
        VersionHash = (32 * VersionHash) + (int)VersionNumberStr[i] + ;
    }

    a = (VersionHash >> 24) & 0xFF;
    b = (VersionHash >> 16) & 0xFF;
    c = (VersionHash >> 8) & 0xFF;
    d = VersionHash & 0xFF;
    DecryptedVersionNumber = (0xff ^ a ^ b ^ c ^ d);

    if (EncryptedVersionNumber == DecryptedVersionNumber)
    {
        return Convert.ToUInt32(VersionHash);
    }
    else
    {
        return 0;
    }
}

With my current understanding it bitshift 5 right and adds some value.

Also some further information:

  • encver appears to be an encrypted version as an int (it is read from a file)
  • realver seems to be version we are testing for match. It is looped in another function to this function from short.MinValue to short.MaxValue

What is the purpose of this loop? How does the code achieve this purpose?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的旅程 2024-12-16 08:34:02

该循环正在计算表示真实版本号的字符串中的字符(数字)的“哈希”。 VersionHash 的值是一个数字,取决于字符串中的每个字符、字符串的长度及其顺序。

The loop is calculating a 'hash' of the characters (digits) in the string representing the real version number. The value of VersionHash is a number which depends on every character in the string, the length of the string, and their order.

塔塔猫 2024-12-16 08:34:02

在我看来,for 循环只是自定义哈希函数的一部分 - 用于生成方法参数 int encver 的函数相同,因为该方法的本质是测试 encver 中的哈希是否与 realver 的重新哈希相匹配- 大概是为了某种完整性检查。

To me it looks like the for loop is just part of a custom hash function - the same function that was used to generate the method param int encver, as the essence of the method is to test that the hash in encver matches a rehash of realver - presumably for some kind of integrity check.

丶视觉 2024-12-16 08:34:02

我不会太担心这个例程中的逻辑。这是一个典型的国产加密系统,用于返回版本号的“散列”。我会在注释中概括该例程,指出该函数,而不会涉及太多细节。

for 循环通过获取字符串中字符的 (int) 值来生成哈希,然后执行相当于将其左移 4 个字节的操作。因此假设版本号的(int)值为:

1,2,3for

循环将生成VersionHash为:

000200030004

I wouldn't worry too much about the logic in this routine. This is a typical home grown encryption system to return a "hash" of the version number. I would blanket the routine in a comment that indicated the function without going into too much detail.

The for loop is generating a hash by getting the (int) value of a character in the string and then doing the equivalent of shifting it left by 4 bytes. So assuming that the (int) values of the version number are:

1, 2, 3

The for loop will generate a VersionHash of:

000200030004

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文