Created cyclic-hash `decr` and `hash` functions

This commit is contained in:
xdrm-brackets 2017-01-26 19:55:53 +01:00
parent 09d252086b
commit 8d0e68803d
2 changed files with 106 additions and 0 deletions

73
lib/cyclic-hash/decr Executable file
View File

@ -0,0 +1,73 @@
#!/usr/bin/php
<?php
#define('SECRET_FILE', '/home/sats/satsd/conf/machine.secret');
define('SECRET_FILE', '/home/xdrm-brackets/Desktop/git.xdrm.io/logauth-sats/lib/cyclic-hash/secret');
define('SECRET_SIZE', 250);
/* [1] Function that generates a random secret
=========================================================*/
function generate_secret(){
/* (1) Generate random set */
$charlist = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';
/* (2) Set useful variables */
$clen = strlen($charlist);
$secret = '';
/* (3) Generate random characters one by one */
for( $i = 0 ; $i < SECRET_SIZE ; $i++ )
$secret .= $charlist[rand(0, $clen - 1)];
/* (4) Return the secret */
return $secret;
}
/* [2] Fetch necessary data
=========================================================*/
/* (1) Fetch secret file */
$secret = @file_get_contents(SECRET_FILE);
/* (2) Check secret file format */
if( !is_string($secret) || !preg_match("/^(.{250}):(\d+)$/", $secret, $match) ){
// Generate new secret
$secret = generate_secret().':1000';
// Try to override the secret file
@file_put_contents(SECRET_FILE, $secret) && die(0) || die(127);
}
/* (3) Extract data */
$key = (string) $match[1];
$depth = (int) $match[2];
/* [3] If can decrement, decrement
=========================================================*/
if( $depth > 1 ){
/* (1) Decrement the depth */
$depth--;
/* (2) Try to override the secret file */
@file_put_contents(SECRET_FILE, "$key:$depth") && die(0) || die(127);
/* [4] If cannot decrement, generate new password
=========================================================*/
}else{
// Generate new secret
$secret = generate_secret().':1000';
// Try to override the secret file
file_put_contents(SECRET_FILE, $secret) && die(0) || die(127);
}
?>

33
lib/cyclic-hash/hash Executable file
View File

@ -0,0 +1,33 @@
#!/usr/bin/php
<?php
#define('SECRET_FILE', '/home/sats/satsd/conf/machine.secret');
define('SECRET_FILE', '/home/xdrm-brackets/Desktop/git.xdrm.io/logauth-sats/lib/cyclic-hash/secret');
define('SECRET_SIZE', 250);
/* [1] Fetch necessary data
=========================================================*/
/* (1) Fetch secret file */
$secret = @file_get_contents(SECRET_FILE);
/* (2) Check secret file format */
if( !is_string($secret) || !preg_match("/^(.{".SECRET_SIZE."}):(\d+)$/", $secret, $match) )
die(127);
/* (3) Extract data for hashing from @secret */
$key = (string) $match[1];
$depth = (int) $match[2];
/* [2] Hash data
=========================================================*/
/* (1) Initialize with data */
$hash = $key;
/* (2) Hash @depth times */
for( $d = 0 ; $d < $depth ; $d++ )
$hash = hash('sha512', $hash);
echo $hash;
?>