473,769 Members | 4,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hash Code?

Dear All,

Is there a function in C# that will hash code a large byte array into an int
value?

I am searching for a hash function that will convert any size data(as for me
the input is a byte array of size 20) to a value range of my choosing.

If there is no such thing, would need some idea how to program this in C#.

In Java a byte array can be dump into a BigInteger object and then reduced
to a smaller value by dividing it. but this is a bad way and it is not like
real hashing...

Thanks

Joey
Nov 16 '05 #1
6 8846
System.Security .Cryptography then look at HashAlgorithm and it's derived
implementations .
What you'll note is that each hashing algorithm has output of different lengths,
so you'll have
to convert the smaller byte array hash value that you get into some other form
for storage.
I often turn hash values into hex strings for storage in the database.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Joseph Lee" <jo*********@ho tmail.com> wrote in message
news:ek******** *****@TK2MSFTNG P11.phx.gbl...
Dear All,

Is there a function in C# that will hash code a large byte array into an int
value?

I am searching for a hash function that will convert any size data(as for me
the input is a byte array of size 20) to a value range of my choosing.

If there is no such thing, would need some idea how to program this in C#.

In Java a byte array can be dump into a BigInteger object and then reduced
to a smaller value by dividing it. but this is a bad way and it is not like
real hashing...

Thanks

Joey

Nov 16 '05 #2
Thanks..

By the way, is it possible to set(overwrite?) the output size of the hash
algorithms?
example SHA-1 will give 20 bytes and cannot be changed and i might need only
12 bytes? so izzit possible

Thanks again

Joey

"Justin Rogers" <Ju****@games4d otnet.com> wrote in message
news:OL******** ******@TK2MSFTN GP09.phx.gbl...
System.Security .Cryptography then look at HashAlgorithm and it's derived
implementations .
What you'll note is that each hashing algorithm has output of different lengths, so you'll have
to convert the smaller byte array hash value that you get into some other form for storage.
I often turn hash values into hex strings for storage in the database.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Joseph Lee" <jo*********@ho tmail.com> wrote in message
news:ek******** *****@TK2MSFTNG P11.phx.gbl...
Dear All,

Is there a function in C# that will hash code a large byte array into an int value?

I am searching for a hash function that will convert any size data(as for me the input is a byte array of size 20) to a value range of my choosing.

If there is no such thing, would need some idea how to program this in C#.
In Java a byte array can be dump into a BigInteger object and then reduced to a smaller value by dividing it. but this is a bad way and it is not like real hashing...

Thanks

Joey


Nov 16 '05 #3

"Joseph Lee" <jo*********@ho tmail.com> wrote in message
news:um******** ******@TK2MSFTN GP09.phx.gbl...
Thanks..

By the way, is it possible to set(overwrite?) the output size of the hash
algorithms?
example SHA-1 will give 20 bytes and cannot be changed and i might need
only
12 bytes? so izzit possible
No, you should either use a hash provider that has the proper size(don't
think any are 12) or discard some of the bytes returned so that it gets down
to your size.

What exactly are you trying to achieve here, anyway? If its for duplicate
checking or for security\crypto graphy reasons then its probably best to keep
all the bytes, but if you are trying to convert it down simply for an
identifier you might be able to find a simple algorthim to do what you want
or safely use the TripleDes keyed hash which returns an 8 byte hash.
Thanks again

Joey

"Justin Rogers" <Ju****@games4d otnet.com> wrote in message
news:OL******** ******@TK2MSFTN GP09.phx.gbl...
System.Security .Cryptography then look at HashAlgorithm and it's derived
implementations .
What you'll note is that each hashing algorithm has output of different

lengths,
so you'll have
to convert the smaller byte array hash value that you get into some other

form
for storage.
I often turn hash values into hex strings for storage in the database.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Joseph Lee" <jo*********@ho tmail.com> wrote in message
news:ek******** *****@TK2MSFTNG P11.phx.gbl...
> Dear All,
>
> Is there a function in C# that will hash code a large byte array into
> an int > value?
>
> I am searching for a hash function that will convert any size data(as for me > the input is a byte array of size 20) to a value range of my choosing.
>
> If there is no such thing, would need some idea how to program this in C#. >
> In Java a byte array can be dump into a BigInteger object and then reduced > to a smaller value by dividing it. but this is a bad way and it is not like > real hashing...
>
> Thanks
>
> Joey
>
>



Nov 16 '05 #4
It's trivial to make your own hash algoritm, why didn't you just do it from
scratch? Than you can have whatever output you want.

--
Horatiu Ripa

"Joseph Lee" <jo*********@ho tmail.com> wrote in message
news:um******** ******@TK2MSFTN GP09.phx.gbl...
Thanks..

By the way, is it possible to set(overwrite?) the output size of the hash
algorithms?
example SHA-1 will give 20 bytes and cannot be changed and i might need only 12 bytes? so izzit possible

Thanks again

Joey

"Justin Rogers" <Ju****@games4d otnet.com> wrote in message
news:OL******** ******@TK2MSFTN GP09.phx.gbl...
System.Security .Cryptography then look at HashAlgorithm and it's derived
implementations .
What you'll note is that each hashing algorithm has output of different lengths,
so you'll have
to convert the smaller byte array hash value that you get into some other form
for storage.
I often turn hash values into hex strings for storage in the database.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Joseph Lee" <jo*********@ho tmail.com> wrote in message
news:ek******** *****@TK2MSFTNG P11.phx.gbl...
Dear All,

Is there a function in C# that will hash code a large byte array into
an int value?

I am searching for a hash function that will convert any size data(as for me the input is a byte array of size 20) to a value range of my choosing.

If there is no such thing, would need some idea how to program this in C#.
In Java a byte array can be dump into a BigInteger object and then reduced to a smaller value by dividing it. but this is a bad way and it is not like real hashing...

