javascript - 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;
}
如果有人能指出我正确的方向,将不胜感激!
谢谢!
解决方案
代码几乎没有错误。
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();
}
推荐阅读
- python - 当列中两个连续的单元格值(字符串)相同时如何拆分数据框
- android - 为什么返回上一个Activity时会有onCreate调用?
- c# - 为私有变量设置这个公共是否有意义?
- android - 后端 API 调用的有效 FirebaseIdToken
- flutter-layout - Web 应用程序 ui 在特定的移动浏览器中工作异常
- node.js - 存储在 AWS s3 存储桶中的空文件
- smtp - Google OAuth API 验证的“验证要求例外”中的“单账户 SMTP 插件”是什么?
- python - 为堆叠的双向编码器-解码器 seq 添加波束解码器到 seq 模型
- amazon-web-services - 如何使用 CloudFormation 在指标过滤器中设置警报
- c# - 是否可以在 C# 中使用 Aspose.Word 在 Microsoft Word 文档中的特定位置插入图标