commit 8325a033326725fcf193f0dae9c04e45c5ec5f97
parent e9ab70b4e0e598baeb4b4033f407640d5236c95c
Author: Florian Dold <florian.dold@gmail.com>
Date: Thu, 30 Jan 2020 16:43:59 +0100
fix up key format and key hash
Diffstat:
3 files changed, 18 insertions(+), 8 deletions(-)
diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt
@@ -334,12 +334,12 @@ fun main() {
)
hostID = subscriber.hostID
userIdLine = subscriber.userID
- esExponentLine = signPubTmp.publicExponent.toByteArray().toHexString()
- esModulusLine = signPubTmp.modulus.toByteArray().toHexString()
- encExponentLine = encPubTmp.publicExponent.toByteArray().toHexString()
- encModulusLine = encPubTmp.modulus.toByteArray().toHexString()
- authExponentLine = authPubTmp.publicExponent.toByteArray().toHexString()
- authModulusLine = authPubTmp.modulus.toByteArray().toHexString()
+ esExponentLine = signPubTmp.publicExponent.toUnsignedHexString()
+ esModulusLine = signPubTmp.modulus.toUnsignedHexString()
+ encExponentLine = encPubTmp.publicExponent.toUnsignedHexString()
+ encModulusLine = encPubTmp.modulus.toUnsignedHexString()
+ authExponentLine = authPubTmp.publicExponent.toUnsignedHexString()
+ authModulusLine = authPubTmp.modulus.toUnsignedHexString()
esKeyHashLine = CryptoUtil.getEbicsPublicKeyHash(signPubTmp).toHexString()
encKeyHashLine = CryptoUtil.getEbicsPublicKeyHash(encPubTmp).toHexString()
authKeyHashLine = CryptoUtil.getEbicsPublicKeyHash(authPubTmp).toHexString()
diff --git a/util/src/main/kotlin/CryptoUtil.kt b/util/src/main/kotlin/CryptoUtil.kt
@@ -121,9 +121,9 @@ object CryptoUtil {
*/
fun getEbicsPublicKeyHash(publicKey: RSAPublicKey): ByteArray {
val keyBytes = ByteArrayOutputStream()
- keyBytes.writeBytes(publicKey.publicExponent.toByteArray().toHexString().toByteArray())
+ keyBytes.writeBytes(publicKey.publicExponent.toUnsignedHexString().toByteArray())
keyBytes.write(' '.toInt())
- keyBytes.writeBytes(publicKey.modulus.toByteArray().toHexString().toByteArray())
+ keyBytes.writeBytes(publicKey.modulus.toUnsignedHexString().toByteArray())
val digest = MessageDigest.getInstance("SHA-256")
return digest.digest(keyBytes.toByteArray())
}
diff --git a/util/src/main/kotlin/strings.kt b/util/src/main/kotlin/strings.kt
@@ -1,6 +1,8 @@
package tech.libeufin.util
+import java.math.BigInteger
import java.util.*
+
fun ByteArray.toHexString() : String {
return this.joinToString("") {
java.lang.String.format("%02x", it)
@@ -14,3 +16,10 @@ fun bytesToBase64(bytes: ByteArray): String {
fun base64ToBytes(encoding: String): ByteArray {
return Base64.getDecoder().decode(encoding)
}
+
+fun BigInteger.toUnsignedHexString(): String {
+ val signedValue = this.toByteArray()
+ require(signedValue[0] == 0.toByte()) { "value must be a positive BigInteger" }
+ val bytes = Arrays.copyOfRange(signedValue, 1, signedValue.size)
+ return bytes.toHexString()
+}
+\ No newline at end of file