Class: Moxml::Signature::Algorithms::DsaSha

Inherits:
SignatureMethodBase show all
Defined in:
lib/moxml/signature/algorithms/dsa_sha.rb

Overview

DSA signature methods per FIPS 186-3.

Two URIs:

- SHA1 (1024-bit, q=160): wire format r‖s with 20-byte halves
- SHA256 (2048-bit, q=256): wire format r‖s with N-byte halves
where N = byte length of q (32 for SHA-256 case)

OpenSSL returns DER; we convert to raw r‖s.

Constant Summary collapse

PAIRINGS =
{
  "http://www.w3.org/2000/09/xmldsig#dsa-sha1" => "SHA1",
  "http://www.w3.org/2009/xmldsig11#dsa-sha256" => "SHA256",
}.freeze

Instance Method Summary collapse

Methods inherited from SignatureMethodBase

identifier, #sign, #verify

Constructor Details

#initialize(identifier_uri:, parameters: nil) ⇒ DsaSha

Returns a new instance of DsaSha.



24
25
26
27
28
# File 'lib/moxml/signature/algorithms/dsa_sha.rb', line 24

def initialize(identifier_uri:, parameters: nil)
  super(nil)
  @identifier_uri = identifier_uri
  @parameters = parameters
end

Instance Method Details

#compute_signature(data, key) ⇒ Object



30
31
32
33
34
35
# File 'lib/moxml/signature/algorithms/dsa_sha.rb', line 30

def compute_signature(data, key)
  dsa = coerce_signing_key(key)
  digest_name = self.class::PAIRINGS.fetch(@identifier_uri)
  der_signature = dsa.sign(digest_name, data)
  der_to_raw(der_signature, coordinate_bytes_for(dsa))
end

#verify_signature(data, key, signature) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/moxml/signature/algorithms/dsa_sha.rb', line 37

def verify_signature(data, key, signature)
  dsa = coerce_verify_key(key)
  digest_name = self.class::PAIRINGS.fetch(@identifier_uri)
  n_bytes = coordinate_bytes_for(dsa)
  der_signature = raw_to_der(signature, n_bytes)
  dsa.verify(digest_name, der_signature, data)
rescue ArgumentError
  false
end