Skip to content

HMAC-SHA512

Todas las solicitudes transaccionales (cash-in, cash-out, refund, webhooks) exigen firma HMAC-SHA512 para garantizar integridad y autenticidad del payload.

Como Funciona

  1. Serialice el body de la solicitud como JSON
  2. Genere la firma HMAC-SHA512 usando su client_secret como clave
  3. Envie la firma en el header hmac en formato hexadecimal lowercase

La API recalcula el hash y lo compara con el valor recibido usando comparacion en tiempo constante (constant-time comparison), impidiendo ataques de timing que intentan descubrir la firma byte a byte.

HMAC-SHA512 vs Webhook SHA256

La API usa HMAC-SHA512 para autenticar solicitudes enviadas por usted. Los webhooks enviados por Owem Pay usan HMAC-SHA256 para firma (header X-Owem-Signature). Son algoritmos diferentes -- cada uno en su contexto. Vea Webhooks para detalles de la validacion de webhooks.

Headers Obligatorios

HeaderValor
AuthorizationApiKey {client_id}:{client_secret}
Content-Typeapplication/json
hmacFirma HMAC-SHA512 en hexadecimal

Ejemplos

JavaScript (Node.js)

javascript
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');

// Enviar como header: hmac: {valor}

Python

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

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

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#

csharp
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)

bash
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"

Validacion

La API valida el HMAC recibido contra el body de la solicitud. Si la firma no coincide:

json
{
  "worked": false,
  "detail": "Invalid HMAC signature"
}

Importante

  • Use el body exactamente como se envia en la solicitud (misma serializacion JSON)
  • El client_secret de la API Key es la clave HMAC
  • La firma debe estar en formato hexadecimal lowercase
  • La comparacion se realiza en tiempo constante -- no es posible descubrir la firma por timing

Owem Pay Instituição de Pagamento — ISPB 37839059