Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
@shackra

This comment has been minimized.

Copy link

shackra commented Oct 2, 2019

Useful, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.