File size: 1,426 Bytes
bd894b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// This file is a part of EverythingSuckz/TG-FileStreamBot
// And is licenced under the Affero General Public License.
// Any distributions of this code MUST be accompanied by a copy of the AGPL
// with proper attribution to the original author(s).

package qrlogin

import (
	"bytes"
	"encoding/base64"
	"encoding/binary"
	"errors"
	"strings"

	"github.com/gotd/td/session"
)

func EncodeToPyrogramSession(data *session.Data, appID int32) (string, error) {
	buf := new(bytes.Buffer)
	if err := buf.WriteByte(byte(data.DC)); err != nil {
		return "", err
	}
	if err := binary.Write(buf, binary.BigEndian, appID); err != nil {
		return "", err
	}
	var testMode byte
	if data.Config.TestMode {
		testMode = 1
	}
	if err := buf.WriteByte(testMode); err != nil {
		return "", err
	}
	if len(data.AuthKey) != 256 {
		return "", errors.New("auth key must be 256 bytes long")
	}
	if _, err := buf.Write(data.AuthKey); err != nil {
		return "", err
	}
	if len(data.AuthKeyID) != 8 {
		return "", errors.New("auth key ID must be 8 bytes long")
	}
	if _, err := buf.Write(data.AuthKeyID); err != nil {
		return "", err
	}
	if err := buf.WriteByte(0); err != nil {
		return "", err
	}
	// Convert the bytes buffer to a base64 string
	encodedString := base64.URLEncoding.EncodeToString(buf.Bytes())
	trimmedEncoded := strings.TrimRight(encodedString, "=")
	return trimmedEncoded, nil
}