Porting C code
HI Guys,
I need help on porting this code to java:
int mysql_encodePassword(char *szOut,char *szPassword,char *szSalt )
{
auto unsignedlong ulP1;
auto unsignedlong ulP2;
auto unsignedlong ulS1;
auto unsignedlong ulS2;
auto unsignedlong ulSeed1;
auto unsignedlong ulSeed2;
autoint i;
autodouble f;
autochar cExtra;
mysql_hash_password( szPassword, &ulP1, &ulP2 );
mysql_hash_password( szSalt, &ulS1, &ulS2 );
ulSeed1 = (ulP1 ^ ulS1) % 0x3FFFFFFF;
ulSeed2 = (ulP2 ^ ulS2) % 0x3FFFFFFF;
for( i=0; i<strlen(szSalt); i++ )
{
f = mysql_rand( &ulSeed1, &ulSeed2);
szOut[i] = (char)( floor(f * 31) + 64 );
}
f = mysql_rand( &ulSeed1, &ulSeed2 );
cExtra = (char)floor(f * 31);
for( i=0; i><strlen(szSalt); i++ )
{
szOut[i] = szOut[i] ^ cExtra;
}
szOut[i] ='\0';
return strlen(szOut);
}
///////////////////////////////////////
void mysql_hash_password(char *szPassword, unsignedlong *pulN1, unsignedlong *pulN2 )
{
auto unsignedlong ulN1;
auto unsignedlong ulN2;
autoint iAdd;
autochar *pIn;
auto unsignedchar c;
ulN1 = 1345345333;
ulN2 = 0x12345671;
iAdd = 7;
for( pIn=szPassword; *pIn; pIn++ )
{
c = *pIn;
if( (c ==' ') || (c =='\t') )
continue;
ulN1 = ulN1 ^ ( ( ( ulN1 & 63 ) + iAdd ) * c + (ulN1 ><< 8) );
ulN2 = ulN2 + ((ulN2 << 8) ^ ulN1);
iAdd = iAdd + c;
}
*pulN1 = ulN1 & 0x7FFFFFFF;
*pulN2 = ulN2 & 0x7FFFFFFF;
}
///////////////////////////////////////
double mysql_rand(unsignedlong *pulSeed1, unsignedlong *pulSeed2)
{
auto unsignedlong ulSeed1;
auto unsignedlong ulSeed2;
ulSeed1 = *pulSeed1;
ulSeed2 = *pulSeed2;
ulSeed1 = ( ( ulSeed1 * 3 ) + ulSeed2 ) % MYSQL_RAND_MAGIC;
ulSeed2 = ( ulSeed1 + ulSeed2 + 33 ) % MYSQL_RAND_MAGIC;
*pulSeed1 = ulSeed1;
*pulSeed2 = ulSeed2;
return ((double)ulSeed1) / ((double)MYSQL_RAND_MAGIC);
}
and this is what I've done so far:
privatebyte[] encryptPassword(byte[] pPassword,byte[] pSalt)
{
long[] hashPassword;
long[] hashSalt =newlong[2];
int hashKeyTmp = 0;
String hashKey =new String();
double f;
char cExtra;
int index;
hashPassword = mysqlHashPassword(pPassword);
hashSalt = mysqlHashPassword(pSalt);
hashPassword[0] = (hashPassword[0] ^ hashSalt[0]) % 0x3FFFFFFF;
hashPassword[1] = (hashPassword[1] ^ hashSalt[1]) % 0x3FFFFFFF;
for(index=0; index < pSalt.length; index++ )
{
f = mysqlRand(hashPassword[0], hashPassword[1]);
hashKeyTmp = (char)(Math.floor(f * 31) + 64 );
hashKey += hashKeyTmp;
}
f = mysqlRand(hashPassword[0], hashPassword[1] );
cExtra = (char)Math.floor(f * 31);
for(index = 0; index < pSalt.length; index++ )
{
hashKeyTmp = (hashKeyTmp ^ cExtra);
hashKey += hashKeyTmp;
}
hashKey +='\0';
return hashKey.getBytes();
}
////////////////////////////////////
privatelong[] mysqlHashPassword(byte[] pStrToHash)//, long[] hashPassword)
{
int iAdd = 7;
long[] hashedString =newlong[2];
hashedString[0] = 1345345333;
hashedString[1] = 0x12345671;
for(int index = 0; index<pStrToHash.length;index++)
{
if((pStrToHash[index] == (byte)' ') || (pStrToHash[index] == (byte)'\t'))
{
continue;
}
hashedString[0] = hashedString[0] ^ (((hashedString[0] & 63) + iAdd) * (char)pStrToHash[index] + (hashedString[0] ><< 8));
hashedString[1] = hashedString[1] + ((hashedString[1] << 8) ^ hashedString[1]);
iAdd = iAdd + (char)pStrToHash[index];
}
hashedString[0] = hashedString[0] & 0x7FFFFFFF;
hashedString[1] = hashedString[1] & 0x7FFFFFFF;
return hashedString;
}
////////////////////////////////////
privatedouble mysqlRand(long pulSeed1,long pulSeed2)
{
pulSeed1 = (( pulSeed1 * 3) + pulSeed2) % MYSQL_RAND_MAGIC;
pulSeed2 = (pulSeed1 + pulSeed2 + 33) % MYSQL_RAND_MAGIC;
return ((double)pulSeed1) / ((double)MYSQL_RAND_MAGIC);
}
Unfortunately it is not working properly... I am still debugging but I was never a true C developer....
Any help is welcome,
MeTitus

