Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind temporary and the permanent authorization key #14

Closed
2012mjm opened this issue Oct 7, 2016 · 6 comments
Closed

Bind temporary and the permanent authorization key #14

2012mjm opened this issue Oct 7, 2016 · 6 comments

Comments

@2012mjm
Copy link
Contributor

2012mjm commented Oct 7, 2016

I have read here https://core.telegram.org/api/pfs and here https://core.telegram.org/method/auth.bindTempAuthKey for bind temporary and the permanent authorization key
I with respect to understanding the AuthKeyHandler class code wrote the following code

class AuthKeyHandler extends AckHandler
{
    ... 
    ... 
    ... 
    public function bind_temp_auth_key()
    {
        $nonce      = \phpseclib\Crypt\Random::string(16);
        $expires_at = time() + $this->settings['authorization']['auth_key']['expires_in'];

        $data = $this->tl->serialize_obj('bind_auth_key_inner',
            [
                'nonce'             => $nonce,
                'temp_auth_key_id'  => $this->settings['authorization']['temp_auth_key']['id'],
                'perm_auth_key_id'  => $this->settings['authorization']['auth_key']['id'],
                'temp_session_id'   => $this->settings['authorization']['session_id'],
                'expires_at'        => $expires_at,
            ]
        );

        $sha_digest         = sha1($data, true);
        $random_bytes       = \phpseclib\Crypt\Random::string(255 - strlen($data) - strlen($sha_digest));
        $to_encrypt         = $sha_digest.$data.$random_bytes;
        $encrypted_message  = $this->key->encrypt($to_encrypt);

        $result = $this->method_call('auth.bindTempAuthKey',
            [
                'perm_auth_key_id'  => $this->settings['authorization']['auth_key']['id'],
                'nonce'             => $nonce,
                'expires_at'        => $expires_at,
                'encrypted_message' => $encrypted_message,
            ]
        );

        var_dump($result);
    }
}

but the following error

Fatal error: Uncaught exception 'danog\MadelineProto\TL\Exception' with message 'serialize_param: given value isn't numeric'

because $nonce int128 type, but i want long type here

@2012mjm 2012mjm changed the title bind temporary and the permanent authorization key Bind temporary and the permanent authorization key Oct 7, 2016
@danog
Copy link
Owner

danog commented Oct 7, 2016

This isn't working because you're not passing a long to the nonce parameter, but a random string.
To turn it into a long you need to unpack it using the <q parameter of PHPStruct (see how I did it in the above functions).

@2012mjm
Copy link
Contributor Author

2012mjm commented Oct 7, 2016

Do you mean this following code

$nonce = \phpseclib\Crypt\Random::string(16);
$nonceToLong = $this->struct->unpack('<q', $nonce);
echo $nonceToLong;

Fatal error: Uncaught exception 'danog\PHP\StructException' with message 'Length of given data (16) is different from length calculated using format string (8).

@danog
Copy link
Owner

danog commented Oct 7, 2016

@2012mjm You need to generate a random string of 8 bytes to convert it to a long

@danog
Copy link
Owner

danog commented Oct 7, 2016

$nonceToLong = $this->struct->unpack('<q',  \phpseclib\Crypt\Random::string(8)); // Do not declare variables that are only going to be used once
echo $nonceToLong;

@2012mjm
Copy link
Contributor Author

2012mjm commented Oct 7, 2016

this is ok

@danog
Copy link
Owner

danog commented Nov 18, 2016

I made it work. Yay!

@danog danog closed this as completed Nov 18, 2016
danog added a commit that referenced this issue Oct 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants