libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit 43fa22d6322e903a431502945ae16676a1a59d5b
parent 2d29bdc42bdac63e3aeda5bce69f44329f5273c7
Author: Florian Dold <florian.dold@gmail.com>
Date:   Wed, 30 Oct 2019 12:36:24 +0100

crypto utils

Diffstat:
Asandbox/src/main/kotlin/CryptoUtil.kt | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dsandbox/src/test/kotlin/RsaTest.kt | 32--------------------------------
2 files changed, 76 insertions(+), 32 deletions(-)

diff --git a/sandbox/src/main/kotlin/CryptoUtil.kt b/sandbox/src/main/kotlin/CryptoUtil.kt @@ -0,0 +1,75 @@ +/* + * This file is part of LibEuFin. + * Copyright (C) 2019 Stanisci and Dold. + + * LibEuFin is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation; either version 3, or + * (at your option) any later version. + + * LibEuFin is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General + * Public License for more details. + + * You should have received a copy of the GNU Affero General Public + * License along with LibEuFin; see the file COPYING. If not, see + * <http://www.gnu.org/licenses/> + */ + +package tech.libeufin.sandbox + +import java.lang.Exception +import java.security.KeyFactory +import java.security.KeyPairGenerator +import java.security.interfaces.RSAPrivateCrtKey +import java.security.interfaces.RSAPublicKey +import java.security.spec.PKCS8EncodedKeySpec +import java.security.spec.RSAPublicKeySpec +import java.security.spec.X509EncodedKeySpec + +/** + * RSA key pair. + */ +data class RsaCrtKeyPair(val private: RSAPrivateCrtKey, val public: RSAPublicKey) + +/** + * Helpers for dealing with crypographic operations in EBICS / LibEuFin. + */ +class CryptoUtil { + companion object { + fun loadRsaPrivateKey(encodedPrivateKey: ByteArray): RSAPrivateCrtKey { + val spec = PKCS8EncodedKeySpec(encodedPrivateKey) + val priv = KeyFactory.getInstance("RSA").generatePrivate(spec) + if (priv !is RSAPrivateCrtKey) + throw Exception("wrong encoding") + return priv + } + fun loadRsaPublicKey(encodedPublicKey: ByteArray): RSAPublicKey { + val spec = X509EncodedKeySpec(encodedPublicKey) + val pub = KeyFactory.getInstance("RSA").generatePublic(spec) + if (pub !is RSAPublicKey) + throw Exception("wrong encoding") + return pub + } + fun getRsaPublicFromPrivate(rsaPrivateCrtKey: RSAPrivateCrtKey): RSAPublicKey { + val spec = RSAPublicKeySpec(rsaPrivateCrtKey.modulus, rsaPrivateCrtKey.publicExponent) + val pub = KeyFactory.getInstance("RSA").generatePublic(spec) + if (pub !is RSAPublicKey) + throw Exception("wrong encoding") + return pub + } + fun generateRsaKeyPair(nbits: Int): RsaCrtKeyPair { + val gen = KeyPairGenerator.getInstance("RSA") + gen.initialize(nbits) + val pair = gen.genKeyPair() + val priv = pair.private + val pub = pair.public + if (priv !is RSAPrivateCrtKey) + throw Exception("key generation failed") + if (pub !is RSAPublicKey) + throw Exception("key generation failed") + return RsaCrtKeyPair(priv, pub) + } + } +} +\ No newline at end of file diff --git a/sandbox/src/test/kotlin/RsaTest.kt b/sandbox/src/test/kotlin/RsaTest.kt @@ -1,31 +0,0 @@ -package tech.libeufin.sandbox - -import org.junit.Test -import java.math.BigInteger -import java.util.* - -class RsaTest { - - val publicModulus = BigInteger("65537") - val publicExponent = BigInteger(512, Random()) - - @Test - fun loadFromModulusAndExponent() { - val key = loadRsaPublicKey(publicExponent.toByteArray(), publicModulus.toByteArray()) - println(key.toString()) - } - - /** - * Values generating helper. - */ - @Test - fun getBase64Values() { - - println( - "Modulus: ${Base64.getEncoder().encodeToString(publicModulus.toByteArray())}" - ) - println( - "Exponent: ${Base64.getEncoder().encodeToString(publicExponent.toByteArray())}" - ) - } -} -\ No newline at end of file