SA-MP Forums Archive
A Small-C question, probably. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: A Small-C question, probably. (/showthread.php?tid=664107)



A Small-C question, probably. - [eLg]elite - 19.02.2019

Consider this code (I found somewhere years ago):

Код:
    stock udb_hash(buf[])
    {
        new length=strlen(buf);
        new s1 = 1;
        new s2 = 0;
        new n;
        for (n=0; n<length; n++)
        {
           s1 = (s1 + buf[n]) % 65521;
           s2 = (s2 + s1)     % 65521;
        }
        return (s2 << 16) + s1;
    }
Now this is my Java implementation:

Код:
	private double udb_hash(String pass)
	{
	    int length = pass.length();
	    int s1 = 1;
	    double s2 = 0;
	    double s3 = 0;
	    
	    for (int n = 0; n < length; n++)
	    {
	       s1 = (s1 + (int) pass.charAt(n)) % 65521;
	       s2 = (s2 + s1) % 65521;
	    }
	    
	    s3 = (Math.round(s2 << 16) + s1;  
	    return s3;
	}
Quora says it's impossible to use a bitwise operator on a double or floating-point variable; Pawn does it with ease. It works as long as I use Math.round to make the result an integer, but that doesn't work properly for hashing. Why is this?

EDIT: I don't know why I didn't realize that since Pawn uses dynamic types it converts the floating-point to a string (array of chars) and THEN shifts. My issue is solved.


Re: A Small-C question, probably. - KingHual - 19.02.2019

where did you get anything related to floating points from?


Re: A Small-C question, probably. - [eLg]elite - 19.02.2019

Pawn's types don't really matter; the result of the code would be the same if I took the route I did in a higher level language. It gives the same value in the end so the under-the-hood differences in the languages is important in understanding Pawn but I'm glad to understand why this works in both cases.

EDIT: Also I was confused by the differences between typeless and dynamic-typed languages.

What would be a better method for password hashing? I'm open to suggestions, I can't use this one anymore anyway because I asked for help publicly with it. I'm probably going to write a new one instead of an Alder-32 hashing method; I just need it to be reliable and secure that's all. I've never worked "close to the machine" with bits, so this is a newer step.


Re: A Small-C question, probably. - chickin - 19.02.2019

Quote:
Originally Posted by [eLg]elite
Посмотреть сообщение
Pawn's types don't really matter; the result of the code would be the same if I took the route I did in a higher level language. It gives the same value in the end so the under-the-hood differences in the languages is important in understanding Pawn but I'm glad to understand why this works in both cases.

EDIT: Also I was confused by the differences between typeless and dynamic-typed languages.

What would be a better method for password hashing? I'm open to suggestions, I can't use this one anymore anyway because I asked for help publicly with it. I'm probably going to write a new one instead of an Alder-32 hashing method; I just need it to be reliable and secure that's all. I've never worked "close to the machine" with bits, so this is a newer step.
Or you could just use
https://sampforum.blast.hk/showthread.php?tid=453544
or
https://sampforum.blast.hk/showthread.php?tid=570945


Re: A Small-C question, probably. - FedeA - 19.02.2019

Use BCrypt, i used this in the past, itsn`t hard.