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
}
@utherbit
Copy link

utherbit commented Dec 2, 2023

Thanks

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