Skip to content

Commit 7a7d3bc

Browse files
committed
[feat] implement support for PKey::EC.generate
1 parent 8e81ebb commit 7a7d3bc

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/main/java/org/jruby/ext/openssl/PKeyEC.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,7 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
248248
IRubyObject arg = args[0];
249249

250250
if ( arg instanceof Group ) {
251-
this.group = (Group) arg;
252-
this.curveName = this.group.getCurveName();
251+
setGroup((Group) arg);
253252
return this;
254253
}
255254

@@ -380,6 +379,11 @@ private void unwrapPrivateKeyWithName() {
380379
}
381380
}
382381

382+
private void setGroup(final Group group) {
383+
this.group = group;
384+
this.curveName = this.group.getCurveName();
385+
}
386+
383387
//private static ECNamedCurveParameterSpec readECParameters(final byte[] input) throws IOException {
384388
// ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(input);
385389
// return ECNamedCurveTable.getParameterSpec(oid.getId());
@@ -407,6 +411,19 @@ public PKeyEC generate_key(final ThreadContext context) {
407411
return this;
408412
}
409413

414+
@JRubyMethod(meta = true)
415+
public static IRubyObject generate(final ThreadContext context, final IRubyObject self, final IRubyObject group) {
416+
PKeyEC randomKey = new PKeyEC(context.runtime, (RubyClass) self);
417+
418+
if (group instanceof Group) {
419+
randomKey.setGroup((Group) group);
420+
} else {
421+
randomKey.curveName = group.convertToString().toString();
422+
}
423+
424+
return randomKey.generate_key(context);
425+
}
426+
410427
@JRubyMethod(name = "dsa_sign_asn1")
411428
public IRubyObject dsa_sign_asn1(final ThreadContext context, final IRubyObject data) {
412429
try {

src/test/ruby/ec/test_ec.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,13 @@ def setup
203203
super
204204
self.class.disable_security_restrictions!
205205

206-
# @data1 = 'foo'; @data2 = 'bar' * 1000 # data too long for DSA sig
207-
208206
@groups = []; @keys = []
209207

210208
OpenSSL::PKey::EC.builtin_curves.each do |curve, comment|
211209
next if curve.start_with?("Oakley") # Oakley curves are not suitable for ECDSA
212-
group = OpenSSL::PKey::EC::Group.new(curve)
213-
214-
key = OpenSSL::PKey::EC.new(group)
215-
key.generate_key
216210

217-
@groups << group; @keys << key
211+
@groups << group = OpenSSL::PKey::EC::Group.new(curve)
212+
@keys << OpenSSL::PKey::EC.generate(group)
218213
end
219214
end
220215

0 commit comments

Comments
 (0)