Class: EC::PublicKey

Inherits:
Object
  • Object
show all
Defined in:
lib/elliptic/public_key.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, group: nil) ⇒ PublicKey

Returns a new instance of PublicKey.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/elliptic/public_key.rb', line 29

def initialize( *args, group: nil )
  if args.size == 2   ## assume (x,y) raw integer points
      @pt = Point.new( *args, group: group )
      point = @pt.to_ec_point  ## convert point to openssl (native) class
      @pkey = OpenSSL::PKey::EC.new( point.group )
      @pkey.public_key = point
  elsif args[0].is_a?( Point ) ||
        args[0].is_a?( OpenSSL::PKey::EC::Point )
     ##  "restore"  public key (only) from point for verify
     ## - OpenSSL::PKey::EC::Point   ## assume public key only (restore pkey object for verify?)
     ## - Point
      point = if args[0].is_a?( Point )
                @pt = args[0]
                @pt.to_ec_point
              else
                args[0]   ## assume it is already OpenSSL::PKey::EC::Point
              end

      ## note: (auto)get group from point
      @pkey = OpenSSL::PKey::EC.new( point.group )
      @pkey.public_key = point
  else  ## assume string in pem/der/base64
    @pkey = OpenSSL::PKey::EC.new( args[0] )
  end
end

Class Method Details

.convert(*args, **kwargs) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/elliptic/public_key.rb', line 5

def self.convert( *args, **kwargs )
  if args.size==1 && args[0].is_a?( PublicKey )
    args[0]   ## pass through as is (already a public key)
  else
    new( *args, group: kwargs[:group] )
  end
end

.decode_base64(str) ⇒ Object Also known as: from_base64

todo/check: only use (allow) base64 for

der (binary)-encoded? why? why not?


20
# File 'lib/elliptic/public_key.rb', line 20

def self.decode_base64( str ) new( Base64.decode64(str)); end

.decode_der(str) ⇒ Object Also known as: from_der



16
# File 'lib/elliptic/public_key.rb', line 16

def self.decode_der( str ) new( str ); end

.decode_pem(str) ⇒ Object Also known as: from_pem



15
# File 'lib/elliptic/public_key.rb', line 15

def self.decode_pem( str ) new( str ); end

Instance Method Details

#groupObject

more helpers for debugging / internals



78
# File 'lib/elliptic/public_key.rb', line 78

def group() @pkey.group; end

#pointObject



57
58
59
60
61
# File 'lib/elliptic/public_key.rb', line 57

def point
  ## cache returned point - why? why not?
  @pt ||= Point.new( @pkey.public_key )
  @pt
end

#private?Boolean

todo/check: keep - needed? - why? why not?

Returns:

  • (Boolean)


80
# File 'lib/elliptic/public_key.rb', line 80

def private?() @pkey.private?; end

#public?Boolean

todo/check: keep - needed? - why? why not?

Returns:

  • (Boolean)


81
# File 'lib/elliptic/public_key.rb', line 81

def public?()  @pkey.public?; end

#to_base64Object



66
# File 'lib/elliptic/public_key.rb', line 66

def to_base64()  Base64.encode64( to_der ); end

#to_derObject



65
# File 'lib/elliptic/public_key.rb', line 65

def to_der()  @pkey.to_der; end

#to_pemObject



64
# File 'lib/elliptic/public_key.rb', line 64

def to_pem()  @pkey.to_pem; end

#to_textObject



79
# File 'lib/elliptic/public_key.rb', line 79

def to_text()  @pkey.to_text; end

#verify?(message, signature) ⇒ Boolean Also known as: valid_signature?

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/elliptic/public_key.rb', line 69

def verify?( message, signature )
  signature_der = signature.to_der
  @pkey.dsa_verify_asn1( message, signature_der )
end