fynmanoj commented on a change in pull request #1032:
URL: https://github.com/apache/fineract/pull/1032#discussion_r439139383



##########
File path: 
fineract-provider/src/main/java/org/apache/fineract/infrastructure/crypt/utils/RSAEncryptionUtils.java
##########
@@ -0,0 +1,110 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.infrastructure.crypt.utils;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import java.security.InvalidKeyException;
+import java.security.KeyFactory;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
+import java.security.PublicKey;
+import java.security.SecureRandom;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.util.Random;
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.NoSuchPaddingException;
+import javax.xml.bind.DatatypeConverter;
+import org.apache.commons.codec.binary.Hex;
+import 
org.apache.fineract.infrastructure.core.exception.GeneralPlatformDomainRuleException;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.infrastructure.crypt.domain.EncryptionKeys;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author manoj
+ */
+@Component
+public class RSAEncryptionUtils {
+    private final static Logger LOG = 
LoggerFactory.getLogger(RSAEncryptionUtils.class);
+
+    public EncryptionKeys generateKeys(){
+        KeyPairGenerator keyPairGenerator;
+        try {
+            keyPairGenerator = KeyPairGenerator.getInstance("RSA");
+
+            KeyPair keyPair = keyPairGenerator.generateKeyPair();
+            PublicKey publicKey = keyPair.getPublic();
+            PrivateKey privateKey = keyPair.getPrivate();
+            byte[] publicKeyAsBytes = publicKey.getEncoded();
+            byte[] privateKeyAsBytes = privateKey.getEncoded();
+
+            return new EncryptionKeys(privateKeyAsBytes, publicKeyAsBytes, 
DateUtils.getLocalDateTimeOfTenant(), generateVersionString());
+
+        } catch (NoSuchAlgorithmException e) {
+           LOG.error("Error occurred", e);
+        }
+        throw new 
GeneralPlatformDomainRuleException("error.msg.error.in.generating.keys", "Error 
in generating keys");

Review comment:
       Changed this.  I was just being sure that the method return something or 
an exception(do or die).Yet you are right, there is better ways to do it rather 
than sending panic

##########
File path: 
fineract-provider/src/main/java/org/apache/fineract/infrastructure/crypt/utils/RSAEncryptionUtils.java
##########
@@ -0,0 +1,110 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.infrastructure.crypt.utils;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import java.security.InvalidKeyException;
+import java.security.KeyFactory;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
+import java.security.PublicKey;
+import java.security.SecureRandom;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.util.Random;
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.NoSuchPaddingException;
+import javax.xml.bind.DatatypeConverter;
+import org.apache.commons.codec.binary.Hex;
+import 
org.apache.fineract.infrastructure.core.exception.GeneralPlatformDomainRuleException;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.infrastructure.crypt.domain.EncryptionKeys;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author manoj
+ */
+@Component
+public class RSAEncryptionUtils {
+    private final static Logger LOG = 
LoggerFactory.getLogger(RSAEncryptionUtils.class);
+
+    public EncryptionKeys generateKeys(){
+        KeyPairGenerator keyPairGenerator;
+        try {
+            keyPairGenerator = KeyPairGenerator.getInstance("RSA");
+
+            KeyPair keyPair = keyPairGenerator.generateKeyPair();
+            PublicKey publicKey = keyPair.getPublic();
+            PrivateKey privateKey = keyPair.getPrivate();
+            byte[] publicKeyAsBytes = publicKey.getEncoded();
+            byte[] privateKeyAsBytes = privateKey.getEncoded();
+
+            return new EncryptionKeys(privateKeyAsBytes, publicKeyAsBytes, 
DateUtils.getLocalDateTimeOfTenant(), generateVersionString());
+
+        } catch (NoSuchAlgorithmException e) {
+           LOG.error("Error occurred", e);
+        }
+        throw new 
GeneralPlatformDomainRuleException("error.msg.error.in.generating.keys", "Error 
in generating keys");
+    }
+
+    public String decryptUsingRSA(String encryptedText, final byte[] 
privateKeyAsBytes, boolean isBase64Encoded) {
+        String decryptedString = null;
+        try {
+            KeyFactory kf = KeyFactory.getInstance("RSA");
+            PrivateKey privateKey = kf.generatePrivate(new 
PKCS8EncodedKeySpec(privateKeyAsBytes));
+            Cipher decrypt = Cipher.getInstance("RSA");
+            decrypt.init(Cipher.DECRYPT_MODE, privateKey);
+            if(isBase64Encoded) {
+                encryptedText = Hex.encodeHexString(encryptedText.getBytes( 
UTF_8));
+                byte[] decryptByteFromUi = 
decrypt.doFinal(DatatypeConverter.parseHexBinary(encryptedText));
+                decryptedString =  new String(decryptByteFromUi,  UTF_8);
+            } else {
+                byte[] decryptByteFromUi = 
decrypt.doFinal(encryptedText.getBytes( UTF_8));
+                decryptedString =  new String(decryptByteFromUi, UTF_8);
+            }
+            return decryptedString;
+        }catch (NoSuchAlgorithmException | InvalidKeySpecException | 
NoSuchPaddingException | InvalidKeyException |

Review comment:
       done!

##########
File path: 
fineract-provider/src/main/java/org/apache/fineract/infrastructure/crypt/api/CryptographyApiResource.java
##########
@@ -0,0 +1,75 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.infrastructure.crypt.api;
+
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
+import io.swagger.annotations.ApiResponse;
+import io.swagger.annotations.ApiResponses;
+import io.swagger.annotations.SwaggerDefinition;
+import io.swagger.annotations.Tag;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import 
org.apache.fineract.infrastructure.core.serialization.ToApiJsonSerializer;
+import org.apache.fineract.infrastructure.crypt.data.CryptData;
+import 
org.apache.fineract.infrastructure.crypt.service.CryptographyReadPlatformService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Scope;
+import org.springframework.stereotype.Component;
+
+
+/**
+ * @author manoj
+ */
+
+@Path("crypt")
+@Component
+@Scope("singleton")
+@Api(tags = {"Cryptography"})
+@SwaggerDefinition(tags = {
+        @Tag(name = "Cryptography", description = "Provides RSA public keys 
for various modules(types)")
+})
+public class CryptographyApiResource {
+
+    private final ToApiJsonSerializer<CryptData> toApiJsonSerializer;
+    private final CryptographyReadPlatformService 
cryptographyReadPlatformService;
+
+    @Autowired
+    public CryptographyApiResource(ToApiJsonSerializer<CryptData> 
toApiJsonSerializer,
+                                   CryptographyReadPlatformService 
cryptographyReadPlatformService) {
+        this.toApiJsonSerializer = toApiJsonSerializer;
+        this.cryptographyReadPlatformService = cryptographyReadPlatformService;
+    }
+
+    @GET
+    @Path("publickey/{type}")
+    @Consumes({ MediaType.APPLICATION_JSON })
+    @Produces({ MediaType.APPLICATION_JSON })
+    @ApiOperation(value = "Provides RSA public key for {type}", notes = "Key 
is provided with a version ")
+    @ApiResponses({@ApiResponse(code = 200, message = "RSA public key")})
+    public String downloadFile(@PathParam("type") @ApiParam(value = "type") 
final String type) {

Review comment:
       NIce Catch 😄 , yes absolutely, it was a classic copy paste.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to