首页 > 解决方案 > Javascript哈希原型到C#

问题描述

我有一个为字符串创建哈希的 javascript string.prototype。JS:

    String.prototype.hashCode = function () {
        var hash = 5381, i = this.length
        while (i)
            hash = (hash * 33) ^ this.charCodeAt(--i)
        return hash >>> 0;
    }

我需要在 C# 中为使用相同数据库的另一个应用程序重新创建此哈希。以下是我到目前为止所拥有的...

    public string hashCode(string password)
    {
        var hash = 5381;
        int i;
        string newHash = "";
        int index = password.Length;
        for (i = 0; i > index; i++)
            hash = (hash * 33) ^ (char)password[--index];
        hash = (int)((uint)index >> 0);
        newHash += hash;
        return newHash;
    }

如果有人能指出我正确的方向,将不胜感激!

谢谢!

标签: javascriptc#

解决方案


代码几乎没有错误。

public string hashCode(string password)
{
     int hash = 5381;
     int i = password.Length;

     while(i > 0)
          hash = (hash * 33) ^ (char)password[--i];
     hash = (int)((uint)i >> 0);
     return hash.ToString();
 }

推荐阅读