偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Golang做API開發(fā)離不開簽名驗證,如何設(shè)計 ?

開發(fā) 前端
在API開發(fā)中,簽名驗證是一種常見的安全措施,用于確保請求的完整性和來源的可靠性。以下是設(shè)計一個簽名驗證機(jī)制的步驟和示例代碼。

在API開發(fā)中,簽名驗證是一種常見的安全措施,用于確保請求的完整性和來源的可靠性。以下是設(shè)計一個簽名驗證機(jī)制的步驟和示例代碼。

設(shè)計思路

  1. 密鑰管理:為每個客戶端分配一個唯一的API密鑰和API密鑰。
  2. 簽名生成:客戶端在請求API時,使用預(yù)定義的算法生成簽名,并將簽名和其他必要參數(shù)(如時間戳、隨機(jī)數(shù)等)一起發(fā)送到服務(wù)器。
  3. 簽名驗證:服務(wù)器接收到請求后,根據(jù)相同的算法重新生成簽名,并與請求中的簽名進(jìn)行對比,如果匹配,則驗證通過。

簽名生成與驗證步驟

  1. 客戶端:
  • 生成時間戳和隨機(jī)數(shù)。
  • 將API密鑰、時間戳、隨機(jī)數(shù)、請求參數(shù)等按照預(yù)定義的順序拼接成字符串。
  • 使用API密鑰對字符串進(jìn)行哈希運算(如HMAC-SHA256)生成簽名。
  • 將簽名、時間戳、隨機(jī)數(shù)等信息作為請求參數(shù)發(fā)送到服務(wù)器。
  1. 服務(wù)器:
  • 從請求中提取簽名、時間戳、隨機(jī)數(shù)等信息。
  • 驗證時間戳是否在合理范圍內(nèi)(防止重放攻擊)。
  • 根據(jù)相同的算法重新生成簽名。
  • 對比服務(wù)器生成的簽名和請求中的簽名,如果匹配,則驗證通過。

示例代碼

以下是一個簡單的Go語言實現(xiàn),用于演示API簽名驗證。

客戶端代碼
package main

import (
    "crypto/hmac"
    "crypto/sha256"
    "encoding/hex"
    "fmt"
    "net/http"
    "net/url"
    "strconv"
    "time"
)

func generateSignature(apiSecret, apiKey, timestamp, nonce string, params url.Values) string {
    message := apiKey + timestamp + nonce + params.Encode()
    mac := hmac.New(sha256.New, []byte(apiSecret))
    mac.Write([]byte(message))
    signature := hex.EncodeToString(mac.Sum(nil))
    return signature
}

func main() {
    apiKey := "your_api_key"
    apiSecret := "your_api_secret"
    timestamp := strconv.FormatInt(time.Now().Unix(), 10)
    nonce := "random_nonce"

    params := url.Values{}
    params.Set("param1", "value1")
    params.Set("param2", "value2")

    signature := generateSignature(apiSecret, apiKey, timestamp, nonce, params)

    req, err := http.NewRequest("GET", "http://example.com/api", nil)
    if err != nil {
        fmt.Println("Error creating request:", err)
        return
    }

    query := req.URL.Query()
    query.Add("apiKey", apiKey)
    query.Add("timestamp", timestamp)
    query.Add("nonce", nonce)
    query.Add("signature", signature)
    req.URL.RawQuery = query.Encode()

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Error making request:", err)
        return
    }
    defer resp.Body.Close()

    fmt.Println("Response status:", resp.Status)
}
服務(wù)器端代碼
package main

import (
    "crypto/hmac"
    "crypto/sha256"
    "encoding/hex"
    "fmt"
    "net/http"
    "net/url"
    "strconv"
    "time"
)

const (
    apiKey    = "your_api_key"
    apiSecret = "your_api_secret"
)

func generateSignature(apiSecret, apiKey, timestamp, nonce string, params url.Values) string {
    message := apiKey + timestamp + nonce + params.Encode()
    mac := hmac.New(sha256.New, []byte(apiSecret))
    mac.Write([]byte(message))
    return hex.EncodeToString(mac.Sum(nil))
}

func validateSignature(r *http.Request) bool {
    apiKey := r.URL.Query().Get("apiKey")
    timestamp := r.URL.Query().Get("timestamp")
    nonce := r.URL.Query().Get("nonce")
    signature := r.URL.Query().Get("signature")

    if apiKey != apiKey {
        return false
    }

    timeInt, err := strconv.ParseInt(timestamp, 10, 64)
    if err != nil {
        return false
    }

    if time.Now().Unix()-timeInt > 300 {
        return false
    }

    params := r.URL.Query()
    params.Del("signature")

    expectedSignature := generateSignature(apiSecret, apiKey, timestamp, nonce, params)

    return hmac.Equal([]byte(signature), []byte(expectedSignature))
}

func handler(w http.ResponseWriter, r *http.Request) {
    if !validateSignature(r) {
        http.Error(w, "Invalid signature", http.StatusUnauthorized)
        return
    }

    fmt.Fprintf(w, "Request is authenticated")
}

func main() {
    http.HandleFunc("/api", handler)
    http.ListenAndServe(":8080", nil)
}

代碼說明

  • 客戶端:
  • generateSignature函數(shù)生成簽名。
  • 使用當(dāng)前時間戳和隨機(jī)數(shù)生成簽名,并將簽名和其他必要參數(shù)添加到請求中。
  • 服務(wù)器端:
  • generateSignature函數(shù)用于重新生成簽名。
  • validateSignature函數(shù)驗證請求中的簽名,包括檢查時間戳是否在合理范圍內(nèi),防止重放攻擊。
  • handler函數(shù)處理請求并驗證簽名,如果驗證通過,則返回成功響應(yīng)。

通過這種方式,API請求可以通過簽名驗證機(jī)制確保請求的完整性和來源的可靠性,有效防止重放攻擊和篡改。

責(zé)任編輯:武曉燕 來源: Go語言圈
相關(guān)推薦

2021-09-02 00:15:01

區(qū)塊鏈農(nóng)業(yè)技術(shù)

2015-10-13 10:41:39

大數(shù)據(jù)厚數(shù)據(jù)

2021-05-16 07:44:01

Hadoop大數(shù)據(jù)HDFS

2020-03-12 12:55:19

擴(kuò)展插件瀏覽器

2013-08-05 11:15:45

GoogleNexus系列

2011-04-29 10:53:35

投影幕

2021-09-03 08:44:51

內(nèi)核模塊Linux社區(qū)

2020-04-28 10:35:14

數(shù)據(jù)安全

2015-02-03 10:32:19

軟件定義存儲SDS混合云

2024-11-05 19:10:17

2025-01-09 08:01:10

2021-07-19 22:41:57

人工智能數(shù)據(jù)創(chuàng)業(yè)

2021-08-04 22:59:19

區(qū)塊鏈汽車技術(shù)

2016-05-03 15:12:35

數(shù)據(jù)科學(xué)

2016-09-06 17:21:00

APM聽云用戶體驗

2013-09-23 16:15:15

輕應(yīng)用超級App何小鵬

2017-04-05 13:30:16

機(jī)器學(xué)習(xí)開源行業(yè)發(fā)展

2012-04-20 12:34:08

iPad

2015-08-26 14:22:45

設(shè)計師HTML動畫工具
點贊
收藏

51CTO技術(shù)棧公眾號