// Generate a hash for extra-security for all passwords
function generateHash ($plainText, $salt = '', $hash = TRUE) {
// Debug output
- //* DEBUG: */ debugOutput('plainText('.strlen($plainText).')=' . $plainText . ',salt('.strlen($salt).')=' . $salt . ',hash=' . intval($hash));
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'plainText('.strlen($plainText).')=' . $plainText . ',salt('.strlen($salt).')=' . $salt . ',hash=' . intval($hash));
// Is the required extension 'sql_patches' there and a salt is not given?
// 123 4 43 3 4 432 2 3 32 2 3 32 2 3 3 21
// Generate SHA1 sum from modula of number and the prime number
$sha1 = sha1(($a % getPrime()) . $server . getEncryptSeparator() . $keys . getEncryptSeparator() . $data . getEncryptSeparator() . getDateKey() . getEncryptSeparator() . $a);
- //* DEBUG: */ debugOutput('SHA1=' . $sha1.' ('.strlen($sha1).')<br />');
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SHA1=' . $sha1.' ('.strlen($sha1).')');
$sha1 = scrambleString($sha1);
- //* DEBUG: */ debugOutput('Scrambled=' . $sha1.' ('.strlen($sha1).')<br />');
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Scrambled=' . $sha1.' ('.strlen($sha1).')');
//* DEBUG: */ $sha1b = descrambleString($sha1);
- //* DEBUG: */ debugOutput('Descrambled=' . $sha1b.' ('.strlen($sha1b).')<br />');
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Descrambled=' . $sha1b.' ('.strlen($sha1b).')');
// Generate the password salt string
$salt = substr($sha1, 0, getSaltLength());
- //* DEBUG: */ debugOutput($salt.' ('.strlen($salt).')<br />');
+ //* DEBUG: */ debugOutput($salt.' ('.strlen($salt).')');
} else {
// Use given salt
- //* DEBUG: */ debugOutput('salt=' . $salt);
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'salt=' . $salt);
$salt = substr($salt, 0, getSaltLength());
- //* DEBUG: */ debugOutput('salt=' . $salt . '(' . strlen($salt) . '/' . getSaltLength() . ')<br />');
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'salt=' . $salt . '(' . strlen($salt) . '/' . getSaltLength() . ')');
// Sanity check on salt
if (strlen($salt) != getSaltLength()) {
$finalHash = $salt . sha1($salt . $plainText);
// Debug output
- //* DEBUG: */ debugOutput('finalHash('.strlen($finalHash).')=' . $finalHash);
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'finalHash('.strlen($finalHash).')=' . $finalHash);
// Return hash
return $finalHash;
//* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, strlen($passHash) . '/' . strlen(getSecretKey()));
if ((strlen($passHash) != 49) || (strlen(getSecretKey()) != 40)) {
// Both keys must have same length so return unencrypted
- //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, strlen($passHash) . '!=49/' . strlen(getSecretKey()) . '!=40');
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, strlen($passHash) . '!=49/' . strlen(getSecretKey()) . '!=40 - EXIT!');
return $ret;
} // END - if
$newHash = ''; $start = 9;
//* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'passHash=' . $passHash . '(' . strlen($passHash) . ')');
for ($idx = 0; $idx < 20; $idx++) {
- $part1 = hexdec(substr($passHash, ($idx * 2) + (strlen($passHash) - strlen(getSecretKey())), 2));
+ // Get hash parts and convert them (00-FF) to matching ASCII value (0-255)
+ $part1 = hexdec(substr($passHash , $start, 2));
$part2 = hexdec(substr(getSecretKey(), $start, 2));
- //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'part1=' . $part1 . '/part2=' . $part2);
+
+ // Default is hexadecimal of index if both are same
$mod = dechex($idx);
+ // Is part1 larger or part2 than its counter part?
if ($part1 > $part2) {
+ // part1 is larger
$mod = dechex(sqrt(($part1 - $part2) * getPrime() / pi()));
} elseif ($part2 > $part1) {
+ // part2 is larger
$mod = dechex(sqrt(($part2 - $part1) * getPrime() / pi()));
}
+
$mod = substr($mod, 0, 2);
- //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'part1=' . $part1 . '/part2=' . $part2 . '/mod=' . $mod . '(' . strlen($mod) . ')');
- $mod = str_repeat(0, (2 - strlen($mod))) . $mod;
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'idx=' . $idx . ',part1=' . $part1 . '/part2=' . $part2 . '/mod=' . $mod . '(' . strlen($mod) . ')');
+ $mod = str_repeat('0', (2 - strlen($mod))) . $mod;
//* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'mod(' . ($idx * 2) . ')=' . $mod . '*');
$start += 2;
$newHash .= $mod;
} // END - for
// Just copy it over, as the master salt is not really helpful here
- //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $passHash . ',' . $newHash . ' (' . strlen($newHash) . ')');
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $passHash . '(' . strlen($passHash) . '),' . $newHash . ' (' . strlen($newHash) . ')');
$ret = $newHash;
} // END - if