Skip to content

Instantly share code, notes, and snippets.

@PaulSonOfLars
Last active November 8, 2024 21:32
Show Gist options
  • Save PaulSonOfLars/5db2fcdde60033b366008d8523adda0b to your computer and use it in GitHub Desktop.
Save PaulSonOfLars/5db2fcdde60033b366008d8523adda0b to your computer and use it in GitHub Desktop.
Simple telegram invite link unhasher, to retrieve the link creator and group id.
import (
"bytes"
"encoding/base64"
"encoding/binary"
"fmt"
)
// hash is the hash at the end of an invite link -> t.me/joinchat/<hash>
func unpack_invitehash(hash string) (int, int, error) {
fmt.Println("hash:", hash)
s, err := base64.RawURLEncoding.DecodeString(hash)
if err != nil {
return 0, 0, err
}
if len(s) < 16 { // uint32 (4B) * 2 + uint64 (8B) -> 16B
return 0, 0, fmt.Errorf("invitelink too short")
}
fmt.Println("bytes:", s)
creator, err := readULong(s[:4])
if err != nil {
return 0, 0, err
}
fmt.Println("creator:", creator)
groupid, err := readULong(s[4:8])
if err != nil {
return 0, 0, err
}
fmt.Println("groupid:", groupid)
// this is useless and only used by tg to create "unique" links
rndnumber, err := readULongLong(s[8:16])
if err != nil {
return 0, 0, err
}
fmt.Println("rnd number:", rndnumber)
return creator, groupid, nil
}
func readULong(b []byte) (int, error) {
buf := bytes.NewBuffer(b)
var x uint32
err := binary.Read(buf, binary.BigEndian, &x)
if err != nil {
return 0, err
}
return int(x), nil
}
func readULongLong(b []byte) (int, error) {
buf := bytes.NewBuffer(b)
var x uint64
err := binary.Read(buf, binary.BigEndian, &x)
if err != nil {
return 0, err
}
return int(x), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment