Method: ECIES::Crypt.public_key_from_hex

Defined in:
lib/ecies/crypt.rb

.public_key_from_hex(hex_string, ec_group = 'secp256k1') ⇒ OpenSSL::PKey::EC

Converts a hex-encoded public key to an OpenSSL::PKey::EC.

Parameters:

  • hex_string (String)

    The hex-encoded public key.

  • ec_group (OpenSSL::PKey::EC::Group, String) (defaults to: 'secp256k1')

    The elliptical curve group for this public key.

Returns:

  • (OpenSSL::PKey::EC)

    The public key.

Raises:

  • (ArgumentError)

    If the public key is invalid.


161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ecies/crypt.rb', line 161

def self.public_key_from_hex(hex_string, ec_group = 'secp256k1')
  ec_group = OpenSSL::PKey::EC::Group.new(ec_group) if ec_group.is_a?(String)

  sequence = OpenSSL::ASN1.Sequence([
    OpenSSL::ASN1.Sequence([
      OpenSSL::ASN1.ObjectId("id-ecPublicKey"),
      OpenSSL::ASN1::ObjectId(ec_group.curve_name),
    ]),
    OpenSSL::ASN1.BitString([hex_string].pack('H*')),
  ])

  OpenSSL::PKey::EC.new(sequence.to_der)
end