How to generate desired size (example 8096) long bit hash codes - c# [closed]

女生的网名这么多〃 提交于 2019-12-13 09:14:46

问题


There are many hashing methods but I want to compose bit hash with 8096 bits long. Is it possible to achieve this?

For example when I enter "House" I should get a string like:

"0101010001010101..." (8096 bits)

How can i achieve this (using C# 4.0 is ok)?

If you wonder why I need such thing, I need it for comparing Signature Files & Vector Space Model.


回答1:


For fast non-cryptographic hashes you can examine the FNV family. By careful and suitable variation you should be able to construct an 8096-bit hash that is reasonably fast.

If speed is not a primary concern but simplicity and quality are then you can simply use a variant of MD5 to make a non-cryptographic hash.

Hash(x) = MD5(0 || x) || MD5(1 || x) ... MD5(62 || x) || MD5(63 || x)<32>, where "||" is the concatenation operation and only the low order 32 bits of the final hash are used, will give you an 8096 bit hash.

EDIT

Here is small code sample showing the MD5 concept:

using System;
using System.Security.Cryptography;
using System.Text;

namespace Hash8096
{
    class MainClass
    {
        public static byte [] H8096(byte [] x) {
            byte [] Result = new byte[8096 / 8];
            byte [] Xplus1 = new byte[x.Length + 1];
            x.CopyTo(Xplus1, 1);
            int ResultOffset = 0;
            int AmountLeft = Result.Length;
            for (int i=0; i<64; i++) {
                // do MD5(i || x)
                var md5 = MD5.Create();
                Xplus1[0] = (byte) i;
                var hash = md5.ComputeHash(Xplus1);
                int NumToCopy = Math.Min(hash.Length, AmountLeft);
                Array.Copy(hash, 0, Result, ResultOffset,NumToCopy);
                ResultOffset += NumToCopy;
                AmountLeft -= NumToCopy;
            }
            return Result;
        }

        public static void Main (string[] args)
        {
            byte [] x = Encoding.UTF8.GetBytes("Hello World!");
            byte [] MonsterHash = H8096(x);
            Console.WriteLine ("Monster hash in hex follows:");
            Console.WriteLine(BitConverter.ToString(MonsterHash));
        }
    }
}


来源:https://stackoverflow.com/questions/14095943/how-to-generate-desired-size-example-8096-long-bit-hash-codes-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!