首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySql和C#散列在将它们转换为BIGINT时不匹配

MySql和C#散列在将它们转换为BIGINT时不匹配
EN

Stack Overflow用户
提问于 2017-04-20 20:54:46
回答 1查看 692关注 0票数 0

我的MySql表需要非加密散列,这样我就可以更快地查询数据。MySql数据库、哈希函数和C#应用程序必须为给定的值生成与它相同的散列,但前提是必须将其保持为字符串。我希望将它们转换为BIGINT,这样就可以避免字符串比较的开销。我知道Sha256是加密哈希函数,但至少MySql和C#为给定输入生成相同的哈希字符串,我不介意将其用于非加密用途。我尝试过其他在线可用的哈希算法,比如MurmurHash3 X86,但是使用了哈希冲突。任何帮助都将不胜感激。谢谢!

MySql查询:

代码语言:javascript
复制
SELECT SHA2('MyString', 256) AS Sha256, CONV(RIGHT(SHA2('MyString',256), 16), 16, 10) AS BIGINT_Sha256, MD5('MyString') AS MD_5, CONV(RIGHT(MD5('MyString'), 16), 16, 10) AS BIGINT_MD5;

C#代码:

代码语言:javascript
复制
static void Main(string[] args)
    {
        using (var sha256 = SHA256.Create())
        {
            var hashBytes = sha256.ComputeHash(Encoding.ASCII.GetBytes("MyString"));
            var hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();

            Console.WriteLine(hash);
        }

        using (var sha256 = SHA256.Create())
        {
            var hashBytes = sha256.ComputeHash(Encoding.ASCII.GetBytes("MyString"));
            var hash = BitConverter.ToInt64(hashBytes, 0);

            Console.WriteLine(hash);
        }

        using (var md5 = MD5.Create())
        {
            var hashBytes = md5.ComputeHash(Encoding.ASCII.GetBytes("MyString"));
            var hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();

            Console.WriteLine(hash);
        }

        using (var md5 = MD5.Create())
        {
            var hashBytes = md5.ComputeHash(Encoding.ASCII.GetBytes("MyString"));
            var hash = BitConverter.ToInt64(hashBytes, 0);

            Console.WriteLine(hash);
        }

        Console.ReadLine();
    }

MySql结果:

C#结果:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-21 19:31:33

这个answer帮助我解决了这个问题。

我没有使用右边的16个字符,而是在MySql侧将其更改为左侧。

MySql查询:

代码语言:javascript
复制
SET @Value = 'MyString';
SELECT 
  SHA2(@Value, 256) AS Sha256, 
  CAST(CONV(LEFT(SHA2(@Value,256), 16), 16, 10) AS INT) AS BIGINT_Sha256, 
  MD5(@Value) AS MD_5, 
  CAST(CONV(LEFT(MD5(@Value), 16), 16, 10) AS INT) AS BIGINT_MD5;

C#代码:

代码语言:javascript
复制
static void Main(string[] args)
    {
        var value = "MyString";

        using (var sha256 = SHA256.Create())
        {
            var hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(value));
            var hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();

            Console.WriteLine(hash);
        }

        using (var sha256 = SHA256.Create())
        {
            var hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(value));
            var hash = BitConverter.ToInt64(hashBytes.Take(8).ToArray(), 0);
            hash = IPAddress.HostToNetworkOrder(hash);

            Console.WriteLine(hash);
        }

        using (var md5 = MD5.Create())
        {
            var hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(value));
            var hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();

            Console.WriteLine(hash);
        }

        using (var md5 = MD5.Create())
        {
            var hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(value));
            var hash = BitConverter.ToInt64(hashBytes.Take(8).ToArray(), 0);
            hash = IPAddress.HostToNetworkOrder(hash);

            Console.WriteLine(hash);
        }

        Console.ReadLine();
    }

MySql结果:

C#结果:

我将测试它超过3000万记录和更新答案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43529736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档