Results 1 to 8 of 8

Thread: hash example

  1. #1

    hash example

    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

  2. #2

    Re: hash example

    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
    http://www.thinbasic.com

  3. #3

    Re: hash example

    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

  4. #4

    Re: hash example

    Thanks Sandy for the example

  5. #5

    Re: hash example

    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
    http://www.thinbasic.com

  6. #6
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,782
    Rep Power
    10

    Re: hash example

    As I said in another post, here we are talking about different things using the same name: hash.

    thinBasic Hash 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/Cryptog..._hash_function

    So, if you want make cryptographic, do not use thinBasic Hash function but functions inside Crypto module.

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  7. #7

    Re: hash example

    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

  8. #8
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,782
    Rep Power
    10

    Re: hash example

    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
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •