Raised This Month: $81 Target: $400
 20% 

[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 , 07: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, 881 views)
__________________

Last edited by Peace-Maker; 02-12-2013 at 21:58. Reason: Fixed SHA1File compile errors
Peace-Maker is offline
kiki33hun
Veteran Member
Join Date: Jul 2011
Location: Magyarország
Old 11-26-2011 , 06: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 , 15: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 15:28.
pheadxdll is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 01-29-2013 , 08: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 , 14: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 15:44.
pheadxdll is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 01-29-2013 , 20:20   Re: [INC] SHA-1 hashing stocks
Reply With Quote #6

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
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 01-29-2013 , 16:55   Re: [INC] SHA-1 hashing stocks
Reply With Quote #7

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 16:56.
Powerlord is offline
AI_
Member
Join Date: Oct 2014
Location: Canada
Old 10-26-2016 , 05:30   Re: [INC] SHA-1 hashing stocks
Reply With Quote #8

This actually generates a small error in the hex string (without cascading) for a few of my files, which usually involves outputting fewer than 40 characters, I narrowed it down to the transcription part of the code:

PHP Code:
SHA1Finalize(Context[SHA1Context], String:output[], bool:bHex)
{
    if(
bHex)
    {
        for(new 
i=0;i<5;i++)
            
// Originally: Format(output, 41, "%s%x", output, Context[Message_Digest][i]);
            
Format(output41"%s%08x"outputContext[Message_Digest][i]);
... 
Attached Files
File Type: inc sha1.inc (8.4 KB, 206 views)

Last edited by AI_; 10-26-2016 at 05:32.
AI_ is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 10-27-2016 , 01:44   Re: [INC] SHA-1 hashing stocks
Reply With Quote #9

Thanks!
__________________
Peace-Maker is offline
ofir753
Senior Member
Join Date: Aug 2012
Old 10-28-2016 , 11:06   Re: [INC] SHA-1 hashing stocks
Reply With Quote #10

Good job!
ofir753 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 18:08.


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