Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby-openssl
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: acbfa26d81eb
Choose a base ref
...
head repository: jruby/jruby-openssl
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 67820b9e8aba
Choose a head ref
  • 3 commits
  • 4 files changed
  • 1 contributor

Commits on Nov 2, 2017

  1. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    NeQuissimus Tim Steinbach
    Copy the full SHA
    75242d4 View commit details
  2. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    NeQuissimus Tim Steinbach
    Copy the full SHA
    6e213fd View commit details
  3. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    NeQuissimus Tim Steinbach
    Copy the full SHA
    67820b9 View commit details
Showing with 115 additions and 142 deletions.
  1. +1 −2 Mavenfile
  2. +9 −117 pom.xml
  3. +47 −23 src/main/java/org/jruby/ext/openssl/BN.java
  4. +58 −0 src/test/ruby/test_bn.rb
3 changes: 1 addition & 2 deletions Mavenfile
Original file line number Diff line number Diff line change
@@ -157,8 +157,7 @@ invoker_run_options = {
# 'bc.versions' => supported_bc_versions.join(',')
# end

jruby_1_7_versions = %w{ 1.7.13 1.7.15 1.7.16 1.7.18 1.7.20 1.7.22 1.7.23 }
jruby_1_7_versions += %w{ 1.7.24 1.7.25 1.7.26 1.7.27 }
jruby_1_7_versions = %w{ 1.7.18 1.7.20 1.7.22 1.7.23 1.7.24 1.7.25 1.7.26 1.7.27 }

jruby_1_7_versions.each { |version|

126 changes: 9 additions & 117 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -58,6 +58,7 @@ DO NOT MODIFIY - GENERATED CODE
</distributionManagement>
<properties>
<bc.versions></bc.versions>
<mavengem.wagon.version>0.2.0</mavengem.wagon.version>
<jruby.plugins.version>1.0.10</jruby.plugins.version>
<invoker.skip>${maven.test.skip}</invoker.skip>
<jruby.version>1.7.20</jruby.version>
@@ -121,8 +122,8 @@ DO NOT MODIFIY - GENERATED CODE
</dependencies>
<repositories>
<repository>
<id>rubygems-releases</id>
<url>http://rubygems-proxy.torquebox.org/releases</url>
<id>mavengems</id>
<url>mavengem:https://rubygems.org</url>
</repository>
<repository>
<releases>
@@ -137,6 +138,11 @@ DO NOT MODIFIY - GENERATED CODE
</repositories>
<build>
<extensions>
<extension>
<groupId>org.torquebox.mojo</groupId>
<artifactId>mavengem-wagon</artifactId>
<version>${mavengem.wagon.version}</version>
</extension>
<extension>
<groupId>de.saumya.mojo</groupId>
<artifactId>gem-with-jar-extension</artifactId>
@@ -377,121 +383,6 @@ DO NOT MODIFIY - GENERATED CODE
</plugins>
</build>
<profiles>
<profile>
<id>test-1.7.13</id>
<build>
<plugins>
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>tests-with-different-bc-versions</id>
<goals>
<goal>install</goal>
<goal>run</goal>
</goals>
<configuration>
<projectsDirectory>integration</projectsDirectory>
<pomIncludes>
<pomInclude>*/pom.xml</pomInclude>
</pomIncludes>
<streamLogs>true</streamLogs>
<properties>
<jruby.versions>${jruby.versions}</jruby.versions>
<jruby.modes>${jruby.modes}</jruby.modes>
<jruby.openssl.version>${project.version}</jruby.openssl.version>
<bc.versions>${bc.versions}</bc.versions>
<runit.dir>${runit.dir}</runit.dir>
</properties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<bc.versions>1.54,1.55,1.56,1.57,1.58</bc.versions>
<jruby.modes>1.9,2.0</jruby.modes>
<jruby.versions>1.7.13</jruby.versions>
</properties>
</profile>
<profile>
<id>test-1.7.15</id>
<build>
<plugins>
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<goals>
<goal>install</goal>
<goal>run</goal>
</goals>
<configuration>
<projectsDirectory>integration</projectsDirectory>
<pomIncludes>
<pomInclude>*/pom.xml</pomInclude>
</pomIncludes>
<streamLogs>true</streamLogs>
<properties>
<jruby.versions>${jruby.versions}</jruby.versions>
<jruby.modes>${jruby.modes}</jruby.modes>
<jruby.openssl.version>${project.version}</jruby.openssl.version>
<bc.versions>${bc.versions}</bc.versions>
<runit.dir>${runit.dir}</runit.dir>
</properties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<bc.versions>1.54,1.55,1.56,1.57,1.58</bc.versions>
<jruby.modes>1.9,2.0</jruby.modes>
<jruby.versions>1.7.15</jruby.versions>
</properties>
</profile>
<profile>
<id>test-1.7.16</id>
<build>
<plugins>
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<goals>
<goal>install</goal>
<goal>run</goal>
</goals>
<configuration>
<projectsDirectory>integration</projectsDirectory>
<pomIncludes>
<pomInclude>*/pom.xml</pomInclude>
</pomIncludes>
<streamLogs>true</streamLogs>
<properties>
<jruby.versions>${jruby.versions}</jruby.versions>
<jruby.modes>${jruby.modes}</jruby.modes>
<jruby.openssl.version>${project.version}</jruby.openssl.version>
<bc.versions>${bc.versions}</bc.versions>
<runit.dir>${runit.dir}</runit.dir>
</properties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<bc.versions>1.54,1.55,1.56,1.57,1.58</bc.versions>
<jruby.modes>1.9,2.0</jruby.modes>
<jruby.versions>1.7.16</jruby.versions>
</properties>
</profile>
<profile>
<id>test-1.7.18</id>
<build>
@@ -501,6 +392,7 @@ DO NOT MODIFIY - GENERATED CODE
<version>1.8</version>
<executions>
<execution>
<id>tests-with-different-bc-versions</id>
<goals>
<goal>install</goal>
<goal>run</goal>
70 changes: 47 additions & 23 deletions src/main/java/org/jruby/ext/openssl/BN.java
Original file line number Diff line number Diff line change
@@ -89,10 +89,6 @@ static BN newInstance(final Ruby runtime, BigInteger value) {
return new BN(runtime, value != null ? value : BigInteger.ZERO);
}

//static BN newInstance(final Ruby runtime, long value) {
// return new BN(runtime, BigInteger.valueOf(value));
//}

public static void createBN(final Ruby runtime, final RubyModule OpenSSL) {
final RubyClass OpenSSLError = OpenSSL.getClass("OpenSSLError");
OpenSSL.defineClassUnder("BNError", OpenSSLError, OpenSSLError.getAllocator());
@@ -119,8 +115,7 @@ public final BigInteger getValue() {
}

@JRubyMethod(name="initialize", required=1, optional=1, visibility = Visibility.PRIVATE)
public IRubyObject initialize(final ThreadContext context,
final IRubyObject[] args) {
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
final Ruby runtime = context.runtime;
if (this.value != BigInteger.ZERO) { // already initialized
throw newBNError(runtime, "illegal initialization");
@@ -130,14 +125,7 @@ public IRubyObject initialize(final ThreadContext context,
final RubyString str = args[0].asString();
switch (base) {
case 0:
final byte[] bytes = str.getBytes(); final int signum;
if ( ( bytes[0] & 0x80 ) != 0 ) {
bytes[0] &= 0x7f; signum = -1;
}
else {
signum = 1;
}
this.value = new BigInteger(signum, bytes);
this.value = initBigIntegerMPI(runtime, str);
break;
case 2:
// this seems wrong to me, but is the behavior of the
@@ -151,20 +139,46 @@ public IRubyObject initialize(final ThreadContext context,
case 10:
case 16:
// here, the ASCII-encoded decimal or hex string is used
try {
this.value = new BigInteger(str.toString(), base);
break;
} catch (NumberFormatException e) {
throw runtime.newArgumentError("value " + str + " is not legal for radix " + base);
}
this.value = initBigIntegerBase(runtime, str, base);
break;
default:
throw runtime.newArgumentError("illegal radix: " + base);
}
return this;
}

private static BigInteger initBigIntegerMPI(final Ruby runtime, RubyString str) {
final ByteList byteList = str.getByteList();
final int off = byteList.getBegin();
final byte[] b = byteList.getUnsafeBytes();

long len = ((b[off] & 0xFFl) << 24) | ((b[off+1] & 0xFF) << 16) | ((b[off+2] & 0xFF) << 8) | (b[off+3] & 0xFF);
final byte[] bytes = new byte[(int) len];
System.arraycopy(b, off + 4, bytes, 0, bytes.length);

final int signum;
if ( (bytes[0] & 0x80) == 0x80 ) {
signum = -1; bytes[0] &= 0x7f;
}
else {
signum = +1;
}
return new BigInteger(signum, bytes);

}

private static BigInteger initBigIntegerBase(final Ruby runtime, RubyString str, final int base) {
// here, the ASCII-encoded decimal or hex string is used
try {
return new BigInteger(str.toString(), base);
}
catch (NumberFormatException e) {
throw runtime.newArgumentError("value " + str + " is not legal for radix " + base);
}
}

@Override
public synchronized IRubyObject initialize_copy(final IRubyObject that) {
public IRubyObject initialize_copy(final IRubyObject that) {
super.initialize_copy(that);
if ( this != that ) this.value = ((BN) that).value;
return this;
@@ -342,8 +356,18 @@ public IRubyObject ucmp(final ThreadContext context, IRubyObject other) {
return context.runtime.newFixnum( value.abs().compareTo( asBigInteger(other).abs() ) );
}

@JRubyMethod(name={"eql?", "==", "==="})
public RubyBoolean eql_p(final ThreadContext context, IRubyObject other) {
@JRubyMethod(name = "eql?")
public IRubyObject eql_p(ThreadContext context, IRubyObject other) {
return context.runtime.newBoolean(eql(other));
}

@Override
public boolean eql(IRubyObject other) {
return equals(other);
}

@JRubyMethod(name = "==")
public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
return context.runtime.newBoolean( value.equals( asBigInteger(other) ) );
}

58 changes: 58 additions & 0 deletions src/test/ruby/test_bn.rb
Original file line number Diff line number Diff line change
@@ -52,9 +52,67 @@ def test_to_bn
assert_equal bn, 1234567890123456789012345678901234567890.to_bn
end

def test_comparison
e1 = OpenSSL::BN.new(999.to_s(16), 16)
e3 = OpenSSL::BN.new((2**107-1).to_s(16), 16)
assert_equal(false, e1 == nil)
assert_equal(false, e1 == -999)
assert_equal(true, e1 == 999)
assert_equal(true, e1 == 999.to_bn)
assert_equal(false, e1.eql?(nil))
assert_equal(false, e1.eql?(999))
assert_equal(true, e1.eql?(999.to_bn))
assert_equal(e1.hash, 999.to_bn.hash)
assert_not_equal(e1.hash, e3.hash)
assert_equal(0, e1.cmp(999))
assert_equal(1, e1.cmp(-999))
assert_equal(0, e1.ucmp(999))
assert_equal(0, e1.ucmp(-999))
assert_instance_of(String, e1.hash.to_s)
end

def test_to_java
assert_equal java.lang.Integer.new(42), OpenSSL::BN.new('42').to_java(:int)
assert_equal java.math.BigInteger.valueOf(24), OpenSSL::BN.new('24').to_java
end if defined? JRUBY_VERSION

def test_new_str
e1 = OpenSSL::BN.new(999.to_s(16), 16) # OpenSSL::BN.new(str, 16) must be most stable
e2 = OpenSSL::BN.new((2**107-1).to_s(16), 16)
assert_equal(e1, OpenSSL::BN.new("999"))
assert_equal(e2, OpenSSL::BN.new((2**107-1).to_s))
assert_equal(e1, OpenSSL::BN.new("999", 10))
assert_equal(e2, OpenSSL::BN.new((2**107-1).to_s, 10))
assert_equal(e1, OpenSSL::BN.new("\x03\xE7", 2))
assert_equal(e2, OpenSSL::BN.new("\a\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 2))
assert_equal(e1, OpenSSL::BN.new("\x00\x00\x00\x02\x03\xE7", 0))
assert_equal(e2, OpenSSL::BN.new("\x00\x00\x00\x0E\a\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 0))
end

def test_new_bn
e1 = OpenSSL::BN.new(999.to_s(16), 16)
e2 = OpenSSL::BN.new((2**107-1).to_s(16), 16)
assert_equal(e1, OpenSSL::BN.new(e1))
assert_equal(e2, OpenSSL::BN.new(e2))
end

def test_new_integer
assert_equal(999.to_bn, OpenSSL::BN.new(999))
assert_equal((2 ** 107 - 1).to_bn, OpenSSL::BN.new(2 ** 107 - 1))
assert_equal(-999.to_bn, OpenSSL::BN.new(-999))
assert_equal((-(2 ** 107 - 1)).to_bn, OpenSSL::BN.new(-(2 ** 107 - 1)))
end

def test_to_bn
e1 = OpenSSL::BN.new(999.to_s(16), 16)
e2 = OpenSSL::BN.new((2**107-1).to_s(16), 16)
assert_equal(e1, 999.to_bn)
assert_equal(e2, (2**107-1).to_bn)
end

def test_prime_p
assert_equal(true, OpenSSL::BN.new((2 ** 107 - 1).to_s(16), 16).prime?)
assert_equal(true, OpenSSL::BN.new((2 ** 127 - 1).to_s(16), 16).prime?(1))
end

end