473,792 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

md5 hash not working?

Hi im trying to get a hash from a string and conert it back to a string

eg. "test" = "098f6bcd4621d3 73cade4e832627b 4f6"

my function:
Private Function GenerateHash(By Val SourceText As String) As String

Dim Ue As New UnicodeEncoding ()
Dim ByteSourceText( ) As Byte = Ue.GetBytes(Sou rceText)
Dim Md5 As New MD5CryptoServic eProvider()
Dim ByteHash() As Byte = Md5.ComputeHash (ByteSourceText )

Return Encoding.ASCII. GetString(ByteH ash)

End Function

when i call the function i get this:

"yAWeLsdBn1kOed fxt3S/5g=="
Nov 20 '05 #1
5 3854
Hi
So where is the problem?
What whould you espected to get?
MD5 will encrypt irreversible your string ..
Possible use is password hiding pourpose...to store a obfuscated value not a
readable password.

Ceers,
Crirus
--

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"HamuNaptra " <ha********@pan dora.be.NOSPAM> wrote in message
news:hy******** **********@phob os.telenet-ops.be...
Hi im trying to get a hash from a string and conert it back to a string

eg. "test" = "098f6bcd4621d3 73cade4e832627b 4f6"

my function:
Private Function GenerateHash(By Val SourceText As String) As String

Dim Ue As New UnicodeEncoding ()
Dim ByteSourceText( ) As Byte = Ue.GetBytes(Sou rceText)
Dim Md5 As New MD5CryptoServic eProvider()
Dim ByteHash() As Byte = Md5.ComputeHash (ByteSourceText )

Return Encoding.ASCII. GetString(ByteH ash)

End Function

when i call the function i get this:

"yAWeLsdBn1kOed fxt3S/5g=="

Nov 20 '05 #2
So where is the problem?
What whould you espected to get?


maybe try and read my post??

for short:
eg. "test" = "098f6bcd4621d3 73cade4e832627b 4f6" when i call the function i get this:

"yAWeLsdBn1kOed fxt3S/5g=="

Nov 20 '05 #3
"HamuNaptra " <ha********@pan dora.be.NOSPAM> wrote
Hi im trying to get a hash from a string and conert it back to a string
eg. "test" = "098f6bcd4621d3 73cade4e832627b 4f6"
my function:


Your code is wrong. First, you're using a unicode encoding to convert "test"
to a byte string; you should use an ASCII encoding or you will never get the
hash result you mention above.
Second, the hash above is in hexadecimal format and the result of your
function appears to be in Base64 format. Here's a small method [in C#] to
convert your bytes to a hexadecimal string:

private static string BytesToHex(byte[] hash) {
StringBuilder sb = new StringBuilder(h ash.Length * 2);
for(int i = 0; i < hash.Length; i++) {
sb.Append(hash[i].ToString("X2") );
}
return sb.ToString();
}

It shouldn't be too hard to port to VB.NET.

Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl
Nov 20 '05 #4
On Tue, 04 Nov 2003 13:09:33 GMT, HamuNaptra wrote:
Hi im trying to get a hash from a string and conert it back to a string

eg. "test" = "098f6bcd4621d3 73cade4e832627b 4f6"

my function:
Private Function GenerateHash(By Val SourceText As String) As String

Dim Ue As New UnicodeEncoding ()
Dim ByteSourceText( ) As Byte = Ue.GetBytes(Sou rceText)
Dim Md5 As New MD5CryptoServic eProvider()
Dim ByteHash() As Byte = Md5.ComputeHash (ByteSourceText )

Return Encoding.ASCII. GetString(ByteH ash)

End Function

when i call the function i get this:

"yAWeLsdBn1kOed fxt3S/5g=="


I modified your function as follows and got the result you expected:

Private Function GenerateHash(By Val SourceText As String) As String

'Dim Ue As New UnicodeEncoding
Dim ByteSourceText( ) As Byte = Encoding.Defaul t.GetBytes(Sour ceText)

'Dim ByteSourceText( ) As Byte = Ue.GetBytes(Sou rceText)
Dim Md5 As New MD5CryptoServic eProvider

Dim ByteHash() As Byte = Md5.ComputeHash (ByteSourceText )

'Convert the Byte array to a hex string
Dim sb As New StringBuilder(3 2)
For x As Integer = 0 To ByteHash.Length - 1
If ByteHash(x) < 16 Then
sb.Append("0")
End If
sb.Append(Conve rt.ToString(Byt eHash(x), 16))
Next

Return sb.ToString

End Function
--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #5

End Function


tnx
Nov 20 '05 #6

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

Similar topics

3
4177
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
22
4649
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last month I had enough of extra proof that the cat doesn't hount mice anymore in more and more situations. And the surrent sicretisme among array and hash is the base for it. I summarized all points in this article:...
2
3792
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
2
2528
by: Ravi | last post by:
Hi, I am working on a winform app. I need to use an object which can store some information(key/value pairs) and also can be acessed by multiple threads(read/write). From what I heard Hash table is best suited for it. My question is Why hash table. why can't we use an array. I thought hash table uses more memory than array. Correct me if am wrong.
10
2457
by: Qiangning Hong | last post by:
I'm writing a spider. I have millions of urls in a table (mysql) to check if a url has already been fetched. To check fast, I am considering to add a "hash" column in the table, make it a unique key, and use the following sql statement: insert ignore into urls (url, hash) values (newurl, hash_of_newurl) to add new url. I believe this will be faster than making the "url" column unique key and doing string comparation. Right?
12
12456
by: shaanxxx | last post by:
I wanted to write hash function float or double. Any suggestion would be appreciated.
3
2343
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
14240
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
7
1918
numberwhun
by: numberwhun | last post by:
Hello all! I am working on trying to figure out how to print out a hash of hashes. How the hash in the code is defined below is very similar to how the hash is defined in the script I am working with. (Yes, this is for work so I cannot share the actual code, sorry). I was using this web page as a reference for figuring this out as it has been of great assistance before, but for some reason, the code below prints out NOTHING. ...
0
9518
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
10430
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
10211
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...
1
10159
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9033
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
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();...
1
4111
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
3
2917
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.