From e80403d45d970908e6aa7258e8985ffb94efb43f Mon Sep 17 00:00:00 2001 From: Suvorov Daniil Date: Tue, 3 Sep 2019 15:09:25 +0300 Subject: [PATCH] Add Golang example in Readme --- README.md | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/README.md b/README.md index 0e4d555..301d159 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ * [PHP](#php) * [Java (1.8)](#java1p8) * [Python 3](#python3) +* [Golang](#go) @@ -144,3 +145,73 @@ status = is_valid(query=query_params, secret=client_secret) print("ok" if status else "fail") ``` + + + +## Пример проверки подписи на Golang + +```go +package main + +import ( + "crypto/hmac" + "crypto/sha256" + "encoding/base64" + "fmt" + "net/url" + "sort" + "strings" +) + +func isValid(link, clientSecret string) (bool, error) { + // Парсинг URL и query-параметров + u, err := url.Parse(link) + if err != nil { + return false, err + } + rawParams, err := url.ParseQuery(u.RawQuery) + if err != nil { + return false, err + } + if len(rawParams["sign"]) == 0 { + return false, fmt.Errorf("not found sign") + } + + // Фильтруем vk_ параметры + vkPrefix := make(url.Values) + + for key, values := range rawParams { + if strings.HasPrefix(key, "vk_") { + for _, value := range values { + vkPrefix.Add(key, value) + } + } + } + + // Генерируем хеш код + mac := hmac.New(sha256.New, []byte(clientSecret)) + _, _ = mac.Write([]byte(vkPrefix.Encode())) // Encode сортирует по ключу + expectedMAC := mac.Sum(nil) + + // Генерируем base64 + base64Sign := base64.StdEncoding.EncodeToString(expectedMAC) + base64Sign = strings.ReplaceAll(base64Sign, "+", "-") + base64Sign = strings.ReplaceAll(base64Sign, "/", "_") + base64Sign = strings.TrimRight(base64Sign, "=") + + // Сверяем и возвращаем + return base64Sign == rawParams["sign"][0], nil +} + +func main() { + status, err := isValid( + "https://example.com/?vk_user_id=494075&vk_app_id=6736218&vk_is_app_user=1&vk_are_notifications_enabled=1&vk_language=ru&vk_access_token_settings=&vk_platform=android&sign=exTIBPYTrAKDTHLLm2AwJkmcVcvFCzQUNyoa6wAjvW6k", + "wvl68m4dR1UpLrVRli", + ) + if err != nil { + panic(err) + } + + fmt.Println(status) +} +```