Skip to content

Commit 2cc7279

Browse files
committed
allow decoding EC private key
1 parent 5ee32dc commit 2cc7279

File tree

1 file changed

+6
-1
lines changed
  • android/src/main/java/com/github/emotokcak/reactnative/mqtt

1 file changed

+6
-1
lines changed

android/src/main/java/com/github/emotokcak/reactnative/mqtt/PEMLoader.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,17 @@ object PEMLoader {
5151
*/
5252
@JvmStatic
5353
fun loadPrivateKeyFromString(pem: String): PrivateKey {
54+
val isRSA = pem.startsWith("-----BEGIN RSA PRIVATE KEY-----")
5455
val pemContents = pem
5556
.replace("-----BEGIN RSA PRIVATE KEY-----", "")
5657
.replace("-----END RSA PRIVATE KEY-----", "")
58+
.replace("-----BEGIN EC PRIVATE KEY-----", "")
59+
.replace("-----END EC PRIVATE KEY-----", "")
60+
.replace("-----BEGIN PRIVATE KEY-----", "")
61+
.replace("-----END PRIVATE KEY-----", "")
5762
val data = Base64.getMimeDecoder().decode(pemContents)
5863
val keySpec = PKCS8EncodedKeySpec(data)
59-
val keyFactory = KeyFactory.getInstance("RSA")
64+
val keyFactory = KeyFactory.getInstance(if (isRSA) "RSA" else "EC")
6065
return keyFactory.generatePrivate(keySpec)
6166
}
6267
}

0 commit comments

Comments
 (0)