HMAC-SHA512
Toutes les requetes transactionnelles (cash-in, cash-out, refund, webhooks) exigent une signature HMAC-SHA512 pour garantir l'integrite et l'authenticite du payload.
Fonctionnement
- Serialisez le body de la requete en JSON
- Generez la signature HMAC-SHA512 en utilisant votre
client_secretcomme cle - Envoyez la signature dans l'en-tete
hmacen format hexadecimal lowercase
L'API recalcule le hash et le compare a la valeur recue en utilisant une comparaison en temps constant (constant-time comparison), empechant les attaques par timing qui tentent de decouvrir la signature octet par octet.
HMAC-SHA512 vs Webhook SHA256
L'API utilise HMAC-SHA512 pour authentifier les requetes que vous envoyez. Les webhooks envoyes par Owem Pay utilisent HMAC-SHA256 pour la signature (en-tete X-Owem-Signature). Ce sont des algorithmes differents -- chacun dans son contexte. Consultez Webhooks pour les details sur la validation des webhooks.
En-tetes Obligatoires
| En-tete | Valeur |
|---|---|
Authorization | ApiKey {client_id}:{client_secret} |
Content-Type | application/json |
hmac | Signature HMAC-SHA512 en hexadecimal |
Exemples
JavaScript (Node.js)
const crypto = require('crypto');
const body = JSON.stringify({
amount: 3000,
pix_key: "12345678901",
pix_key_type: "cpf",
description: "Pagamento"
});
const hmac = crypto
.createHmac('sha512', 'sk_seu-client-secret')
.update(body)
.digest('hex');
// Envoyer comme en-tete : hmac: {valeur}Python
import hmac
import hashlib
import json
body = json.dumps({
"amount": 3000,
"pix_key": "12345678901",
"pix_key_type": "cpf",
"description": "Pagamento"
})
signature = hmac.new(
b"sk_seu-client-secret",
body.encode("utf-8"),
hashlib.sha512
).hexdigest()PHP
$body = json_encode([
'amount' => 3000,
'pix_key' => '12345678901',
'pix_key_type' => 'cpf',
'description' => 'Pagamento'
]);
$hmac = hash_hmac('sha512', $body, 'sk_seu-client-secret');Java
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
String body = "{\"amount\":3000,\"pix_key\":\"12345678901\",\"pix_key_type\":\"cpf\",\"description\":\"Pagamento\"}";
String secret = "sk_seu-client-secret";
Mac mac = Mac.getInstance("HmacSHA512");
SecretKeySpec keySpec = new SecretKeySpec(secret.getBytes(), "HmacSHA512");
mac.init(keySpec);
byte[] hash = mac.doFinal(body.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : hash) {
sb.append(String.format("%02x", b));
}
String hmac = sb.toString();C#
using System.Security.Cryptography;
using System.Text;
var body = "{\"amount\":3000,\"pix_key\":\"12345678901\",\"pix_key_type\":\"cpf\",\"description\":\"Pagamento\"}";
var secret = Encoding.UTF8.GetBytes("sk_seu-client-secret");
using var hmacSha512 = new HMACSHA512(secret);
var hash = hmacSha512.ComputeHash(Encoding.UTF8.GetBytes(body));
var hmac = BitConverter.ToString(hash).Replace("-", "").ToLower();Bash (curl)
BODY='{"amount":3000,"pix_key":"12345678901","pix_key_type":"cpf","description":"Pagamento"}'
SECRET="sk_seu-client-secret"
HMAC=$(echo -n "$BODY" | openssl dgst -sha512 -hmac "$SECRET" | awk '{print $2}')
curl -X POST https://api.owem.com.br/api/external/pix/cash-out \
-H "Authorization: ApiKey $CLIENT_ID:$CLIENT_SECRET" \
-H "Content-Type: application/json" \
-H "hmac: $HMAC" \
-d "$BODY"Validation
L'API valide le HMAC recu par rapport au body de la requete. Si la signature ne correspond pas :
{
"worked": false,
"detail": "Invalid HMAC signature"
}Important
- Utilisez le body exactement tel qu'envoye dans la requete (meme serialisation JSON)
- Le
client_secretde l'API Key est la cle HMAC - La signature doit etre en format hexadecimal lowercase
- La comparaison est effectuee en temps constant -- il n'est pas possible de decouvrir la signature par timing