'missing', 'seconds' => 0.0, ]; if ( ! is_string( $value ) || strlen( $value ) > 40 ) { return $none; } if ( 1 === preg_match( '/^js\.([0-9]{1,12})$/D', $value, $m ) ) { return [ 'format' => 'js', 'seconds' => (float) $m[1] / 1000, ]; } if ( 1 === preg_match( '/^([0-9]{1,12})\.([0-9a-f]{16})$/D', $value, $m ) ) { if ( ! hash_equals( self::mac( $form_id, $source, $m[1] ), $m[2] ) ) { return [ 'format' => 'forged', 'seconds' => 0.0, ]; } return [ 'format' => 'signed', 'seconds' => (float) max( 0, Clock::now() - (int) $m[1] ), ]; } return $none; } /** * MAC of a signed render time. * * @param string $form_id Effective formId. * @param string $source Source reference. * @param int|string $unix Unix time. * @return string 16 hex characters. */ private static function mac( $form_id, $source, $unix ) { return substr( hash_hmac( 'sha256', 't|' . $form_id . '|' . $source . '|' . $unix, Render_Token::key() ), 0, 16 ); } }