如何生成所需的大小(例如8096)长位哈希代码 - c# [英] How to generate desired size (example 8096) long bit hash codes - c#

查看:259
本文介绍了如何生成所需的大小(例如8096)长位哈希代码 - c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多散列方法,但是我想用8096位长的字来组成位散列。是否有可能实现这一点?



例如,当我输入House时,我应该得到一个字符串:

 0101010001010101 ...(8096位)

我怎么能实现这一点(使用C#4.0是好的)?



如果您想知道为什么需要这样的东西,我需要它来比较Signature Files&向量空间模型。

解决方案

对于快速的非加密哈希,您可以检查 FNV家族。通过仔细和适当的变化,你应该能够构建一个相当快的8096位散列。

如果速度不是主要问题,但简单性和质量是你的可以简单地使用MD5的变体来创建一个非加密散列。

Hash(x)= MD5(0 || x)|| MD5(1 || x)... MD5(62 || x)|| MD5(63 || x)32,其中||是连接操作,只有最后一个散列的低32位被使用,会给你一个8096位散列。



编辑



下面是一个小代码示例,显示了MD5概念:

  using System; 
使用System.Security.Cryptography;
使用System.Text;

namespace Hash8096
{
class MainClass
{
public static byte [] H8096(byte [] x){
byte []结果=新字节[8096/8];
byte [] Xplus1 =新字节[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结果;


public static void Main(string [] args)
{
byte [] x = Encoding.UTF8.GetBytes(Hello World!);
byte [] MonsterHash = H8096(x);
Console.WriteLine(怪物哈希十六进制如下:);
Console.WriteLine(BitConverter.ToString(MonsterHash));
}
}
}


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.

解决方案

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));
        }
    }
}

这篇关于如何生成所需的大小(例如8096)长位哈希代码 - c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