私人端點的認證演算法是什麼?

上次更新時間: 2025年4月1日

每當 API 呼叫透過其中一種私人 API 方法存取帳戶時,都需要進行 API 認證。

例如,帳戶管理方法如 Balance、TradeBalance 和 OpenOrders;交易方法 AddOrder 和 CancelOrder;以及資金方法如 DepositAddresses 和 WithdrawInfo,都是需要 API 認證的私人 API 方法。

API 金鑰和加密簽名

API 認證基於公鑰/私鑰對(統稱為 API 金鑰)以及使用 SHA256 和 HMAC SHA512 等雜湊演算法的加密簽名。

API 金鑰範例將包含類似以下的公鑰和私鑰:

bash

Bash

Public Key: CJbfPw4tnbf/9en/ZmpewCTKEwmmzO18LXZcHQcu7HPLWre4l8+V9I3y
 
 Private Key: FRs+gtq09rR7OFtKj9BGhyOGS3u5vtY/EdiIBO9kD8NFtRX7w7LeJDSrX6cq1D8zmQmGkWFjksuhBvKOAWJohQ==

請注意,這些範例無法使用,因為它們未與任何 Kraken 帳戶關聯。

加密簽名由 API 用戶端程式碼即時建立。API 呼叫的資料會通過一系列雜湊和訊息認證演算法,這些演算法使用 API 密鑰(API 金鑰的私有部分)安全地簽署資料。

HTTP 標頭

API 金鑰和簽名透過自訂 HTTP 標頭 API-KeyAPI-Sign 提供給 API。

API 金鑰用於識別正在存取的帳戶,因此每個 API 呼叫的金鑰值都相同。加密簽名用於驗證 API 呼叫,並使用變數值(例如 nonce)計算,因此每個 API 呼叫的簽名值都不同。

API-Key 和 API-Sign HTTP 標頭的範例如下:

  • API-Key: CJbfPw4tnbf/9en/ZmpewCTKEwmmzO18LXZcHQcu7HPLWre4l8+V9I3y

  • API-Sign: RdQzoXRC83TPmbERpFj0XFVArq0Hfadm0eLolmXTuN2R24hzIqtAnF/f7vSfW1tGt7xQOn8bjm+Ht+X0KrMwlA==

請注意,API-Key 值與上面顯示的公鑰完全相同,但 API-Sign 值(簽名)與上面顯示的私鑰不同,並且每個 API 呼叫都會變為新值。

簽名偽代碼

我們的 API 文件指出 API-Sign 值是「使用 HMAC-SHA512 對 (URI 路徑 + SHA256(nonce + POST 資料)) 進行的訊息簽名,並使用 base64 解碼的秘密 API 金鑰」,這可以分為幾個不同的部分,如下所示:

變數

  • URI 路徑 = API 呼叫的 URL,不含「https://api.kraken.com」。

  • nonce = 必須隨每個 API 呼叫而增加值的唯一識別碼(通常是 UNIX 時間戳記,即自 1970 年 1 月 1 日以來的秒數(或毫秒數以獲得更高解析度))。

  • POST 資料 = nonce 和 API 方法參數的表單編碼名稱/值對。

呼叫 TradeBalance 方法的變數範例如下(請注意,無論值代表什麼,所有值都是字串值):

  • URI 路徑 = "/0/private/TradeBalance"

  • nonce = "1540973848000"

  • POST 資料 = "nonce=1540973848000&asset=xbt"

演算法

  • 計算 nonce 和 POST 資料的 SHA256。

  • 從 base64 解碼 API 密鑰(API 金鑰的私有部分)。

  • 使用 SHA512 作為 HMAC 雜湊,並使用解碼的 API 密鑰作為 HMAC 金鑰,計算 URI 路徑和 SHA256 的 HMAC。

  • 將 HMAC 編碼為 base64。

使用上面顯示的變數的演算法範例如下:

  • Base64Encode(HMAC-SHA512 of ("/0/private/TradeBalance" + SHA256("1540973848000nonce=1540973848000&asset=xbt")) using Base64Decode("FRs+gtq09rR7OFtKj9BGhyOGS3u5vtY/EdiIBO9kD8NFtRX7w7LeJDSrX6cq1D8zmQmGkWFjksuhBvKOAWJohQ==") as the HMAC key

結果是 API-Sign 值。

範例代碼

以下是為 TradeBalance API 端點生成 API 認證簽名的最小(無錯誤檢查)但功能齊全的範例:

python

Python

# Import required Python libraries
import time
import base64
import hashlib
import hmac

# Decode API private key from base64 format displayed in account management
api_secret = base64.b64decode(
    "FRs+gtq09rR7OFtKj9BGhyOGS3u5vtY/EdiIBO9kD8NFtRX7w7LeJDSrX6cq1D8zmQmGkWFjksuhBvKOAWJohQ=="
)

# Variables (API endpoint, nonce and HTTP POST data)
api_path = "/0/private/TradeBalance"
api_nonce = str(int(time.time() * 1000))
api_post = "nonce=" + api_nonce + "&asset=xbt"

# Cryptographic hash algorithms
api_sha256 = hashlib.sha256(api_nonce.encode('utf-8') + api_post.encode('utf-8'))
api_hmac = hmac.new(
    api_secret,
    api_path.encode('utf-8') + api_sha256.digest(),
    hashlib.sha512
)

# Encode signature into base64 format used in API-Sign value
api_signature = base64.b64encode(api_hmac.digest())

# API authentication signature for use in API-Sign HTTP header
print(api_signature.decode())


NodeJS

bash

Bash

// Import required NodeJS libraries
const crypto = require('crypto');

// Decode API private key from base64 format displayed in account management
const apiSecret = Buffer.from(
  'FRs+gtq09rR7OFtKj9BGhyOGS3u5vtY/EdiIBO9kD8NFtRX7w7LeJDSrX6cq1D8zmQmGkWFjksuhBvKOAWJohQ==',
  'base64'
);

// Variables (API endpoint, nonce and HTTP POST data)
const apiPath = '/0/private/TradeBalance';
const apiNonce = Date.now().toString();
const apiPost = `nonce=${apiNonce}&asset=xbt`;

// Cryptographic hash algorithms
const apiSha256 = crypto.createHash('sha256').update(`${apiNonce}${apiPost}`).digest();
const apiSha512 = crypto.createHmac('sha512', apiSecret).update(apiPath).update(apiSha256).digest();

// Encode signature into base64 format used in API-Sign value
const apiSignature = apiSha512.toString('base64');

// API authentication signature for use in API-Sign HTTP header
console.log(apiSignature);


PHP

bash

Bash

// Decode API private key from base64 format displayed in account management
$apiSecret = base64_decode('FRs+gtq09rR7OFtKj9BGhyOGS3u5vtY/EdiIBO9kD8NFtRX7w7LeJDSrX6cq1D8zmQmGkWFjksuhBvKOAWJohQ==');

// Variables (API endpoint, nonce and HTTP POST data)
$apiPath = '/0/private/TradeBalance';
$apiNonce = explode(' ', microtime());
$apiNonce = $apiNonce[1] . substr($apiNonce[0], 2, 3);
$apiPost = "nonce={$apiNonce}&asset=xbt";

// Cryptographic hash algorithms
$apiSha256 = hash('sha256', $apiNonce . $apiPost, true);
$apiSha512 = hash_hmac('sha512', $apiPath . $apiSha256, $apiSecret, true);

// Encode signature into base64 format used in API-Sign value
$apiSignature = base64_encode($apiSha512);

// API authentication signature for use in API-Sign HTTP header
print $apiSignature;

需要更多幫助?