Raised This Month: $12 Target: $400
 3% 

[INC] SHA-1 hashing stocks


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 11-23-2011 , 06:43   [INC] SHA-1 hashing stocks
Reply With Quote #1

This file provides 2 stock functions to calculate the SHA-1 hash for a given string or file.

SHA-1, the Secure Hashing Algorithm, is used by the Secure Hashing Standard as defined in FIPS PUB 180-1 published April 17, 1995.

Since it's very unlikely, that 2 messages will result in the same hash, SHA-1 can be used as a fingerprint for a message.

PHP Code:
/**
 * @brief            Produces a 160-bit message digest for a given data stream
 *
 * @param str        Input string to calculate the hash from.
 * @param output    Output buffer. If bHex is true, output must be at least 40+1 of length, if not it has to be 20+1.
 * @param bHex        If true, return the hash as hexadecimal number, else return a raw binary format.
 * @return          True, if hash was calculated, false otherwise
 */
stock bool:SHA1String(const String:str[], String:output[], bool:bHex=true)

/**
 * @brief            Produces a 160-bit message digest for a given file
 *
 * @param hFile        File handle returned by OpenFile(path, "r"); 
 * @param output    Output buffer. If bHex is true, output must be at least 40+1 of length, if not it has to be 20+1.
 * @param bHex        If true, return the hash as hexadecimal number, else return a raw binary format.
 * @return          True, if hash was calculated, false otherwise
 */
stock bool:SHA1File(Handle:hFileString:output[], bool:bHex=true
Changelog:
  • 13.02.2013: Fixed SHA1File compile errors
Attached Files
File Type: inc sha1.inc (8.4 KB, 890 views)
__________________

Last edited by Peace-Maker; 02-12-2013 at 20:58. Reason: Fixed SHA1File compile errors
Peace-Maker is offline
kiki33hun
Veteran Member
Join Date: Jul 2011
Location: Magyarország
Old 11-26-2011 , 05:36   Re: [INC] SHA-1 hashing stocks
Reply With Quote #2

Nice
__________________
kiki33hun is offline
pheadxdll
AlliedModders Donor
Join Date: Jun 2008
Old 01-28-2013 , 14:25   Re: [INC] SHA-1 hashing stocks
Reply With Quote #3

This doesn't compile anymore

Code:
    new input[1];
    while(!IsEndOfFile(hFile))
    {
        ReadFileCell(hFile, input[1])
        SHA1Input(Context, input, 1);
    }
ReadFileCell has changed- I tried to fix it briefly but I couldn't verify the computed hash..
__________________

Last edited by pheadxdll; 01-28-2013 at 14:28.
pheadxdll is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 01-29-2013 , 07:43   Re: [INC] SHA-1 hashing stocks
Reply With Quote #4

Quote:
Originally Posted by pheadxdll View Post
This doesn't compile anymore

Code:
    new input[1];
    while(!IsEndOfFile(hFile))
    {
        ReadFileCell(hFile, input[1])
        SHA1Input(Context, input, 1);
    }
ReadFileCell has changed- I tried to fix it briefly but I couldn't verify the computed hash..
It has changed?! Are you sure? Compiles fine here.
__________________
Peace-Maker is offline
pheadxdll
AlliedModders Donor
Join Date: Jun 2008
Old 01-29-2013 , 13:58   Re: [INC] SHA-1 hashing stocks
Reply With Quote #5

Edit: Sorry, I should have been more specific: I'm trying to call SHA1File..

SourcePawn Compiler 1.5.0-dev
C:\Users\ws\Documents\Sourcemod\sourcemod-1.5.0-hg3456-windows\addons\sourcemod\scripting\include\sh a1.inc(75) : error 032: array index out of bounds (variable "input")
Code:
new input[1];
ReadFileCell(hFile, input[1]) // ???
stock ReadFileCell(Handle:hndl, &data, size) 3 parameters vs. 2

Missing semicolon too,

What sourcemod compiler are you using? How did this compile?

I've tried a number of fixes but the hash is wrong
__________________

Last edited by pheadxdll; 01-29-2013 at 14:44.
pheadxdll is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 01-29-2013 , 15:55   Re: [INC] SHA-1 hashing stocks
Reply With Quote #6

Quote:
Originally Posted by pheadxdll View Post
This doesn't compile anymore

Code:
    new input[1];
    while(!IsEndOfFile(hFile))
    {
        ReadFileCell(hFile, input[1])
        SHA1Input(Context, input, 1);
    }
ReadFileCell has changed- I tried to fix it briefly but I couldn't verify the computed hash..
Why are you trying to access the second element ([1]) of a single element array? Or did you mean
PHP Code:
ReadFileCell(hFileinput[0]); 
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 01-29-2013 at 15:56.
Powerlord is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 01-29-2013 , 19:20   Re: [INC] SHA-1 hashing stocks
Reply With Quote #7

Quote:
Originally Posted by pheadxdll View Post
Edit: Sorry, I should have been more specific: I'm trying to call SHA1File..

SourcePawn Compiler 1.5.0-dev
C:\Users\ws\Documents\Sourcemod\sourcemod-1.5.0-hg3456-windows\addons\sourcemod\scripting\include\sh a1.inc(75) : error 032: array index out of bounds (variable "input")
Code:
new input[1];
ReadFileCell(hFile, input[1]) // ???
stock ReadFileCell(Handle:hndl, &data, size) 3 parameters vs. 2

Missing semicolon too,

What sourcemod compiler are you using? How did this compile?

I've tried a number of fixes but the hash is wrong
Oh, didn't try to hash a file ;)
That line should probably read
PHP Code:
ReadFileCell(hFileinput[0], 1); 
__________________
Peace-Maker is offline
pheadxdll
AlliedModders Donor
Join Date: Jun 2008
Old 01-29-2013 , 20:39   Re: [INC] SHA-1 hashing stocks
Reply With Quote #8

The hash won't validate against php's sha1_file() method with that code.
__________________
pheadxdll is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 02-12-2013 , 20:59   Re: [INC] SHA-1 hashing stocks
Reply With Quote #9

Quote:
Originally Posted by pheadxdll View Post
The hash won't validate against php's sha1_file() method with that code.
You're right. That's fixed now and produces (slowly) a valid sha1 hash for files opened with OpenFile(path, "rb")
__________________
Peace-Maker is offline
pheadxdll
AlliedModders Donor
Join Date: Jun 2008
Old 02-13-2013 , 16:13   Re: [INC] SHA-1 hashing stocks
Reply With Quote #10

Thanks, works fast enough for my gamedata files.
__________________
pheadxdll is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:42.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode