Skip to main content

Open-source reference implementation of the SHA-256 chained hash algorithm required by the Spanish Tax Agency (AEAT) for VeriFactu billing records.

Project description

verifactu-hash-calculator

🇪🇸 Versión en español

Open-source reference implementation of the SHA-256 chained hash algorithm required by the Spanish Tax Agency (AEAT) for VeriFactu billing records.

License .NET CI TypeScript CI Python CI

NuGet npm PyPI

What it does

verifactu-hash-calculator provides simple functions to calculate and verify the cryptographic footprint (hash) of a VeriFactu billing record (alta, anulación, or evento). It takes the specific fields required by the Spanish Tax Agency as input and produces the standard 64-character uppercase hexadecimal SHA-256 hash.

Why it exists

The AEAT requires a very specific field concatenation logic, trimming rules, and omission handling before hashing. This repository serves as an open-source reference implementation that perfectly passes all the official AEAT test vectors, avoiding developers the hassle of implementing these rules from scratch based on the official PDF.

Conformance

Conforms to AEAT spec v0.1.2 (27/08/2024): "Detalle de las especificaciones técnicas para la generación de la huella o hash de los registros de facturación". Official PDF Document

Install

.NET

dotnet add package Kreyo.VerifactuHashCalculator

Node.js / TypeScript

npm install @kreyo/verifactu-hash-calculator

Python

pip install kreyo-verifactu-hash-calculator

Quick example

.NET

using Kreyo.VerifactuHashCalculator;

var input = new RegistroAltaInput(
    IDEmisorFactura: "89890001K",
    NumSerieFactura: "12345678/G33",
    FechaExpedicionFactura: "01-01-2024",
    TipoFactura: "F1",
    CuotaTotal: "12.35",
    ImporteTotal: "123.45",
    HuellaAnterior: null,
    FechaHoraHusoGenRegistro: "2024-01-01T19:20:30+01:00"
);

string hash = HashCalculator.ComputeRegistroAlta(input);
// => "3C464DAF61ACB827C65FDA19F352A4E3BDC2C640E9E9FC4CC058073F38F12F60"

TypeScript

import { computeRegistroAlta } from '@kreyo/verifactu-hash-calculator';

const hash = computeRegistroAlta({
  idEmisorFactura: "89890001K",
  numSerieFactura: "12345678/G33",
  fechaExpedicionFactura: "01-01-2024",
  tipoFactura: "F1",
  cuotaTotal: "12.35",
  importeTotal: "123.45",
  huellaAnterior: null,
  fechaHoraHusoGenRegistro: "2024-01-01T19:20:30+01:00"
});
// => "3C464DAF61ACB827C65FDA19F352A4E3BDC2C640E9E9FC4CC058073F38F12F60"

Python

from kreyo_verifactu_hash_calculator import compute_registro_alta, RegistroAltaInput

input_data = RegistroAltaInput(
    id_emisor_factura="89890001K",
    num_serie_factura="12345678/G33",
    fecha_expedicion_factura="01-01-2024",
    tipo_factura="F1",
    cuota_total="12.35",
    importe_total="123.45",
    huella_anterior=None,
    fecha_hora_huso_gen_registro="2024-01-01T19:20:30+01:00"
)

hash_str = compute_registro_alta(input_data)
# => "3C464DAF61ACB827C65FDA19F352A4E3BDC2C640E9E9FC4CC058073F38F12F60"

API reference

For details on the algorithm step-by-step, see docs/algorithm.md.

  • Compute:
    • computeRegistroAlta(input)
    • computeRegistroAnulacion(input)
    • computeRegistroEvento(input)
  • Verify:
    • verifyRegistroAlta(input, expectedHash)
    • verifyRegistroAnulacion(input, expectedHash)
    • verifyRegistroEvento(input, expectedHash)

Test vectors

Official test vectors from the AEAT specification:

Case 1: First "alta" record

Input:

{
  "idEmisorFactura": "89890001K",
  "numSerieFactura": "12345678/G33",
  "fechaExpedicionFactura": "01-01-2024",
  "tipoFactura": "F1",
  "cuotaTotal": "12.35",
  "importeTotal": "123.45",
  "huellaAnterior": null,
  "fechaHoraHusoGenRegistro": "2024-01-01T19:20:30+01:00"
}

Expected Hash: 3C464DAF61ACB827C65FDA19F352A4E3BDC2C640E9E9FC4CC058073F38F12F60

Case 2: Second "alta" record (chained)

Input:

{
  "idEmisorFactura": "89890001K",
  "numSerieFactura": "12345679/G34",
  "fechaExpedicionFactura": "01-01-2024",
  "tipoFactura": "F1",
  "cuotaTotal": "12.35",
  "importeTotal": "123.45",
  "huellaAnterior": "3C464DAF61ACB827C65FDA19F352A4E3BDC2C640E9E9FC4CC058073F38F12F60",
  "fechaHoraHusoGenRegistro": "2024-01-01T19:20:35+01:00"
}

Expected Hash: F7B94CFD8924EDFF273501B01EE5153E4CE8F259766F88CF6ACB8935802A2B97

Case 3: "Anulación" record (chained)

Input:

{
  "idEmisorFacturaAnulada": "89890001K",
  "numSerieFacturaAnulada": "12345679/G34",
  "fechaExpedicionFacturaAnulada": "01-01-2024",
  "huellaAnterior": "F7B94CFD8924EDFF273501B01EE5153E4CE8F259766F88CF6ACB8935802A2B97",
  "fechaHoraHusoGenRegistro": "2024-01-01T19:20:40+01:00"
}

Expected Hash: 177547C0D57AC74748561D054A9CEC14B4C4EA23D1BEFD6F2E69E3A388F90C68

Limitations / Non-goals

This repository only computes hashes. It does not:

  • Generate or format VeriFactu XMLs.
  • Sign the XML records (XMLDSig).
  • Handle AEAT submission, mTLS connection, or retries.
  • Generate QR codes.
  • Validate fiscal fields (e.g. NIF syntax).

About Kreyo

This library is maintained by Kreyo, a Spanish fiscal compliance API platform. If you need full VeriFactu submission (signing, mTLS, AEAT integration, QR, retries) instead of just hash calculation, check out Kreyo VeriFactu.

License

MIT License. See LICENSE.

Contributing

Please see CONTRIBUTING.md for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kreyo_verifactu_hash_calculator-0.1.2.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kreyo_verifactu_hash_calculator-0.1.2-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file kreyo_verifactu_hash_calculator-0.1.2.tar.gz.

File metadata

File hashes

Hashes for kreyo_verifactu_hash_calculator-0.1.2.tar.gz
Algorithm Hash digest
SHA256 767f9a7c0ce55aca5f6f85597f0a5c069de51b25344bf73e69b036c2a4b02a63
MD5 f5709f322f87dc55d5f53548c4e5a7b0
BLAKE2b-256 afb47bc78b303c18781c73f28e091def5cd289f287ba8761f65e8c60e2a5eaa0

See more details on using hashes here.

File details

Details for the file kreyo_verifactu_hash_calculator-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for kreyo_verifactu_hash_calculator-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 06259342c5cca42c36a5d102d88b53f340a9ad2a964ad2016f57a25cb39b102b
MD5 c6f41ac6bf652267cd1dafa5cf977c79
BLAKE2b-256 92831cbe3cee8b4f6af51014a1ec3de7c51dd3bfb87809a9284b29107774d40a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page