Module: JsonWebToken::Format::Asn1

Included in:
Algorithm::Ecdsa
Defined in:
lib/json_web_token/format/asn1.rb

Overview

ASN1 data structures are usually encoded using the Distinguished Encoding Rules (DER). The ASN1 module provides the necessary classes that allow generation of ASN1 data structures and the methods to encode them using a DER encoding. The decode method allows parsing arbitrary DER-encoded data to a Ruby object that can then be modified and re-encoded at will.

Constant Summary collapse

KEY_BITS =
{
  '256' => 256,
  '384' => 384,
  '512' => 521 # note difference
}

Class Method Summary collapse

Class Method Details

.der_to_signature(der, sha_bits) ⇒ Object



21
22
23
24
25
# File 'lib/json_web_token/format/asn1.rb', line 21

def der_to_signature(der, sha_bits)
  signature_pair = OpenSSL::ASN1.decode(der).value
  width = per_part_byte_count(sha_bits)
  signature_pair.map { |part| part.value.to_s(2).rjust(width, "\x00") }.join
end

.signature_to_der(signature, sha_bits) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/json_web_token/format/asn1.rb', line 27

def signature_to_der(signature, sha_bits)
  hsh = destructured_sig(signature, sha_bits)
  asn1_seq = OpenSSL::ASN1::Sequence.new([
    asn1_int(hsh[:r]),
    asn1_int(hsh[:s])
  ])
  asn1_seq.to_der
end