@@ -931,44 +931,53 @@ private static RaiseException newError(final Ruby runtime, final String message)
931
931
return Utils .newError (runtime , Error , message );
932
932
}
933
933
934
- @ JRubyMethod (rest = true , visibility = Visibility .PRIVATE )
935
- public IRubyObject initialize (final ThreadContext context , final IRubyObject [] args ) {
936
- final Ruby runtime = context . runtime ;
934
+ @ JRubyMethod (visibility = Visibility .PRIVATE )
935
+ public IRubyObject initialize (final ThreadContext context , final IRubyObject groupOrPoint ) {
936
+ getPointAndGroup ( context , groupOrPoint ) ;
937
937
938
- final int argc = Arity . checkArgumentCount ( runtime , args , 1 , 2 ) ;
939
- final IRubyObject arg = args [ 0 ];
938
+ return this ;
939
+ }
940
940
941
- if ( arg instanceof Point ) {
942
- this . group = (( Point ) arg ). group ;
943
- this . point = (( Point ) arg ). point ;
941
+ @ JRubyMethod ( visibility = Visibility . PRIVATE )
942
+ public IRubyObject initialize ( final ThreadContext context , final IRubyObject groupOrPoint , final IRubyObject bn ) {
943
+ if ( getPointAndGroup ( context , groupOrPoint )) {
944
944
return this ;
945
945
}
946
946
947
- if ( arg instanceof Group ) {
948
- this .group = (Group ) arg ;
947
+ final byte [] encoded ;
948
+ if (bn instanceof BN ) {
949
+ encoded = ((BN ) bn ).getValue ().abs ().toByteArray ();
949
950
} else {
950
- throw runtime . newTypeError ( arg , _EC ( runtime ). getClass ( "Group" ) );
951
+ encoded = bn . convertToString (). getBytes ( );
951
952
}
952
-
953
- if ( argc == 2 ) { // (group, bn)
954
- final byte [] encoded ;
955
- if (args [1 ] instanceof BN ) {
956
- encoded = ((BN ) args [1 ]).getValue ().abs ().toByteArray ();
957
- } else {
958
- encoded = args [1 ].convertToString ().getBytes ();
959
- }
960
- try {
961
- this .point = ECPointUtil .decodePoint (group .getCurve (), encoded );
962
- }
963
- catch (IllegalArgumentException ex ) {
964
- // MRI: OpenSSL::PKey::EC::Point::Error: invalid encoding
965
- throw newError (context .runtime , ex .getMessage ());
966
- }
953
+ try {
954
+ this .point = ECPointUtil .decodePoint (group .getCurve (), encoded );
955
+ }
956
+ catch (IllegalArgumentException ex ) {
957
+ // MRI: OpenSSL::PKey::EC::Point::Error: invalid encoding
958
+ throw newError (context .runtime , ex .getMessage ());
967
959
}
968
960
969
961
return this ;
970
962
}
971
963
964
+ private boolean getPointAndGroup (ThreadContext context , IRubyObject groupOrPoint ) {
965
+ final Ruby runtime = context .runtime ;
966
+
967
+ if ( groupOrPoint instanceof Point ) {
968
+ this .group = ((Point ) groupOrPoint ).group ;
969
+ this .point = ((Point ) groupOrPoint ).point ;
970
+ return true ;
971
+ }
972
+
973
+ if ( groupOrPoint instanceof Group ) {
974
+ this .group = (Group ) groupOrPoint ;
975
+ } else {
976
+ throw runtime .newTypeError (groupOrPoint , _EC (runtime ).getClass ("Group" ));
977
+ }
978
+ return false ;
979
+ }
980
+
972
981
@ Override
973
982
@ JRubyMethod (name = { "==" , "eql?" })
974
983
public IRubyObject op_equal (final ThreadContext context , final IRubyObject obj ) {
@@ -1059,6 +1068,20 @@ public IRubyObject inspect() {
1059
1068
return ObjectSupport .inspect (this , (List ) Collections .singletonList (entry ));
1060
1069
}
1061
1070
1071
+ @ Deprecated
1072
+ public IRubyObject initialize (final ThreadContext context , final IRubyObject [] args ) {
1073
+ final int argc = Arity .checkArgumentCount (context .runtime , args , 1 , 2 );
1074
+
1075
+ switch (argc ) {
1076
+ case 1 :
1077
+ return initialize (context , args [0 ]);
1078
+ case 2 :
1079
+ return initialize (context , args [0 ], args [1 ]);
1080
+ default :
1081
+ throw context .runtime .newArgumentError (args .length , 1 );
1082
+ }
1083
+ }
1084
+
1062
1085
}
1063
1086
1064
1087
static byte [] encode (final ECPublicKey pubKey ) {
0 commit comments