Module: ECDSA::Format::SignatureDerString

Defined in:
lib/ecdsa/format/signature_der_string.rb

Overview

This module provides methods to convert between Signature objects and their binary DER representation. The format used is defined in [RFC 3278 section 8.2](tools.ietf.org/html/rfc3278#section-8.2).

Class Method Summary collapse

Class Method Details

.decode(der_string) ⇒ Signature

Converts a DER string to a Signature.

Parameters:

  • der_string (String)

Returns:



13
14
15
16
17
18
# File 'lib/ecdsa/format/signature_der_string.rb', line 13

def self.decode(der_string)
  asn1 = OpenSSL::ASN1.decode(der_string)
  r = asn1.value[0].value.to_i
  s = asn1.value[1].value.to_i
  Signature.new(r, s)
end

.encode(signature) ⇒ String

Converts a Signature to a DER string.

Parameters:

Returns:

  • (String)


23
24
25
26
27
28
29
# File 'lib/ecdsa/format/signature_der_string.rb', line 23

def self.encode(signature)
  asn1 = OpenSSL::ASN1::Sequence.new [
    OpenSSL::ASN1::Integer.new(signature.r),
    OpenSSL::ASN1::Integer.new(signature.s),
  ]
  asn1.to_der
end