Skip to content

Instantly share code, notes, and snippets.

@jhonsore
Created March 18, 2016 13:29
Show Gist options
  • Save jhonsore/ecfba556278d70f0d957 to your computer and use it in GitHub Desktop.
Save jhonsore/ecfba556278d70f0d957 to your computer and use it in GitHub Desktop.
EncryptHash
<?php
//namespace utils;
class Hash {
//------------------------------------------------------------------------//
var $key = "e7NjchMCEGgTpsx3mKXbVPiAqn8DLzWo_6.tvwJQ-R0OUrSak954fd2FYyuH~1lIBZ";
public function __construct(/* ... */) {
//stuff goes here
//$args = func_get_args();
}
//------------------------------------------------------------------------//
public function encryptHash($string, $key) {
$returnString = "";
$charsArray = str_split("e7NjchMCEGgTpsx3mKXbVPiAqn8DLzWo_6.tvwJQ-R0OUrSak954fd2FYyuH~1lIBZ");
$charsLength = count($charsArray);
$stringArray = str_split($string);
$keyArray = str_split(md5($key));
$randomKeyArray = array();
while (count($randomKeyArray) < $charsLength) {
$randomKeyArray[] = $charsArray[rand(0, $charsLength - 1)];
}
for ($a = 0; $a < count($stringArray); $a++) {
$numeric = ord($stringArray[$a]) + ord($randomKeyArray[$a % $charsLength]);
$returnString .= $charsArray[floor($numeric / $charsLength)];
$returnString .= $charsArray[$numeric % $charsLength];
}
$randomKeyEnc = '';
for ($a = 0; $a < $charsLength; $a++) {
$numeric = ord($randomKeyArray[$a]) + ord($keyArray[$a % count($keyArray)]);
$randomKeyEnc .= $charsArray[floor($numeric / $charsLength)];
$randomKeyEnc .= $charsArray[$numeric % $charsLength];
}
return $randomKeyEnc . md5($string) . $returnString;
}
//------------------------------------------------------------------------//
public function decryptHash($string, $key) {
$returnString = "";
$charsArray = str_split("e7NjchMCEGgTpsx3mKXbVPiAqn8DLzWo_6.tvwJQ-R0OUrSak954fd2FYyuH~1lIBZ");
$charsLength = count($charsArray);
$keyArray = str_split(md5($key));
$stringArray = str_split(substr($string, ($charsLength * 2) + 32));
$md5 = substr($string, ($charsLength * 2), 32);
$randomKeyArray = str_split(substr($string, 0, $charsLength * 2));
$randomKeyDec = array();
for ($a = 0; $a < $charsLength * 2; $a+=2) {
$numeric = array_search($randomKeyArray[$a], $charsArray) * $charsLength;
$numeric += array_search($randomKeyArray[$a + 1], $charsArray);
$numeric -= ord($keyArray[floor($a / 2) % count($keyArray)]);
$randomKeyDec[] = chr($numeric);
}
for ($a = 0; $a < count($stringArray); $a+=2) {
$numeric = array_search($stringArray[$a], $charsArray) * $charsLength;
$numeric += array_search($stringArray[$a + 1], $charsArray);
$numeric -= ord($randomKeyDec[floor($a / 2) % $charsLength]);
$returnString .= chr($numeric);
}
if (md5($returnString) != $md5) {
return false;
} else {
return $returnString;
}
}
//------------------------------------------------------------------------//
}
//end Hash
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Force download</title>
</head>
<body>
<?php
require_once('Hash.php');
$hash = new Hash();
$encrypted = $hash->encryptHash('senha','key_senha');
echo $encrypted;
echo '<br><br><br>';
echo $hash->decryptHash($encrypted,'key_senha');
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment