Skip to content

Commit

Permalink
[refactor] handle some warnings and a safer check
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Jan 9, 2025
1 parent 6a6ce2e commit f176706
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/jruby/ext/openssl/PKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,16 +380,16 @@ protected static void addSplittedAndFormatted(StringBuilder result, BigInteger v
}

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

String sep = "";
char sep = '\0';
for (int i = 0; i < v.length(); i += 2) {
result.append(sep);
if ((i % 30) == 0) {
result.append("\n ");
}
result.append(v, i, i + 2);
sep = ":";
sep = ':';
}
result.append('\n');
}
Expand Down
20 changes: 5 additions & 15 deletions src/main/java/org/jruby/ext/openssl/PKeyEC.java
Original file line number Diff line number Diff line change
Expand Up @@ -785,18 +785,16 @@ public RubyString to_text() {
final ECParameterSpec spec = getParamSpec();
result.append("Private-Key: (").append(spec.getOrder().bitLength()).append(" bit)").append('\n');

if (privateKey != null) {
if (privateKey instanceof ECPrivateKey) {
result.append("priv:");
addSplittedAndFormatted(result, ((ECPrivateKey) privateKey).getS());
}

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

private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
public Group allocate(Ruby runtime, RubyClass klass) { return new Group(runtime, klass); }
};

static void createGroup(final Ruby runtime, final RubyClass EC, final RubyClass OpenSSLError) {
RubyClass Group = EC.defineClassUnder("Group", runtime.getObject(), ALLOCATOR);
RubyClass Group = EC.defineClassUnder("Group", runtime.getObject(), Group::new);

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

private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
public Point allocate(Ruby runtime, RubyClass klass) { return new Point(runtime, klass); }
};

static void createPoint(final Ruby runtime, final RubyClass EC, final RubyClass OpenSSLError) {
RubyClass Point = EC.defineClassUnder("Point", runtime.getObject(), ALLOCATOR);
RubyClass Point = EC.defineClassUnder("Point", runtime.getObject(), Point::new);

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

0 comments on commit f176706

Please sign in to comment.