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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package rpc
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. )
  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. func (r *NonceResponse) Valid(req *NonceRequest) error {
  23. if !bytes.Equal(r.Type, TagNonce) {
  24. return errors.New("unexpected RPC type")
  25. }
  26. if !bytes.Equal(r.Crypto, NonceCryptoAES) {
  27. return errors.New("unexpected crypto type")
  28. }
  29. if !bytes.Equal(r.KeySelector, req.KeySelector) {
  30. return errors.New("unexpected key selector")
  31. }
  32. return nil
  33. }
  34. // NewNonceResponse build new nonce response based on the given data.
  35. func NewNonceResponse(data []byte) (*NonceResponse, error) {
  36. if len(data) != 32 {
  37. return nil, fmt.Errorf("unexpected message length %d", len(data))
  38. }
  39. return &NonceResponse{
  40. NonceRequest: NonceRequest{
  41. KeySelector: data[4:8],
  42. CryptoTS: data[12:16],
  43. Nonce: data[16:],
  44. },
  45. Type: data[:4],
  46. Crypto: data[8:12],
  47. }, nil
  48. }