HMAC-SHA512
Todas las solicitudes transaccionales (cash-out, cash-in, refund) requieren firma HMAC-SHA512 para garantizar integridad y autenticidad.
Como Funciona
- Serialice el body de la solicitud como JSON
- Genere la firma HMAC-SHA512 usando su
client_secretcomo clave - Envie la firma en el encabezado
hmac
Encabezados Obligatorios
| Encabezado | Valor |
|---|---|
Authorization | Bearer {access_token} |
Content-Type | application/json |
hmac | Firma HMAC-SHA512 en hexadecimal |
Ejemplos
JavaScript (Node.js)
javascript
const crypto = require('crypto');
const body = JSON.stringify({
amount: 10000,
pix_key: "12345678901",
description: "Pago"
});
const hmac = crypto
.createHmac('sha512', 'su-api-key-secret')
.update(body)
.digest('hex');
// Enviar como encabezado: hmac: {valor}Python
python
import hmac
import hashlib
import json
body = json.dumps({
"amount": 10000,
"pix_key": "12345678901",
"description": "Pago"
})
signature = hmac.new(
b"su-api-key-secret",
body.encode("utf-8"),
hashlib.sha512
).hexdigest()PHP
php
$body = json_encode([
'amount' => 10000,
'pix_key' => '12345678901',
'description' => 'Pago'
]);
$hmac = hash_hmac('sha512', $body, 'su-api-key-secret');Java
java
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
String body = "{\"amount\":10000,\"pix_key\":\"12345678901\",\"description\":\"Pago\"}";
String secret = "su-api-key-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#
csharp
using System.Security.Cryptography;
using System.Text;
var body = "{\"amount\":10000,\"pix_key\":\"12345678901\",\"description\":\"Pago\"}";
var secret = Encoding.UTF8.GetBytes("su-api-key-secret");
using var hmacSha512 = new HMACSHA512(secret);
var hash = hmacSha512.ComputeHash(Encoding.UTF8.GetBytes(body));
var hmac = BitConverter.ToString(hash).Replace("-", "").ToLower();Bash (curl)
bash
BODY='{"amount":10000,"pix_key":"12345678901","description":"Pago"}'
SECRET="su-api-key-secret"
HMAC=$(echo -n "$BODY" | openssl dgst -sha512 -hmac "$SECRET" | awk '{print $2}')
curl -X POST https://api.owem.com.br/api/v2/external/pix/cash-out \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-H "hmac: $HMAC" \
-d "$BODY"Validacion
La API valida el HMAC recibido contra el body de la solicitud. Si la firma no coincide:
json
{
"worked": false,
"detail": "HMAC invalido"
}Importante
- Use el body exactamente como se envia en la solicitud (misma serializacion JSON)
- El
client_secretde la API Key es la clave HMAC - La firma debe estar en formato hexadecimal en minusculas