Skip to content

Commit fbef97c

Browse files
committed
[feat] implement OpenSSL::PKey::EC#to_text (#280)
1 parent 7ca8743 commit fbef97c

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -369,20 +369,22 @@ protected PublicKey tryX509EncodedKey(final Ruby runtime, final KeyFactory keyFa
369369
}
370370

371371
protected static void addSplittedAndFormatted(StringBuilder result, BigInteger value) {
372-
String v = value.toString(16);
373-
if ((v.length() % 2) != 0) {
374-
v = "0" + v;
375-
}
372+
addSplittedAndFormatted(result, value.toString(16));
373+
}
374+
375+
static void addSplittedAndFormatted(StringBuilder result, CharSequence v) {
376+
if ((v.length() % 2) != 0) v = '0' + v.toString();
377+
376378
String sep = "";
377379
for (int i = 0; i < v.length(); i += 2) {
378380
result.append(sep);
379381
if ((i % 30) == 0) {
380382
result.append("\n ");
381383
}
382-
result.append(v.substring(i, i + 2));
384+
result.append(v, i, i + 2);
383385
sep = ":";
384386
}
385-
result.append("\n");
387+
result.append('\n');
386388
}
387389

388390
protected static CipherSpec cipherSpec(final IRubyObject cipher) {

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,31 @@ public RubyString to_pem(ThreadContext context, final IRubyObject[] args) {
688688
}
689689
}
690690

691+
@JRubyMethod
692+
public RubyString to_text() {
693+
StringBuilder result = new StringBuilder();
694+
final ECParameterSpec spec = getParamSpec();
695+
result.append("Private-Key: (").append(spec.getOrder().bitLength()).append(" bit)").append('\n');
696+
697+
if (privateKey != null) {
698+
result.append("priv:");
699+
addSplittedAndFormatted(result, ((ECPrivateKey) privateKey).getS());
700+
}
701+
702+
if (publicKey != null) {
703+
result.append("pub:");
704+
final byte[] pubBytes = encodeUncompressed(getGroup(true).getBitLength(), publicKey.getW());
705+
final StringBuilder hexBytes = new StringBuilder(pubBytes.length * 2);
706+
for (byte b: pubBytes) {
707+
hexBytes.append(Integer.toHexString(Byte.toUnsignedInt(b)));
708+
}
709+
addSplittedAndFormatted(result, hexBytes);
710+
}
711+
result.append("ASN1 OID: ").append(getCurveName()).append('\n');
712+
713+
return RubyString.newString(getRuntime(), result);
714+
}
715+
691716
private enum PointConversion {
692717
COMPRESSED, UNCOMPRESSED, HYBRID;
693718

src/test/ruby/ec/test_ec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ def test_read_pem
3737
#pem = key.to_pem
3838
#puts pem
3939
#assert_equal(pem, OpenSSL::PKey::EC.new(pem).to_pem)
40+
41+
# to_text
42+
text = key.to_text
43+
assert_include text, "Private-Key: (112 bit)\n"
44+
assert_include text, "bb:54:b8:93:a9:51:3c:09:a6:37:f7:2c:95:2e\n"
45+
assert_include text, "ASN1 OID: secp112r1\n"
4046
end
4147

4248
def test_read_pem2

0 commit comments

Comments
 (0)