Class: Ccrypto::Java::ECCPrivateKey

Inherits:
ECCPrivateKey
  • Object
show all
Includes:
DataConversion
Defined in:
lib/ccrypto/java/engines/ecc_engine.rb

Overview

class ECCPublicKey

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataConversion

#from_b64, #from_b64_mime, #from_hex, included, #logger, #to_b64, #to_b64_mime, #to_hex, #to_java_bytes, #to_str

Constructor Details

#initialize(privKey, curve = nil) ⇒ ECCPrivateKey

Returns a new instance of ECCPrivateKey.



103
104
105
106
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 103

def initialize(privKey, curve = nil)
  super(privKey)
  @curve = curve
end

Instance Attribute Details

#curveObject (readonly)

Returns the value of attribute curve.



102
103
104
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 102

def curve
  @curve
end

Class Method Details

.from_pem(str) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 115

def self.from_pem(str)
  if str =~ /ECC PRIVATE/
    cont = str.lines[1..-2].join.strip
    to_key(from_b64(cont))
  else
    raise KeypairEngineException, "Not an ECC private key"
  end
end

.to_key(bin, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 88

def self.to_key(bin, &block)
  if block
    prov = block.call(:jce_provider)
  else
    prov = JCEProvider::BCProv
  end

  kf = java.security.KeyFactory.getInstance("ECDSA",prov)
  priv = kf.generate_private(java.security.spec.PKCS8EncodedKeySpec.new(bin))
  curve = priv.params.name
  ECCPrivateKey.new(priv, curve)

end

Instance Method Details

#equals?(privKey) ⇒ Boolean Also known as: key_equals?

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 128

def equals?(privKey)
  if not @native_privKey.nil?
    case privKey
    when ECCPrivateKey
      @native_privKey.encoded == privKey.to_bin
    else
      logger.warn "Unmatched private key : (native) #{@native_privKey} vs. (subject) #{privKey}"
      false
    end
  else
    logger.warn "ECCPrivateKey equals? returned false because native_privKey is nil"
    false
  end
end

#to_binObject



124
125
126
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 124

def to_bin
  @native_privKey.encoded
end

#to_pemObject



108
109
110
111
112
113
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 108

def to_pem
  cont = ["-----BEGIN ECC PRIVATE KEY-----\n"]
  cont << to_b64(@native_privKey.encoded)
  cont << "\n-----END ECC PRIVATE KEY-----"
  cont.join
end