Class: Ccrypto::Java::ECCEngine
- Inherits:
-
Object
- Object
- Ccrypto::Java::ECCEngine
show all
- Includes:
- DataConversion, TR::CondUtils
- Defined in:
- lib/ccrypto/java/engines/ecc_engine.rb
Overview
Class Method Summary
collapse
Instance Method Summary
collapse
#from_b64, #from_b64_mime, #from_hex, included, #logger, #to_b64, #to_b64_mime, #to_bin, #to_hex, #to_java_bytes, #to_str
Constructor Details
#initialize(*args, &block) ⇒ ECCEngine
Returns a new instance of ECCEngine.
311
312
313
314
315
|
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 311
def initialize(*args,&block)
@config = args.first
raise KeypairEngineException, "1st parameter must be a #{Ccrypto::KeypairConfig.class} object" if not @config.is_a?(Ccrypto::KeypairConfig)
raise KeypairEngineException, "Keypair config must be an initialized config. Please get the initialized config from the list of supported_curves()" if is_empty?(@config.provider_config)
end
|
Class Method Details
.ecc_public_key_from_private_key(privKey) ⇒ Object
KeyFactory keyFactory = KeyFactory.getInstance(EC.getAlgorithmName(), BouncyCastleProviderHolder.getInstance()); BCECPrivateKey ecPrivateKey = (BCECPrivateKey) privateKey; ECParameterSpec ecParameterSpec = ecPrivateKey.getParameters(); ECPoint ecPoint = new FixedPointCombMultiplier().multiply(ecParameterSpec.getG(), ecPrivateKey.getD()); ECPublicKeySpec keySpec = new ECPublicKeySpec(ecPoint, ecParameterSpec); return keyFactory.generatePublic(keySpec);
397
398
399
400
401
402
403
|
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 397
def self.ecc_public_key_from_private_key(privKey)
kf = java.security.KeyFactory.getInstance("ECDSA", JCEProvider::BCProv)
param = privKey.parameters
ecPoint = org.bouncycastle.math.ec.FixedPointCombMultiplier.new.multiply(param.g, privKey.d)
keySpec = org.bouncycastle.jce.spec.ECPublicKeySpec.new(ecPoint, param)
kf.generatePublic(keySpec)
end
|
.find_curve(name) ⇒ Object
Also known as:
find_by_param
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 292
def self.find_curve(name)
case name
when String, Symbol
supported_curves.select { |c| c.provider_config[:curve] == name or c.curve == name.downcase.to_sym }.first
when Ccrypto::ECCKeyBundle
fname = name.key_param
supported_curves.select { |c| c.provider_config[:curve] == fname.provider_config[:curve] or c.curve == fname.algo }.first
when Ccrypto::ECCPublicKey
fname = name.curve
supported_curves.select { |c| c.provider_config[:curve] == fname or c.curve == fname.to_sym }.first
else
raise KeypairEngineException, "Not supported curve finder with '#{name.class}'"
end
end
|
.supported_curves ⇒ Object
Also known as:
supported_params
281
282
283
284
285
286
287
288
289
290
|
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 281
def self.supported_curves
if @curves.nil?
@curves = org.bouncycastle.asn1.x9.ECNamedCurveTable.getNames.sort.to_a.map { |c|
conf = Ccrypto::ECCConfig.new(c.downcase.to_sym)
conf.provider_config = { curve: c }
conf
}
end
@curves
end
|
.verify(pubKey, val, sign) ⇒ Object
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 372
def self.verify(pubKey, val, sign)
ver = java.security.Signature.getInstance("SHA256WithECDSA")
ver.initVerify(pubKey)
case val
when java.io.InputStream
buf = Java::byte[102400].new
while((read = val.read(buf, 0 ,buf.length)) != nil)
ver.update(buf,0, read)
end
else
ver.update(to_java_bytes(val))
end
ver.verify(to_java_bytes(sign))
end
|
Instance Method Details
#generate_keypair(&block) ⇒ Object
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 317
def generate_keypair(&block)
algoName = "ECDSA"
prov = Ccrypto::Java::JCEProvider::BCProv
randomEngine = java.security.SecureRandom.new
if block
uprov = block.call(:jce_provider)
prov = uprov if not is_empty?(uprov)
uAlgo = block.call(:jce_algo_name)
algoName = uAlgo if not is_empty?(uAlgo)
uRandEng = block.call(:random_engine)
randomEngine = uRandEng if not uRandEng.nil?
end
kpg = java.security.KeyPairGenerator.getInstance(algoName, prov)
kpg.java_send :initialize, [java.security.spec.AlgorithmParameterSpec, randomEngine.class], java.security.spec.ECGenParameterSpec.new(@config.provider_config[:curve]), randomEngine
kp = kpg.generate_key_pair
kb = ECCKeyBundle.new(kp, @config)
kb
end
|
#regenerate_keypair(pubKey, privKey, &block) ⇒ Object
346
347
348
349
|
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 346
def regenerate_keypair(pubKey, privKey, &block)
kp = java.security.KeyPair.new(pubKey, privKey)
ECCKeyBundle.new(kp)
end
|
#sign(val, &block) ⇒ Object
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
# File 'lib/ccrypto/java/engines/ecc_engine.rb', line 351
def sign(val, &block)
raise KeypairEngineException, "Keypair is required" if @config.keypair.nil?
raise KeypairEngineException, "ECC keypair is required. Given #{@config.keypair}" if not @config.keypair.is_a?(ECCKeyBundle)
kp = @config.keypair
sign = java.security.Signature.getInstance("SHA256WithECDSA")
sign.initSign(kp.private_key)
case val
when java.io.InputStream
buf = Java::byte[102400].new
while((read = val.read(buf, 0, buf.length)) != nil)
sign.update(buf,0,read)
end
else
sign.update(to_java_bytes(val))
end
sign.sign
end
|