sha1 Function

Function:

Generates SHA1 hash on the str string.

Syntax:

sha1(byref str as string,byref input_hash as string,sha1_mode as sha1_modes,total_len as word) as string

Returns:

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

See Also:

md5


Part

Description

str

String containing (the next portion of) the input data to generate SHA1 hash on. When sha1_mode = 0 — SHA1_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 sha1_mode = 1 — SHA1_FINISH, this string can have any length (up to 255 bytes).

input_hash

Hash obtained as a result of SHA1 calculation on the previous data portion. Leave it empty for the first portion of data. Use the result of the SHA1 calculation on the previous data portion for the second and all subsequent portions of data. The result of sha1 is always 20 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.

sha1_mode

0 — SHA1_UPDATE : Set this mode for all data portions except the last one.

1 — SHA1_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 sha1_mode = 1 — SHA1_FINISH. That is, only relevant for the last or a single data portion.


Details

SHA1 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 sha1 method can be invoked repeatedly in order to process the data of any size (see the example below).


Examples

Tibbo BASIC
Dim s, hash As String
 
'simple calculation on a short string
s="Sting to calculate SHA1 on"
hash=sha1(s,"",SHA1_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=sha1(s,hash,SHA1_UPDATE,0)
   s=romfile.getdata(192)
Wend
hash=sha1(s,hash,SHA1_FINISH,romfile.size) 'last portion for whatever unprocessed data is remaining in the file

sha1 Function

Details

Examples