Thanks

Joey



Nov 16 '05 #5
Thanks for helping me :)
What exactly are you trying to achieve here, anyway? If its for duplicate
checking or for security\crypto graphy reasons then its probably best to keep all the bytes, but if you are trying to convert it down simply for an
identifier you might be able to find a simple algorthim to do what you want or safely use the TripleDes keyed hash which returns an 8 byte hash.


Before i contiunue, i would like to explain that i am trying to write a
research paper comparing different search method on encrypted data.
I have look at papers by Song, Wagner, Perring
http://citeseer.ist.psu.edu/song00practical.html
and others...

One of them i am looking at is by Eu Jin Goh
http://crypto.stanford.edu/~eujin/papers/secureindex/ -> page
http://crypto.stanford.edu/~eujin/pa...-encsearch.pdf ->
the slides

So I am trying to implement this method of searching on encrypted data

I will try to summarize and simplify about the method implementation
1>a keyword is hashed using a hash method, the author suggests HMAC-SHA1
2>the ouput value determines the location of an array where the value in
that location is changed to flag an entry.(the author does not define
anything about how the array is created in programming, just that it acts
like a database of single bit value 0 and 1 to mark as flagged or not)
3>this is done to every keywords there is filling up the array

So I am having a problem where the output value is a 20 bytes(HMAC-SHA1).
I don't think that an array that i define can be of that size?? am i wrong
here?

So i need a way to either
reduce the size of the output value 20 bytes to a smaller value that i can
define array[value] and flag the value in the array
or a way to represent a huge array for a value up to 20 bytes location
spaces 2^20*8

Any other way of implementation is greatly appreciated.


Nov 16 '05 #6
Joseph Lee <jo*********@ho tmail.com> wrote:

<snip>
I will try to summarize and simplify about the method implementation
1>a keyword is hashed using a hash method, the author suggests HMAC-SHA1
2>the ouput value determines the location of an array where the value in
that location is changed to flag an entry.(the author does not define
anything about how the array is created in programming, just that it acts
like a database of single bit value 0 and 1 to mark as flagged or not)
3>this is done to every keywords there is filling up the array

So I am having a problem where the output value is a 20 bytes(HMAC-SHA1).
I don't think that an array that i define can be of that size?? am i wrong
here?

So i need a way to either
reduce the size of the output value 20 bytes to a smaller value that i can
define array[value] and flag the value in the array
or a way to represent a huge array for a value up to 20 bytes location
spaces 2^20*8

Any other way of implementation is greatly appreciated.


Unless you're willing to use really large amounts of memory for this
array, I would suggest a different technique - the same technique used
in normal hashtables.

Break your sparse array into "buckets". The more entries you have, the
more buckets you have. Each bucket represents part of the array - each
bucket is usually the same size, and they're set at regular intervals.
Within each bucket, you have a straight list of entries. Within a
hashtable each entry would be the hash code, the key and the value.

When you are asked to look up a key (or a hashcode) you find the right
bucket (which is very quick, as it's basically just a division
operation) and then you walk down the list of entries in that bucket.
The more buckets you have, the faster it is to find an entry but the
more memory is taken.

I would suggest cutting the 20 byte hash down to 8 bytes to start with,
as you can then use a long to represent it in a simple way. You can
always keep the "full" hash in the list entries, if you want.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4176
by: Murali | last post by:
I have a requirement where I have to use two unsigned ints as a key in a STL hash map. A couple of ways to do this is 1. create a struct with two unsigned ints and use that as key (write my own HashFcn and EqualKey template args) or, 2. convert the two unsigned ints to char*s, concatenate them and use that as Key. For method 1, the difficulty I am having is in writing the HashFcn. HashFcn requires the following method
4
4255
by: Pelo GANDO | last post by:
Hi everybody ! I am a beginner in C++. I am looking for a (simple if it's possible) source code in C++ about hash table... Thank you ! Pelo
3
3340
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to substitute the set<Data*> with a hash_set<Data*>: typedef hash_set<const Data*, hash<const Data*>, eqData> DataPointerHashSet; // typedef set<Data*> DataPointerHashSet; // (see complete code below) But it doesn't work! Everything is fine...
2
3791
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently, Wang, Yu, and Lin showed a short- cut solution for finding collisions in SHA-1 . Their result
21
3223
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code (i.e. the get_hash function) is borrowed from various snippets I found on the net. Thee free function could probably need some love. I have been thinking about having a second linked list of all entries so that the cost of freeing is in proportion to...
13
2533
by: ababeel | last post by:
Hi I am using a calloc in a hash table program on an openvms system. The calloc is used to declare the hash table char **pHashes; pHashes = calloc(hash_size,sizeof(char *)); //hash_size = 101 and then later on when it is half full i do the following char **newHashes; if(newHashes)
3
2342
by: keithl | last post by:
Hi (again !) I posted a question yesterday just for info on hashrefs which I have read and it makes sense. Putting this into proactive though has now toally confused me ! I have an XML file sucked into a hash via XML::Simple. That's OK. I can see the data using Data::Dumper. I seem to have a Hash of Arrays, and those Arrays contain another hash. All I am trying to do is get to the data but every route I take I am getting myself into...
139
14225
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
1
1658
by: pavanponnapalli | last post by:
hi , I have got a code as under : sub dbcall { my %hash; my $i=0; print "<<<<<<<@_>>>>>>> \n"; foreach(@_)
1
2974
by: sixtyfootersdude | last post by:
Good Morning! I am a perl newbie and I think that I am struggling with references. I have an array of references to hashes which I am trying to print. This is what I have: for(my $i=0; $i<@input; $i++){ my $hash = $input; print "$i: \n";
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.