Skip to main content

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.

Metadata

Release files for kreyo-verifactu-hash-calculator 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kreyo-verifactu-hash-calculator 0.1.2
File Size Uploaded Provenance Actions
kreyo_verifactu_hash_calculator-0.1.2.tar.gz 6.3 kB Without provenance Details for kreyo_verifactu_hash_calculator-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for kreyo-verifactu-hash-calculator 0.1.2
File Interpreter ABI Platform Actions
kreyo_verifactu_hash_calculator-0.1.2-py3-none-any.whl Python 3 none any Details for kreyo_verifactu_hash_calculator-0.1.2-py3-none-any.whl

Total release size: 16.3 kB

Release files / kreyo_verifactu_hash_calculator-0.1.2.tar.gz

Download URL kreyo_verifactu_hash_calculator-0.1.2.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
767f9a7c0ce55aca5f6f85597f0a5c069de51b25344bf73e69b036c2a4b02a63
BLAKE2b-256 checksum
How to use checksums
afb47bc78b303c18781c73f28e091def5cd289f287ba8761f65e8c60e2a5eaa0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / kreyo_verifactu_hash_calculator-0.1.2-py3-none-any.whl

Download URL kreyo_verifactu_hash_calculator-0.1.2-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
06259342c5cca42c36a5d102d88b53f340a9ad2a964ad2016f57a25cb39b102b
BLAKE2b-256 checksum
How to use checksums
92831cbe3cee8b4f6af51014a1ec3de7c51dd3bfb87809a9284b29107774d40a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Security for kreyo-verifactu-hash-calculator 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page