Sha256: 3b34fe1ca64a2a8490487796cbacce7bfa9baffbc2fde2d12c8e6c467fe3d596

Contents?: true

Size: 1016 Bytes

Versions: 2

Compression:

Stored size: 1016 Bytes

Contents

# frozen_string_literal: true

require "openssl"
require "openssl/signature_algorithm/base"

module OpenSSL
  module SignatureAlgorithm
    class ECDSA < Base
      class SigningKey < OpenSSL::PKey::EC
        def initialize(*args)
          super(*args).generate_key
        end

        def verify_key
          VerifyKey.new(group, public_key.to_bn)
        end
      end

      class VerifyKey < OpenSSL::PKey::EC::Point
        def verify(*args)
          ec_key = OpenSSL::PKey::EC.new(group)
          ec_key.public_key = self

          ec_key.verify(*args)
        end
      end

      CURVE_BY_DIGEST_LENGTH = {
        "256" => "prime256v1",
        "384" => "secp384r1",
        "512" => "secp521r1"
      }.freeze

      def generate_signing_key
        @signing_key = SigningKey.new(curve_name)
      end

      def curve_name
        CURVE_BY_DIGEST_LENGTH[digest_length] ||
          raise(OpenSSL::SignatureAlgorithm::Error, "Unsupported digest length #{digest_length}")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
openssl-signature_algorithm-0.1.1 lib/openssl/signature_algorithm/ecdsa.rb
openssl-signature_algorithm-0.1.0 lib/openssl/signature_algorithm/ecdsa.rb