Skip to content

Commit 7308da1

Browse files
committed
align BH#hash with eql? (+ equals/hashCode on Java) & renamed method
1 parent dec94fc commit 7308da1

File tree

2 files changed

+63
-25
lines changed

2 files changed

+63
-25
lines changed

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

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public synchronized IRubyObject initialize_copy(final IRubyObject that) {
173173
@JRubyMethod(name = "copy")
174174
public IRubyObject copy(IRubyObject other) {
175175
if (this != other) {
176-
this.value = getBigInteger(other);
176+
this.value = asBigInteger(other);
177177
}
178178
return this;
179179
}
@@ -271,8 +271,23 @@ public IRubyObject inspect() {
271271
return ObjectSupport.inspect(this, Collections.EMPTY_LIST);
272272
}
273273

274+
@Override
275+
public boolean equals(Object other) {
276+
return (other instanceof BN) ? this.value.equals(((BN) other).value) : false;
277+
}
278+
279+
@Override
280+
public int hashCode() {
281+
return 997 * value.hashCode();
282+
}
283+
284+
@JRubyMethod(name = "hash")
285+
public RubyInteger hash(final ThreadContext context) {
286+
return context.runtime.newFixnum(hashCode());
287+
}
288+
274289
@JRubyMethod(name = "to_i")
275-
public IRubyObject to_i() {
290+
public RubyInteger to_i() {
276291
if ( value.compareTo( MAX_LONG ) > 0 || value.compareTo( MIN_LONG ) < 0 ) {
277292
return RubyBignum.newBignum(getRuntime(), value);
278293
}
@@ -321,17 +336,17 @@ public RubyBoolean odd_p(final ThreadContext context) {
321336

322337
@JRubyMethod(name={"cmp", "<=>"})
323338
public IRubyObject cmp(final ThreadContext context, IRubyObject other) {
324-
return context.runtime.newFixnum( value.compareTo( getBigInteger(other) ) );
339+
return context.runtime.newFixnum( value.compareTo( asBigInteger(other) ) );
325340
}
326341

327342
@JRubyMethod(name="ucmp")
328343
public IRubyObject ucmp(final ThreadContext context, IRubyObject other) {
329-
return context.runtime.newFixnum( value.abs().compareTo( getBigInteger(other).abs() ) );
344+
return context.runtime.newFixnum( value.abs().compareTo( asBigInteger(other).abs() ) );
330345
}
331346

332347
@JRubyMethod(name={"eql?", "==", "==="})
333348
public RubyBoolean eql_p(final ThreadContext context, IRubyObject other) {
334-
return context.runtime.newBoolean( value.equals( getBigInteger(other) ) );
349+
return context.runtime.newBoolean( value.equals( asBigInteger(other) ) );
335350
}
336351

337352
@JRubyMethod(name="sqr")
@@ -347,23 +362,23 @@ public BN not(final ThreadContext context) {
347362

348363
@JRubyMethod(name="+")
349364
public BN add(final ThreadContext context, IRubyObject other) {
350-
return newBN(context.runtime, value.add(getBigInteger(other)));
365+
return newBN(context.runtime, value.add(asBigInteger(other)));
351366
}
352367

353368
@JRubyMethod(name="-")
354369
public BN sub(final ThreadContext context, IRubyObject other) {
355-
return newBN(context.runtime, value.subtract(getBigInteger(other)));
370+
return newBN(context.runtime, value.subtract(asBigInteger(other)));
356371
}
357372

358373
@JRubyMethod(name="*")
359374
public BN mul(final ThreadContext context, IRubyObject other) {
360-
return newBN(context.runtime, value.multiply(getBigInteger(other)));
375+
return newBN(context.runtime, value.multiply(asBigInteger(other)));
361376
}
362377

363378
@JRubyMethod(name="%")
364379
public BN mod(final ThreadContext context, IRubyObject other) {
365380
try {
366-
return newBN(context.runtime, value.mod(getBigInteger(other)));
381+
return newBN(context.runtime, value.mod(asBigInteger(other)));
367382
}
368383
catch (ArithmeticException e) {
369384
throw context.runtime.newZeroDivisionError();
@@ -374,7 +389,7 @@ public BN mod(final ThreadContext context, IRubyObject other) {
374389
public IRubyObject div(final ThreadContext context, IRubyObject other) {
375390
final Ruby runtime = context.runtime;
376391
try {
377-
BigInteger[] result = value.divideAndRemainder(getBigInteger(other));
392+
BigInteger[] result = value.divideAndRemainder(asBigInteger(other));
378393
return runtime.newArray(newBN(runtime, result[0]), newBN(runtime, result[1]));
379394
}
380395
catch (ArithmeticException e) {
@@ -384,17 +399,17 @@ public IRubyObject div(final ThreadContext context, IRubyObject other) {
384399

385400
@JRubyMethod(name="&")
386401
public BN and(final ThreadContext context, IRubyObject other) {
387-
return newBN(context.runtime, value.and(getBigInteger(other)));
402+
return newBN(context.runtime, value.and(asBigInteger(other)));
388403
}
389404

390405
@JRubyMethod(name="|")
391406
public BN or(final ThreadContext context, IRubyObject other) {
392-
return newBN(context.runtime, value.or(getBigInteger(other)));
407+
return newBN(context.runtime, value.or(asBigInteger(other)));
393408
}
394409

395410
@JRubyMethod(name="^")
396411
public BN xor(final ThreadContext context, IRubyObject other) {
397-
return newBN(context.runtime, value.xor(getBigInteger(other)));
412+
return newBN(context.runtime, value.xor(asBigInteger(other)));
398413
}
399414

400415
@JRubyMethod(name="**")
@@ -439,13 +454,13 @@ else if ( other instanceof RubyBignum ) { // inherently too big
439454

440455
@JRubyMethod(name="gcd")
441456
public BN gcd(final ThreadContext context, IRubyObject other) {
442-
return newBN(context.runtime, value.gcd(getBigInteger(other)));
457+
return newBN(context.runtime, value.gcd(asBigInteger(other)));
443458
}
444459

445460
@JRubyMethod(name="mod_sqr")
446461
public BN mod_sqr(final ThreadContext context, IRubyObject other) {
447462
try {
448-
return newBN(context.runtime, value.modPow(TWO, getBigInteger(other)));
463+
return newBN(context.runtime, value.modPow(TWO, asBigInteger(other)));
449464
}
450465
catch (ArithmeticException e) {
451466
throw context.runtime.newZeroDivisionError();
@@ -455,7 +470,7 @@ public BN mod_sqr(final ThreadContext context, IRubyObject other) {
455470
@JRubyMethod(name="mod_inverse")
456471
public BN mod_inverse(final ThreadContext context, IRubyObject other) {
457472
try {
458-
return newBN(context.runtime, value.modInverse(getBigInteger(other)));
473+
return newBN(context.runtime, value.modInverse(asBigInteger(other)));
459474
}
460475
catch (ArithmeticException e) {
461476
throw context.runtime.newZeroDivisionError();
@@ -465,7 +480,7 @@ public BN mod_inverse(final ThreadContext context, IRubyObject other) {
465480
@JRubyMethod(name="mod_add")
466481
public BN mod_add(final ThreadContext context, IRubyObject other, IRubyObject mod) {
467482
try {
468-
return newBN(context.runtime, value.add(getBigInteger(other)).mod(getBigInteger(mod)));
483+
return newBN(context.runtime, value.add(asBigInteger(other)).mod(asBigInteger(mod)));
469484
}
470485
catch (ArithmeticException e) {
471486
throw context.runtime.newZeroDivisionError();
@@ -475,7 +490,7 @@ public BN mod_add(final ThreadContext context, IRubyObject other, IRubyObject mo
475490
@JRubyMethod(name="mod_sub")
476491
public BN mod_sub(final ThreadContext context, IRubyObject other, IRubyObject mod) {
477492
try {
478-
return newBN(context.runtime, value.subtract(getBigInteger(other)).mod(getBigInteger(mod)));
493+
return newBN(context.runtime, value.subtract(asBigInteger(other)).mod(asBigInteger(mod)));
479494
}
480495
catch (ArithmeticException e) {
481496
throw context.runtime.newZeroDivisionError();
@@ -485,7 +500,7 @@ public BN mod_sub(final ThreadContext context, IRubyObject other, IRubyObject mo
485500
@JRubyMethod(name="mod_mul")
486501
public BN mod_mul(final ThreadContext context, IRubyObject other, IRubyObject mod) {
487502
try {
488-
return newBN(context.runtime, value.multiply(getBigInteger(other)).mod(getBigInteger(mod)));
503+
return newBN(context.runtime, value.multiply(asBigInteger(other)).mod(asBigInteger(mod)));
489504
}
490505
catch (ArithmeticException e) {
491506
throw context.runtime.newZeroDivisionError();
@@ -495,7 +510,7 @@ public BN mod_mul(final ThreadContext context, IRubyObject other, IRubyObject mo
495510
@JRubyMethod(name="mod_exp")
496511
public BN mod_exp(final ThreadContext context, IRubyObject other, IRubyObject mod) {
497512
try {
498-
return newBN(context.runtime, value.modPow(getBigInteger(other), getBigInteger(mod)));
513+
return newBN(context.runtime, value.modPow(asBigInteger(other), asBigInteger(mod)));
499514
}
500515
catch (ArithmeticException e) {
501516
throw context.runtime.newZeroDivisionError();
@@ -657,8 +672,8 @@ public static IRubyObject generate_prime(IRubyObject recv, IRubyObject[] args) {
657672
int argc = Arity.checkArgumentCount(runtime, args, 1, 4);
658673
int bits = RubyNumeric.num2int(args[0]);
659674
boolean safe = argc > 1 ? args[1] != runtime.getFalse() : true;
660-
BigInteger add = argc > 2 ? getBigInteger(args[2]) : null;
661-
BigInteger rem = argc > 3 ? getBigInteger(args[3]) : null;
675+
BigInteger add = argc > 2 ? asBigInteger(args[2]) : null;
676+
BigInteger rem = argc > 3 ? asBigInteger(args[3]) : null;
662677
if (bits < 3) {
663678
if (safe) throw runtime.newArgumentError("bits < 3");
664679
if (bits < 2) throw runtime.newArgumentError("bits < 2");
@@ -794,12 +809,12 @@ public static BigInteger getRandomBI(int bits, int top, boolean bottom, Random r
794809

795810
@JRubyMethod(name = "rand_range", meta = true)
796811
public static IRubyObject rand_range(IRubyObject recv, IRubyObject arg) {
797-
return randomValueInRange(recv.getRuntime(), getBigInteger(arg), getSecureRandom());
812+
return randomValueInRange(recv.getRuntime(), asBigInteger(arg), getSecureRandom());
798813
}
799814

800815
@JRubyMethod(name = "pseudo_rand_range", meta = true)
801816
public static IRubyObject pseudo_rand_range(IRubyObject recv, IRubyObject arg) {
802-
return randomValueInRange(recv.getRuntime(), getBigInteger(arg), getRandom());
817+
return randomValueInRange(recv.getRuntime(), asBigInteger(arg), getRandom());
803818
}
804819

805820
private static BN randomValueInRange(Ruby runtime, BigInteger limit, Random random) {
@@ -850,7 +865,7 @@ public static RaiseException newBNError(Ruby runtime, String message) {
850865
return new RaiseException(runtime, runtime.getModule("OpenSSL").getClass("BNError"), message, true);
851866
}
852867

853-
public static BigInteger getBigInteger(final IRubyObject arg) {
868+
public static BigInteger asBigInteger(final IRubyObject arg) {
854869
if ( arg.isNil() ) return null;
855870

856871
if ( arg instanceof RubyInteger ) {
@@ -862,6 +877,15 @@ public static BigInteger getBigInteger(final IRubyObject arg) {
862877
throw arg.getRuntime().newTypeError("Cannot convert into OpenSSL::BN");
863878
}
864879

880+
public static BigInteger asBigInteger(final BN arg) {
881+
return arg.isNil() ? null : arg.value;
882+
}
883+
884+
@Deprecated
885+
public static BigInteger getBigInteger(final IRubyObject arg) {
886+
return asBigInteger(arg);
887+
}
888+
865889
@Override
866890
public Object toJava(Class target) {
867891
if ( target.isAssignableFrom(BigInteger.class) || target == Number.class ) return value;

src/test/ruby/test_bn.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ def test_comparable
2626
assert OpenSSL::BN.include? Comparable
2727
end
2828

29+
def test_cmp
30+
bn1 = OpenSSL::BN.new('1')
31+
bn2 = OpenSSL::BN.new('1')
32+
bn3 = OpenSSL::BN.new('2')
33+
assert_equal(false, bn1 == nil)
34+
assert_equal(true, bn1 != nil)
35+
assert_equal(true, bn1 == bn2)
36+
assert_equal(false, bn1 == bn3)
37+
assert_equal(true, bn1.eql?(bn2))
38+
assert_equal(false, bn1.eql?(bn3))
39+
assert_equal(bn1.hash, bn2.hash)
40+
assert_not_equal(bn3.hash, bn1.hash)
41+
end if RUBY_VERSION >= '2.3'
42+
2943
def test_to_bn
3044
bn = OpenSSL::BN.new('4224')
3145
assert_equal bn, 4224.to_bn

0 commit comments

Comments
 (0)