Unique integer from string?
-
Is it possible to generate a unique number from a string of say 50-100 characters? Is that what a "hash" is? If so, how do you do it? NATHAN RIDLEY Web Application Developer email: nathan @ netlab.com.au [remove the spaces before and after the @ symbol]
-
Is it possible to generate a unique number from a string of say 50-100 characters? Is that what a "hash" is? If so, how do you do it? NATHAN RIDLEY Web Application Developer email: nathan @ netlab.com.au [remove the spaces before and after the @ symbol]
-
Is it possible to generate a unique number from a string of say 50-100 characters? Is that what a "hash" is? If so, how do you do it? NATHAN RIDLEY Web Application Developer email: nathan @ netlab.com.au [remove the spaces before and after the @ symbol]
While it's not guaranteed to be unique, it is mathematically impropable (not impossible) to get the same hash. You can generate a digest over your string like so:
public string Digest(string value)
{
if (value == null) throw new ArgumentNullException("value");
byte[] buffer = Encoding.Unicode.GetBytes(value); // Or whatever encoding
MD5 md5 = MD5.Create();
byte[] hash = md5.ComputeHash(buffer);
return ConvertToHex(hash);
}
public string ConvertToHex(byte[] buffer)
{
if (buffer == null) throw new ArgumentNullException();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < buffer.Length; i++)
sb.AppendFormat("{0:X2}", buffer[i]);
return sb.ToString();
}Microsoft MVP, Visual C# My Articles
-
Is it possible to generate a unique number from a string of say 50-100 characters? Is that what a "hash" is? If so, how do you do it? NATHAN RIDLEY Web Application Developer email: nathan @ netlab.com.au [remove the spaces before and after the @ symbol]
Hash function definition When people talk about hashing data, they're passing it through a hash function. Hash functions are one-way functions at least in the sense that data over an unconstrained range may be made to fit into a constrained range; however, it's sometimes possible to figure out possible input values from some output. MD5 is an often-used hashing algorithm. The single most popular use for hashing, in my experience, is to store passwords in a somewhat secure way. Many servers don't store a plain-text version of a password, or can be configured that way; instead, they store the hashed version, and when some client requests authentication, they take the passed password, hash it, and compare it to the hashed version. Regards, Jeff Varszegi EEEP!
-
Hash function definition When people talk about hashing data, they're passing it through a hash function. Hash functions are one-way functions at least in the sense that data over an unconstrained range may be made to fit into a constrained range; however, it's sometimes possible to figure out possible input values from some output. MD5 is an often-used hashing algorithm. The single most popular use for hashing, in my experience, is to store passwords in a somewhat secure way. Many servers don't store a plain-text version of a password, or can be configured that way; instead, they store the hashed version, and when some client requests authentication, they take the passed password, hash it, and compare it to the hashed version. Regards, Jeff Varszegi EEEP!
Thanks guys for the answers. Very helpful! :) NATHAN RIDLEY Web Application Developer email: nathan @ netlab.com.au [remove the spaces before and after the @ symbol]
-
Thanks guys for the answers. Very helpful! :) NATHAN RIDLEY Web Application Developer email: nathan @ netlab.com.au [remove the spaces before and after the @ symbol]
You're welcome! Regards, Jeff Varszegi EEEP!