Skip to content

Commit f176706

Browse files
committed
[refactor] handle some warnings and a safer check
1 parent 6a6ce2e commit f176706

File tree

2 files changed

+8
-18
lines changed

2 files changed

+8
-18
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,16 +380,16 @@ protected static void addSplittedAndFormatted(StringBuilder result, BigInteger v
380380
}
381381

382382
static void addSplittedAndFormatted(StringBuilder result, CharSequence v) {
383-
if ((v.length() % 2) != 0) v = '0' + v.toString();
383+
if ((v.length() % 2) != 0) v = "0" + v;
384384

385-
String sep = "";
385+
char sep = '\0';
386386
for (int i = 0; i < v.length(); i += 2) {
387387
result.append(sep);
388388
if ((i % 30) == 0) {
389389
result.append("\n ");
390390
}
391391
result.append(v, i, i + 2);
392-
sep = ":";
392+
sep = ':';
393393
}
394394
result.append('\n');
395395
}

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -785,18 +785,16 @@ public RubyString to_text() {
785785
final ECParameterSpec spec = getParamSpec();
786786
result.append("Private-Key: (").append(spec.getOrder().bitLength()).append(" bit)").append('\n');
787787

788-
if (privateKey != null) {
788+
if (privateKey instanceof ECPrivateKey) {
789789
result.append("priv:");
790790
addSplittedAndFormatted(result, ((ECPrivateKey) privateKey).getS());
791791
}
792792

793793
if (publicKey != null) {
794794
result.append("pub:");
795-
final byte[] pubBytes = encodeUncompressed(getGroup(true).getBitLength(), publicKey.getW());
795+
final byte[] pubBytes = encodeCompressed(publicKey.getW());
796796
final StringBuilder hexBytes = new StringBuilder(pubBytes.length * 2);
797-
for (byte b: pubBytes) {
798-
hexBytes.append(Integer.toHexString(Byte.toUnsignedInt(b)));
799-
}
797+
for (byte b: pubBytes) hexBytes.append(Integer.toHexString(Byte.toUnsignedInt(b)));
800798
addSplittedAndFormatted(result, hexBytes);
801799
}
802800
result.append("ASN1 OID: ").append(getCurveName()).append('\n');
@@ -815,12 +813,8 @@ String toRubyString() {
815813
@JRubyClass(name = "OpenSSL::PKey::EC::Group")
816814
public static final class Group extends RubyObject {
817815

818-
private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
819-
public Group allocate(Ruby runtime, RubyClass klass) { return new Group(runtime, klass); }
820-
};
821-
822816
static void createGroup(final Ruby runtime, final RubyClass EC, final RubyClass OpenSSLError) {
823-
RubyClass Group = EC.defineClassUnder("Group", runtime.getObject(), ALLOCATOR);
817+
RubyClass Group = EC.defineClassUnder("Group", runtime.getObject(), Group::new);
824818

825819
// OpenSSL::PKey::EC::Group::Error
826820
Group.defineClassUnder("Error", OpenSSLError, OpenSSLError.getAllocator());
@@ -1006,12 +1000,8 @@ static PointConversion parse_point_conversion_form(final Ruby runtime, final IRu
10061000
@JRubyClass(name = "OpenSSL::PKey::EC::Point")
10071001
public static final class Point extends RubyObject {
10081002

1009-
private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
1010-
public Point allocate(Ruby runtime, RubyClass klass) { return new Point(runtime, klass); }
1011-
};
1012-
10131003
static void createPoint(final Ruby runtime, final RubyClass EC, final RubyClass OpenSSLError) {
1014-
RubyClass Point = EC.defineClassUnder("Point", runtime.getObject(), ALLOCATOR);
1004+
RubyClass Point = EC.defineClassUnder("Point", runtime.getObject(), Point::new);
10151005

10161006
// OpenSSL::PKey::EC::Point::Error
10171007
Point.defineClassUnder("Error", OpenSSLError, OpenSSLError.getAllocator());

0 commit comments

Comments
 (0)