PDA

View Full Version : hash example



sandyrepope
24-07-2007, 19:59
Here is a hash example I made up so I could understand hash better:




dim username as string = "George W Bush" 'example user name
dim password as string = "MyPassword" 'example password
dim HashNumber as long 'holds returned hash number
dim MyHash as string 'holds username and password concatenated into one string
dim HashString as string
dim HashLoop as long 'for/next loop variable

MyHash = username + password 'make two strings into one

HashNumber = hash(1, MyHash) 'begin hashing using 1st algorithm, use 2 for second algorithm
'any other number always returns -2147483648 from somewhere
for HashLoop = 1 to 65000 'hash it for 65000 times
HashNumber = hash(1, HashNumber) 'hash number hashed by itself
next 'end of for/next loop


msgbox 0, "The hash number is " + HashNumber 'show the hash number


I hope it is clear enough. I've added comments to help explain what it is doing. I'm not very good at short comments.

Thanks
Sandy

RobertoBianchi
25-07-2007, 10:02
Sandy,

sorry but I don't uderstand the meaning of the loop.
I rearranged your script in order to make simple encryption/decryption using the a key created by the Hash() function.


uses "crypto"

Dim sPlainTxt as string = "This is the plain text that contains some U.S. president's secrets!" ' Example of test to encrypt
dim sUserName as string = "George W Bush" ' Example user name
dim sPassword as string = "MyPassword" ' Example password
dim sHash as string ' Hash string
dim sDarkTxt as string ' Encrypted string
dim sTxt as string ' Back to plain text

sHash = str$(hash(1, sUserName + sPassword)) ' Create a hash string

msgbox 0, "The hash string is" + sHash ' Show the hash string

sDarkTxt = iCrypto_DecryptRC4(sPlainTxt, sHash) ' Encrypt secret data

msgbox 0, "This is the encrypted (dark) text:" + $crlf + "<text>" + sDarkTxt + "</text>" 'Show the encrypted data

sTxt = iCrypto_DecryptRC4(sDarkTxt, sHash) ' Decrypt secret data

msgbox 0, "This is the decrypted (plain) text:" + $crlf + "<text>" + sTxt + "</text>" ' Show the decrypted data

' Lazy men error checking!
if sPlainTxt = sTxt then
msgbox 0, "Ok strigs are equals:"+ $crlf + "<text>" + sPlainTxt + "</text>" + $crlf + "<text>" + sTxt + "</text>"
else
MSGBOX 0, "Error!"
end if


Ciao,
Roberto

sandyrepope
25-07-2007, 15:55
The loop was there only because I read on the a site that the hash value would be more secure that way. I don't know for sure if that's true or not and the loop probably isn't really needed.

Thanks
Sandy

Michael Hartlef
25-07-2007, 17:55
Thanks Sandy for the example :)

RobertoBianchi
26-07-2007, 08:51
Sandy,

the following example show the difference between the hash obtained by the Hash() and iCrypto_MD5() functions.
Again, if you are play with cryptography stuff please use the iCrypto_MD5().


uses "crypto"

Dim sPlainTxt as string = "This is the plain text that contains some U.S. president's secrets!" ' Example of test to encrypt
dim sUserName as string = "George W Bush" ' Example user name
dim sPassword as string = "MyPassword" ' Example password
dim sHash as string ' Hash string
dim sDarkTxt as string ' Encrypted string
dim sTxt as string ' Back to plain text
DIM sMD5Hash as string

sHash = str$(hash(1, sUserName + sPassword)) ' Create a hash string
sMD5Hash = iCrypto_MD5(sUserName + sPassword)

msgbox 0, "The hash string is" + sHash + $crlf + "and MD5 hash is " + sMD5Hash ' Show the hash string

sDarkTxt = iCrypto_DecryptRC4(sPlainTxt, sMD5Hash) ' Encrypt secret data

msgbox 0, "This is the encrypted (dark) text:" + $crlf + "<text>" + sDarkTxt + "</text>" 'Show the encrypted data

sTxt = iCrypto_DecryptRC4(sDarkTxt, sMD5Hash) ' Decrypt secret data

msgbox 0, "This is the decrypted (plain) text:" + $crlf + "<text>" + sTxt + "</text>" ' Show the decrypted data

' Lazy men error checking!
if sPlainTxt = sTxt then
msgbox 0, "Ok strigs are equals:"+ $crlf + "<text>" + sPlainTxt + "</text>" + $crlf + "<text>" + sTxt + "</text>"
else
MSGBOX 0, "Error!"
end if


Ciao,
Roberto

ErosOlmi
26-07-2007, 13:10
As I said in another post, here we are talking about different things using the same name: hash.

thinBasic Hash (http://www.thinbasic.com/public/products/thinBasic/help/html/hash.htm) function is used to return "index" giving a string. Those index can be than used in arrays or more complex data structures as reference for that string. See some info here: http://en.wikipedia.org/wiki/Hash_function
In those cases it is better to indicate also the 3rd parameter of Hash function because this will limit the range of the returned value.

Roberto is talking about cryptographic hash that usually return complex strings giving as input the string buffer you want to crypt. More info: http://en.wikipedia.org/wiki/Cryptographic_hash_function

So, if you want make cryptographic, do not use thinBasic Hash function but functions inside Crypto (http://www.thinbasic.com/public/products/thinBasic/help/html/crypto.htm) module.

Ciao
Eros

sandyrepope
26-07-2007, 16:00
if you are play with cryptography stuff please use the iCrypto_MD5().

After reading all that everyone had to say I've decided to use iCripto_MD5() in my program.

Thanks to everyone who posted all that helpful information. I understand everything better now.

Thanks
Sandy

ErosOlmi
26-07-2007, 16:10
Testing HASH function I've found a problem.
If passed string is empty, HASH function produces a GPF.
Fix will be present in next release.

Regards
Eros