Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nonce_response.go 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package rpc
  2. import (
  3. "bytes"
  4. "github.com/juju/errors"
  5. )
  6. // NonceResponse is the data type which contains data of nonce response.
  7. type NonceResponse struct {
  8. NonceRequest
  9. Type []byte
  10. Crypto []byte
  11. }
  12. // Bytes returns serialized form of the nonce response.
  13. func (r *NonceResponse) Bytes() []byte {
  14. buf := &bytes.Buffer{}
  15. buf.Write(r.Type) // nolint: gosec
  16. buf.Write(r.KeySelector) // nolint: gosec
  17. buf.Write(r.Crypto) // nolint: gosec
  18. buf.Write(r.CryptoTS) // nolint: gosec
  19. buf.Write(r.Nonce) // nolint: gosec
  20. return buf.Bytes()
  21. }
  22. // Valid checks that nonce response compliments nonce request.
  23. func (r *NonceResponse) Valid(req *NonceRequest) error {
  24. if !bytes.Equal(r.Type, TagNonce) {
  25. return errors.New("Unexpected RPC type")
  26. }
  27. if !bytes.Equal(r.Crypto, NonceCryptoAES) {
  28. return errors.New("Unexpected crypto type")
  29. }
  30. if !bytes.Equal(r.KeySelector, req.KeySelector) {
  31. return errors.New("Unexpected key selector")
  32. }
  33. return nil
  34. }
  35. // NewNonceResponse build new nonce response based on the given data.
  36. func NewNonceResponse(data []byte) (*NonceResponse, error) {
  37. if len(data) != 32 {
  38. return nil, errors.New("Unexpected message length")
  39. }
  40. return &NonceResponse{
  41. NonceRequest: NonceRequest{
  42. KeySelector: data[4:8],
  43. CryptoTS: data[12:16],
  44. Nonce: data[16:],
  45. },
  46. Type: data[:4],
  47. Crypto: data[8:12],
  48. }, nil
  49. }