49 lines
1.1 KiB
PHP
49 lines
1.1 KiB
PHP
|
#!/usr/bin/php
|
||
|
|
||
|
<?php
|
||
|
|
||
|
require_once __DIR__.'/../../include/const';
|
||
|
|
||
|
|
||
|
function cyclichash_new(){
|
||
|
|
||
|
/* [1] Fetch necessary data
|
||
|
=========================================================*/
|
||
|
/* (1) Fetch secret file */
|
||
|
$secret = @file_get_contents(SECRET_CONF);
|
||
|
|
||
|
/* (2) Check secret file format */
|
||
|
if( !is_string($secret) || !preg_match("/^(.{".SECRET_SIZE."}):(\d+)$/", $secret, $match) ){
|
||
|
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];
|
||
|
|
||
|
|
||
|
/* (4) Die if not token not changed */
|
||
|
if( $depth < 999 ){
|
||
|
slog("No new secret with $depth depth", 'cyclic-hash:new');
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
/* [2] If hash have just been created (original depth = 1000)
|
||
|
=========================================================*/
|
||
|
/* (1) Return new hash */
|
||
|
$newhash = $key;
|
||
|
|
||
|
/* (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();
|
||
|
?>
|