A Small-C question, probably.
#1

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.
Reply
#2

where did you get anything related to floating points from?
Reply
#3

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.
Reply
#4

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
Reply
#5

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


Forum Jump:


Users browsing this thread: 3 Guest(s)