2017-01-27 19:41:02 +00:00
|
|
|
#!/usr/bin/php
|
|
|
|
|
|
|
|
<?php
|
|
|
|
|
2017-01-30 09:39:45 +00:00
|
|
|
require_once __DIR__.'/../../include/php/const';
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
function cyclichash_new(){
|
|
|
|
|
|
|
|
/* [1] Fetch necessary data
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Fetch secret file */
|
|
|
|
$secret = @file_get_contents(SECRET_CONF);
|
|
|
|
|
|
|
|
/* (2) Check secret file format */
|
2017-01-28 17:58:49 +00:00
|
|
|
if( !is_string($secret) || !preg_match("/^(.{".SECRET_SIZE."}):(\d+):(.{".SECRET_SIZE."})$/", $secret, $match) ){
|
2017-01-27 19:41:02 +00:00
|
|
|
slog("Error while reading secret", 'cyclic-hash:new');
|
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Extract data for hashing from @secret */
|
|
|
|
$key = (string) $match[1];
|
|
|
|
$depth = (int) $match[2];
|
2017-01-28 17:58:49 +00:00
|
|
|
$next = (string) $match[3];
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (4) Die if not token not changed */
|
2017-01-28 17:58:49 +00:00
|
|
|
if( $depth > 1 ){
|
2017-01-27 19:41:02 +00:00
|
|
|
slog("No new secret with $depth depth", 'cyclic-hash:new');
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] If hash have just been created (original depth = 1000)
|
|
|
|
=========================================================*/
|
2017-01-28 17:58:49 +00:00
|
|
|
/* (1) Return new hash (from @next) */
|
|
|
|
$newhash = $next;
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
/* (2) Hash @depth times = 1000 */
|
|
|
|
for( $d = 0 ; $d < 1000 ; $d++ )
|
|
|
|
$newhash = hash('sha512', $newhash);
|
|
|
|
|
|
|
|
slog("New secret with $depth depth", 'cyclic-hash:new');
|
|
|
|
return $newhash;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
echo cyclichash_new();
|
|
|
|
?>
|