Skip to content

Instantly share code, notes, and snippets.

@greevex
Created August 5, 2022 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greevex/bfbe64d018731876f0934ff3a6438a01 to your computer and use it in GitHub Desktop.
Save greevex/bfbe64d018731876f0934ff3a6438a01 to your computer and use it in GitHub Desktop.
<?php
namespace mpcmf\apps\nfsu2\libraries\helpers;
/**
* Ea account password hash generator/checker
*/
class EAPassword
{
const EA_MASK_LENGTH = 32;
public static function encrypt($password, $rawMask, $hashedMask = false)
{
$currentByte = "\0";
$maskIndex = 0;
$mask = $hashedMask ? $rawMask : self::makeMask($rawMask);
$result = '';
foreach(str_split($password) as $char) {
$char = ord($char);
$char ^= $currentByte;
$currentByte = ((($char << 3) & 0xFF) | (($char >> 5) & 0xFF));
$currentByte ^= ord($mask[$maskIndex]);
if(!isset($mask[++$maskIndex])) {
$maskIndex = 0;
}
$result .= sprintf('%02x', $currentByte);
}
return $result;
}
public static function decrypt($string, $rawMask, $hashedMask = false)
{
$currentByte = "\0";
$maskIndex = 0;
$mask = $hashedMask ? $rawMask : self::makeMask($rawMask);
$result = '';
foreach(str_split($string, 2) as $char) {
$char = hexdec($char);
$charOrig = $char;
$char ^= ord($mask[$maskIndex]);
$char = ((($char << 5) & 0xFF) | (($char >> 3) & 0xFF));
$char ^= (int) $currentByte;
$currentByte = $charOrig;
if(!isset($mask[++$maskIndex])) {
$maskIndex = 0;
}
$result .= chr($char);
}
return $result;
}
public static function makeMask($something)
{
/** @noinspection SubStrUsedAsArrayAccessInspection */
$offset = (int)substr((string)$something, -1, 1);
$hash = hash('sha256', gethostname() . self::getSalt() . $something, false);
return strrev(substr($hash, $offset, self::EA_MASK_LENGTH));
}
protected static function getSalt()
{
$gitHeadCommitFile = APP_ROOT . '/.git/ORIG_HEAD';
/** @noinspection PhpUnusedLocalVariableInspection */
$composerLockFile = APP_ROOT . '/composer.lock';
if(file_exists($gitHeadCommitFile)) {
$salt = file_get_contents($gitHeadCommitFile);
} elseif(file_exists($composerLockFile)) {
$salt = md5_file($composerLockFile);
} else {
$salt = md5_file(APP_ROOT . '/bin/console');
}
return $salt;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment