@@ -233,7 +233,7 @@ internal data class ECKeyData(
233233
234234/* *
235235 * Parses a PEM-encoded EC private key and extracts the private key scalar and public key (x, y) coordinates.
236- * Supports both "EC PRIVATE KEY" and " PRIVATE KEY" PEM formats for P-256 curve keys.
236+ * Supports "EC PRIVATE KEY" PEM formats for P-256 curve keys.
237237 */
238238private fun parseECKeyPem (pem : String ): ECKeyData {
239239 val base64 = pem.replace(" -----BEGIN EC PRIVATE KEY-----" , " " )
@@ -259,15 +259,15 @@ private fun parseECKeyPem(pem: String): ECKeyData {
259259 val remainingBytes = der.size - publicKeyStart
260260 val coordLen = remainingBytes / 2
261261
262- val x = der.copyOfRange(publicKeyStart, publicKeyStart + coordLen).padOrTrimTo32 ()
263- val y = der.copyOfRange(publicKeyStart + coordLen, publicKeyStart + 2 * coordLen).padOrTrimTo32 ()
262+ val x = der.copyOfRange(publicKeyStart, publicKeyStart + coordLen).padTo32 ()
263+ val y = der.copyOfRange(publicKeyStart + coordLen, publicKeyStart + 2 * coordLen).padTo32 ()
264264
265265 return ECKeyData (d, x, y)
266266}
267267
268- private fun ByteArray.padOrTrimTo32 (): ByteArray =
269- if (size >= 32 ) {
270- takeLast( 32 ).toByteArray( )
268+ private fun ByteArray.padTo32 (): ByteArray =
269+ if (size > 32 ) {
270+ error( " Unexpected byte array of size $size ; expected 32 bytes or less " )
271271 } else {
272272 ByteArray (32 - size) + this
273273 }
0 commit comments