Class: Ksef::Crypto::Digest

Inherits:
Data
  • Object
show all
Defined in:
lib/ksef/crypto/digest.rb

Overview

The SHA-256-and-size pair KSeF wants beside a payload (docs/REFERENCE.md §11.1).

POST /sessions/online/{ref}/invoices carries four integrity values — the hash and size of the plaintext invoice and of the ciphertext. Computing one over the wrong artifact is an easy mistake and a silent one, which is why Encryptor#seal produces both at once rather than leaving a caller to pair them up.

size is a byte count, not a character count. An invoice full of Polish characters has more bytes than characters, and reporting the shorter figure would be rejected downstream.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bytes ⇒ Object (readonly)

Returns the value of attribute bytes

Returns:

  • (Object) —

    the current value of bytes



16
17
18
# File 'lib/ksef/crypto/digest.rb', line 16

def bytes
  @bytes
end

#size ⇒ Object (readonly)

Returns the value of attribute size

Returns:

  • (Object) —

    the current value of size



16
17
18
# File 'lib/ksef/crypto/digest.rb', line 16

def size
  @size
end

Class Method Details

.of(content) ⇒ Object

Parameters:

  • content (String) —

    the exact bytes that will be sent



18
# File 'lib/ksef/crypto/digest.rb', line 18

def self.of(content) = new(bytes: Crypto.sha256(content), size: content.bytesize)

Instance Method Details

#base64 ⇒ String

Returns the wire form: base64 of the digest, always 44 characters, which is what the contract's Sha256HashBase64 constrains it to.

Returns:

  • (String) —

    the wire form: base64 of the digest, always 44 characters, which is what the contract's Sha256HashBase64 constrains it to



22
# File 'lib/ksef/crypto/digest.rb', line 22

def base64 = Crypto.encode(bytes)