AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [INC] SHA-1 hashing stocks (https://forums.alliedmods.net/showthread.php?t=172593)

Peace-Maker 11-23-2011 06:43

[INC] SHA-1 hashing stocks
 
1 Attachment(s)
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

kiki33hun 11-26-2011 05:36

Re: [INC] SHA-1 hashing stocks
 
Nice :)

pheadxdll 01-28-2013 14:25

Re: [INC] SHA-1 hashing stocks
 
:crab: This doesn't compile anymore :crab:

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..

Peace-Maker 01-29-2013 07:43

Re: [INC] SHA-1 hashing stocks
 
Quote:

Originally Posted by pheadxdll (Post 1882423)
:crab: This doesn't compile anymore :crab:

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.

pheadxdll 01-29-2013 13:58

Re: [INC] SHA-1 hashing stocks
 
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, :3

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

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

Powerlord 01-29-2013 15:55

Re: [INC] SHA-1 hashing stocks
 
Quote:

Originally Posted by pheadxdll (Post 1882423)
:crab: This doesn't compile anymore :crab:

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]); 


Peace-Maker 01-29-2013 19:20

Re: [INC] SHA-1 hashing stocks
 
Quote:

Originally Posted by pheadxdll (Post 1883032)
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, :3

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); 


pheadxdll 01-29-2013 20:39

Re: [INC] SHA-1 hashing stocks
 
The hash won't validate against php's sha1_file() method with that code.

Peace-Maker 02-12-2013 20:59

Re: [INC] SHA-1 hashing stocks
 
Quote:

Originally Posted by pheadxdll (Post 1883251)
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")

pheadxdll 02-13-2013 16:13

Re: [INC] SHA-1 hashing stocks
 
Thanks, works fast enough for my gamedata files. :)


All times are GMT -4. The time now is 13:20.

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