Skip to content
smallPict

Authenticatie en HMAC-SHA256 handtekeningen

Beveiligde verzoeken via HMAC-SHA256, tijdstempelvalidatie en bescherming tegen replay.

Om maximale integriteit in productieomgevingen te garanderen, vereist SmallPict voor elk bewerkingsverzoek een HMAC-SHA256 handtekening. Dit voorkomt datatransit-manipulatie en weert replay-aanvallen af.


Verplichte HTTP-headers

HeaderTypeBeschrijving
X-API-KeyTekstUw openbare API-sleutel (sp_live_... of sp_test_...).
X-TimestampGeheel getalUnix-tijdstempel in seconden UTC (bijv. 1716301234).
X-SignatureTekstBerekende HMAC-SHA256 handtekening in hexadecimaal formaat.

Algoritme voor handtekeningberekening

De handtekening ontstaat door HMAC-SHA256 toe te passen met uw Secret Key op de canonieke string:

TEXT
CANONICAL_STRING = HTTP_METHOD + "\n" + REQUEST_PATH + "\n" + TIMESTAMP + "\n" + SHA256_HEX(REQUEST_BODY)

Belangrijk: De waarde in X-Timestamp mag maximaal ±300 seconden (5 minuten) afwijken van de officiële SmallPict-servertijd. Verzoeken die hierbuiten vallen, worden afgewezen met 401 Unauthorized.


Codevoorbeelden

Node.js / TypeScript

TypeScript
import crypto from "crypto";
function generateSignature(method: string, path: string, timestamp: number, body: string, secretKey: string): string {  const bodyHash = crypto.createHash("sha256").update(body).digest("hex");  const canonical = `${method.toUpperCase()}\n${path}\n${timestamp}\n${bodyHash}`;  return crypto.createHmac("sha256", secretKey).update(canonical).digest("hex");}

Python

Python
import hmac, hashlib
def generate_signature(method: str, path: str, timestamp: int, body: bytes, secret_key: str) -> str:    body_hash = hashlib.sha256(body).hexdigest()    canonical = f"{method.upper()}\n{path}\n{timestamp}\n{body_hash}"    return hmac.new(secret_key.encode('utf-8'), canonical.encode('utf-8'), hashlib.sha256).hexdigest()

Golang

Go
package main
import (    "crypto/hmac"    "crypto/sha256"    "encoding/hex"    "fmt"    "strings")
func GenerateSignature(method, path string, timestamp int64, body []byte, secretKey string) string {    bodyHash := sha256.Sum256(body)    canonical := fmt.Sprintf("%s\n%s\n%d\n%s", strings.ToUpper(method), path, timestamp, hex.EncodeToString(bodyHash[:]))    mac := hmac.New(sha256.New, []byte(secretKey))    mac.Write([]byte(canonical))    return hex.EncodeToString(mac.Sum(nil))}