473,434 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

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 8825
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*********@hotmail.com> wrote in message
news:ek*************@TK2MSFTNGP11.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****@games4dotnet.com> wrote in message
news:OL**************@TK2MSFTNGP09.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*********@hotmail.com> wrote in message
news:ek*************@TK2MSFTNGP11.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*********@hotmail.com> wrote in message
news:um**************@TK2MSFTNGP09.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\cryptography 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****@games4dotnet.com> wrote in message
news:OL**************@TK2MSFTNGP09.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*********@hotmail.com> wrote in message
news:ek*************@TK2MSFTNGP11.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*********@hotmail.com> wrote in message
news:um**************@TK2MSFTNGP09.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****@games4dotnet.com> wrote in message
news:OL**************@TK2MSFTNGP09.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*********@hotmail.com> wrote in message
news:ek*************@TK2MSFTNGP11.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\cryptography 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*********@hotmail.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.com>
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
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...
4
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
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...
2
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...
21
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...
13
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 ...
3
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...
139
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
by: pavanponnapalli | last post by:
hi , I have got a code as under : sub dbcall { my %hash; my $i=0; print "<<<<<<<@_>>>>>>> \n"; foreach(@_)
1
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.