Skip to main content

Overview

The hash utilities module provides classes for generating cryptographic hashes using various SHA algorithms. These are commonly used for digital signatures in UBL documents. Source: src/tools/shas.ts

SHA256

Class for generating SHA-256 cryptographic hashes.
class SHA256 {
  getHash(content: string, inputEncoding?: string, outputEncoding?: string): string
  getAlgorithmName(): string
}

Methods

getHash

Generates a SHA-256 hash of the provided content.
content
string
required
The string content to hash
inputEncoding
'utf8' | 'base64' | 'binary' | 'hex'
default:"utf8"
Encoding of the input content
outputEncoding
'utf8' | 'base64' | 'binary' | 'hex'
default:"base64"
Encoding format for the output hash
Returns: string - The computed hash in the specified output encoding

getAlgorithmName

Returns the W3C algorithm identifier URI for SHA-256. Returns: string - "http://www.w3.org/2001/04/xmlenc#sha256"

Examples

import { SHA256 } from 'ubl-builder';

const hasher = new SHA256();
const hash = hasher.getHash('abc123');
console.log(hash); // Base64-encoded SHA-256 hash

SHA384

Class for generating SHA-384 cryptographic hashes.
class SHA384 {
  getHash(content: string, inputEncoding?: string, outputEncoding?: string): string
  getAlgorithmName(): string
}

Methods

getHash

Generates a SHA-384 hash of the provided content.
content
string
required
The string content to hash
inputEncoding
'utf8' | 'base64' | 'binary' | 'hex'
default:"utf8"
Encoding of the input content
outputEncoding
'utf8' | 'base64' | 'binary' | 'hex'
default:"hex"
Encoding format for the output hash
Returns: string - The computed hash in the specified output encoding

getAlgorithmName

Returns the W3C algorithm identifier URI for SHA-384. Returns: string - "http://www.w3.org/2001/04/xmlenc#sha256"

Examples

import { SHA384 } from 'ubl-builder';

const hasher = new SHA384();
const hash = hasher.getHash('abc123');
console.log(hash); // Hex-encoded SHA-384 hash (default)

SHA1

Class for generating SHA-1 cryptographic hashes.
SHA-1 is considered cryptographically weak for security-critical applications. Use SHA-256 or stronger algorithms for new implementations.
class SHA1 {
  getHash(content: string, inputEncoding?: string, outputEncoding?: string): string
  getAlgorithmName(): string
}

Methods

getHash

Generates a SHA-1 hash of the provided content.
content
string
required
The string content to hash
inputEncoding
'utf8' | 'base64' | 'binary' | 'hex'
default:"utf8"
Encoding of the input content
outputEncoding
'utf8' | 'base64' | 'binary' | 'hex'
default:"base64"
Encoding format for the output hash
Returns: string - The computed hash in the specified output encoding

getAlgorithmName

Returns the W3C algorithm identifier URI for SHA-1. Returns: string - "http://www.w3.org/2000/09/xmldsig#sha1"

Examples

import { SHA1 } from 'ubl-builder';

const hasher = new SHA1();
const hash = hasher.getHash('abc123');
console.log(hash); // Base64-encoded SHA-1 hash

SHA512

Class for generating SHA-512 cryptographic hashes.
class SHA512 {
  getHash(content: string, inputEncoding?: string, outputEncoding?: string): string
  getAlgorithmName(): string
}

Methods

getHash

Generates a SHA-512 hash of the provided content.
content
string
required
The string content to hash
inputEncoding
'utf8' | 'base64' | 'binary' | 'hex'
default:"utf8"
Encoding of the input content
outputEncoding
'utf8' | 'base64' | 'binary' | 'hex'
default:"base64"
Encoding format for the output hash
Returns: string - The computed hash in the specified output encoding

getAlgorithmName

Returns the W3C algorithm identifier URI for SHA-512. Returns: string - "http://www.w3.org/2001/04/xmlenc#sha512"

Examples

import { SHA512 } from 'ubl-builder';

const hasher = new SHA512();
const hash = hasher.getHash('abc123');
console.log(hash); // Base64-encoded SHA-512 hash

Common Usage Patterns

import { SHA256 } from 'ubl-builder';

// Generate hash for UBL document signing
const xmlDocument = invoice.getXML();
const hasher = new SHA256();
const documentHash = hasher.getHash(xmlDocument, 'utf8', 'base64');
const algorithmUri = hasher.getAlgorithmName();

console.log('Hash:', documentHash);
console.log('Algorithm:', algorithmUri);

Use Cases

  • Digital signatures for UBL invoices and documents
  • Document integrity verification
  • Compliance with XML signature standards (XMLDSig)
  • Cryptographic operations in e-invoicing systems
  • Content verification and tamper detection

Algorithm Recommendations

  • SHA-256: Recommended for most UBL document signing scenarios
  • SHA-384: For enhanced security requirements
  • SHA-512: For maximum security or specific regulatory requirements
  • SHA-1: Only for legacy system compatibility (not recommended for new implementations)

Build docs developers (and LLMs) love