[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[libeufin] 02/02: crypto utils
From: |
gnunet |
Subject: |
[libeufin] 02/02: crypto utils |
Date: |
Wed, 30 Oct 2019 12:36:31 +0100 |
This is an automated email from the git hooks/post-receive script.
dold pushed a commit to branch master
in repository libeufin.
commit 43fa22d6322e903a431502945ae16676a1a59d5b
Author: Florian Dold <address@hidden>
AuthorDate: Wed Oct 30 12:36:24 2019 +0100
crypto utils
---
sandbox/src/main/kotlin/CryptoUtil.kt | 75 +++++++++++++++++++++++++++++++++++
sandbox/src/test/kotlin/RsaTest.kt | 31 ---------------
2 files changed, 75 insertions(+), 31 deletions(-)
diff --git a/sandbox/src/main/kotlin/CryptoUtil.kt
b/sandbox/src/main/kotlin/CryptoUtil.kt
new file mode 100644
index 0000000..fb72d09
--- /dev/null
+++ 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
deleted file mode 100644
index 836d88d..0000000
--- a/sandbox/src/test/kotlin/RsaTest.kt
+++ /dev/null
@@ -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
--
To stop receiving notification emails like this one, please contact
address@hidden.