commit 7db6c2a92931d72d35216d923bc02f3a9af005df
parent 43fa22d6322e903a431502945ae16676a1a59d5b
Author: Florian Dold <florian.dold@gmail.com>
Date: Wed, 30 Oct 2019 20:47:23 +0100
crypto util test
Diffstat:
2 files changed, 60 insertions(+), 29 deletions(-)
diff --git a/sandbox/src/test/kotlin/CryptoUtilTest.kt b/sandbox/src/test/kotlin/CryptoUtilTest.kt
@@ -0,0 +1,59 @@
+/*
+ * 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 org.junit.Test
+import java.security.KeyPairGenerator
+import java.security.interfaces.RSAPrivateCrtKey
+import kotlin.test.assertEquals
+import kotlin.test.assertTrue
+
+class CryptoUtilTest {
+
+ @Test
+ fun loadFromModulusAndExponent() {
+ val keyPair = CryptoUtil.generateRsaKeyPair(1024)
+ val pub2 = CryptoUtil.loadRsaPublicKeyFromComponents(
+ keyPair.public.modulus.toByteArray(),
+ keyPair.public.publicExponent.toByteArray()
+ )
+ assertEquals(keyPair.public, pub2)
+ }
+
+ @Test
+ fun keyGeneration() {
+ val gen: KeyPairGenerator = KeyPairGenerator.getInstance("RSA")
+ gen.initialize(2048)
+ val pair = gen.genKeyPair()
+ println(pair.private)
+ assertTrue(pair.private is RSAPrivateCrtKey)
+ }
+
+ @Test
+ fun testCryptoUtilBasics() {
+ val keyPair = CryptoUtil.generateRsaKeyPair(1024)
+ val encodedPriv = keyPair.private.encoded
+ val encodedPub = keyPair.public.encoded
+ val otherKeyPair =
+ RsaCrtKeyPair(CryptoUtil.loadRsaPrivateKey(encodedPriv), CryptoUtil.loadRsaPublicKey(encodedPub))
+ assertEquals(keyPair.private, otherKeyPair.private)
+ assertEquals(keyPair.public, otherKeyPair.public)
+ }
+}
+\ No newline at end of file
diff --git a/sandbox/src/test/kotlin/GeneratePrivateKeyTest.kt b/sandbox/src/test/kotlin/GeneratePrivateKeyTest.kt
@@ -1,28 +0,0 @@
-package tech.libeufin.sandbox
-
-import org.junit.Test
-import junit.framework.TestCase.assertTrue
-import org.jetbrains.exposed.sql.transactions.transaction
-import org.junit.Before
-import tech.libeufin.sandbox.db.EbicsBankPrivateKey
-import tech.libeufin.sandbox.db.dbCreateTables
-
-class GeneratePrivateKeyTest {
-
- @Before
- fun setUp() {
- dbCreateTables()
- }
-
- @Test
- fun loadOrGeneratePrivateKey() {
-
- getOrMakePrivateKey()
-
- assertTrue(
- transaction {
- EbicsBankPrivateKey.findById(1)
- } != null
- )
- }
-}
-\ No newline at end of file