Skip to content

Instantly share code, notes, and snippets.

@cvhau
Created October 28, 2022 08:10
Show Gist options
  • Save cvhau/430a73dc061ecbdd2bfe256a134c48ef to your computer and use it in GitHub Desktop.
Save cvhau/430a73dc061ecbdd2bfe256a134c48ef to your computer and use it in GitHub Desktop.
WordPress Salts Generator
#!/usr/bin/env php
<?php
class WpSaltsGenerator
{
private static $instance = null;
public function __construct()
{
// code
}
public static function getInstance()
{
if (static::$instance === null) {
static::$instance = new static();
}
return static::$instance;
}
protected function getWpSecretKeyLines()
{
$saltUrl = 'https://api.wordpress.org/secret-key/1.1/salt';
$context = stream_context_create([
'http' => [
'method' => 'GET',
'follow_location' => 0,
],
]);
$saltLines = null;
$tries = 10;
while ($tries > 0) {
$saltLines = file($saltUrl, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES, $context);
if (is_array($saltLines) && !empty($saltLines)) {
break;
}
sleep(1);
$tries--;
}
if (empty($saltLines)) {
error_log('Failed to get secret keys from WordPress.org secret-key service. Using fallback keys!');
// code-spell-checker:disable
$saltLines = [
'define(\'AUTH_KEY\', \'Ny-Hid3)a,txte@1{>1*ZV!$~*bv!?qy|gdgI}m866_d3KUU3]9X(5{%p~1:vR<M\');',
'define(\'SECURE_AUTH_KEY\', \'!$++)e`-in%%BI8p85j$7s_s| gNK]W5DDY;nvc*2cN+7 V5u?_-w0i##)j[`]56\');',
'define(\'LOGGED_IN_KEY\', \'~7 z?b)9#Hi aSO*m+oH{SO}<bX]3#3 xEz-v|fHc&=GWK8A`^dWrbGlx]:i|q3B\');',
'define(\'NONCE_KEY\', \'TT_j[UrR.<pbph5X*_]YqxaQQ1_X_l1&@-x-+7kzZg)P]T8f++-j13aY0Izpm0Kc\');',
'define(\'AUTH_SALT\', \'le)+1%+4gfPWEznArm+S`gKMJ=QEMJN)Jh:_r6_&a=9^!4l(YH jtv_yi|E?LED`\');',
'define(\'SECURE_AUTH_SALT\', \'z^~Ysj0_;]MBT1$C-QDP?+eCu4W+~cd~YAoK?nu-Xj,/cbPx#KN8^|[6mUWZpsV5\');',
'define(\'LOGGED_IN_SALT\', \'|xft)SI6OLY*Y+fi.+^i+8n/3xW!#]A_?djgjVN99#+e4b>d]%0*COz$H,q]l-.]\');',
'define(\'NONCE_SALT\', \'_c@o,|<mo;|ta`70BJC~{Fsu~h;S$Rj!HMNakU.|#%Lh}v,45AL62sP,6VWy7Cx@\');',
];
// code-spell-checker:enable
}
return $saltLines;
}
public function generateSalts()
{
$saltLines = $this->getWpSecretKeyLines();
$saltKeyValues = [];
$saltKeyValuesBase64 = [];
foreach ($saltLines as $saltLine) {
if (preg_match("/^define\('([A-Z_]+)',\s+'(.+)'\);$/m", $saltLine, $matches)) {
$saltKeyValues[$matches[1]] = $matches[2];
}
}
if (!empty($saltKeyValues)) {
foreach ($saltKeyValues as $saltKey => $saltValue) {
$saltKeyValuesBase64[$saltKey] = base64_encode($saltValue);
}
}
echo "# PHP code" . PHP_EOL;
foreach ($saltLines as $saltLine) {
echo $saltLine . PHP_EOL;
}
echo PHP_EOL;
echo "# Base64 encoded" . PHP_EOL;
foreach ($saltKeyValuesBase64 as $saltKey => $saltValue) {
echo sprintf('%s \'%s\'', str_pad($saltKey, 17), $saltValue) . PHP_EOL;
}
}
}
WpSaltsGenerator::getInstance()->generateSalts();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment