Md5 Function

Top  Previous  Next

Function:

Generates MD5 hash on the str string.

Syntax:

md5(byref str as string,byref input_hash as string,md5_mode as md5_modes,total_len as word) as string

Returns:

16-character hash string; an empty string when invalid str or input_hash argument was detected

See Also:

sha1

 


Part

Description

str

String containing (the next portion of) the input data to generate MD5 hash on. When md5_mode= 0- MD5_UPDATE, this string must be 64, 128, or 192 characters in length. Any other length will result in error and the function will return an empty string. When md5_mode= 1- MD5_FINISH, this string can have any length (up to 255 bytes).

input_hash

Hash obtained as a result of MD5 calculation on the previous data portion. Leave it empty for the first portion of data. Use the result of MD5 calculation on the previous data portion for the second and all subsequent portions of data. The result of MD5 is always 16 characters long, so passing the string of any other length (except 0 — see above) will result in error and this function will return an empty string.

md5_mode

0- MD5_UPDATE : Set this mode for all data portions except the last one.

1- MD5_FINISH: Set this mode for the last data portion; also use this selection if you only have a single data portion.

total_len

Total length of processed data (in all data portions combined). Only relevant when md5_mode= 1- MD5_FINISH. That is, only relevant for the last or a single data portion.

Details

MD5 is a standard method of calculating hash codes on data of any size. The amount of input data can often exceed maximum capacity of string variables (255 characters). The md5 method can be invoked repeatedly in order to process the data of any size (see the example below).

Examples

 

Dim s, hash As String

 

'simple calculation on a short string

s="Sting to calculate MD5 on"

hash=md5(s,"",MD5_FINISH,Len(s))

 

'calculation on the entire contents of data in the 'text.txt 'file

romfile.open("text.txt")

hash=""

s=romfile.getdata(192) 'use max portions

While Len(s)=192

  hash=md5(s,hash,MD5_UPDATE,0)

  s=romfile.getdata(192)

Wend

hash=md5(s,hash,MD5_FINISH,romfile.size) 'last portion for whatever unprocessed data is remaining in the file