Skip to content

Instantly share code, notes, and snippets.

@recoilme
Created February 14, 2018 07:32
Show Gist options
  • Save recoilme/a1b9059b5d5f12c18a63bae58b3bc659 to your computer and use it in GitHub Desktop.
Save recoilme/a1b9059b5d5f12c18a63bae58b3bc659 to your computer and use it in GitHub Desktop.
Check Telegram Authorization in golang
// check telegram authorization on golang
//php version: https://gist.github.com/anonymous/6516521b1fb3b464534fbc30ea3573c2#file-check_authorization-php
//usage:
/*
func TestTg(t *testing.T) {
data := "id=1263310&first_name=Vadim&last_name=Kulibaba&username=recoilme&photo_url=https://t.me/i/userpic/320/recoilme.jpg&auth_date=1518535618&hash=1d7069137bf517a63261ee156919a057dca93a416118eebfd0d8f5697442cdce"
token := "YOUR:TOKEN"
if !checkTelegramAuthorization(data, token) {
t.Fail()
}
}*/
func checkTelegramAuthorization(data, token string) bool {
params, _ := url.ParseQuery(data)
strs := []string{}
var hash = ""
for k, v := range params {
if k == "hash" {
hash = v[0]
continue
}
strs = append(strs, k+"="+v[0])
}
sort.Strings(strs)
var imploded = ""
for _, s := range strs {
if imploded != "" {
imploded += "\n"
}
imploded += s
}
sha256hash := sha256.New()
io.WriteString(sha256hash, token)
hmachash := hmac.New(sha256.New, sha256hash.Sum(nil))
io.WriteString(hmachash, imploded)
ss := hex.EncodeToString(hmachash.Sum(nil))
return hash == ss
}
@Jamshid90
Copy link

Thank you a lot!

@apivid123
Copy link

ทำแบบไหน สอนที

@Donsdon
Copy link

Donsdon commented Jul 3, 2021

Thanks

@ipqhjjybj
Copy link

thank you

@utherbit
Copy link

utherbit commented Dec 2, 2023

Thanks

@Datswishty
Copy link

Datswishty commented Feb 20, 2025

For my usecase this library worked, check it out if you are stuck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment