-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'aiccra-tip-migrations' into aiccra-dev
- Loading branch information
Showing
6 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
marlo-utils/src/main/java/org/cgiar/ccafs/marlo/utils/AESConvert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/***************************************************************** | ||
* This file is part of Managing Agricultural Research for Learning & | ||
* Outcomes Platform (MARLO). | ||
* MARLO is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* at your option) any later version. | ||
* MARLO 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 General Public License for more details. | ||
* You should have received a copy of the GNU General Public License | ||
* along with MARLO. If not, see <http://www.gnu.org/licenses/>. | ||
*****************************************************************/ | ||
package org.cgiar.ccafs.marlo.utils; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.security.MessageDigest; | ||
import java.util.Base64; | ||
import java.util.zip.GZIPOutputStream; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.KeyGenerator; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
|
||
public class AESConvert { | ||
|
||
private static Logger LOG = LoggerFactory.getLogger(AESConvert.class); | ||
|
||
public static String stringToAES(String value) { | ||
MessageDigest md; | ||
try { | ||
// Generate a secret key | ||
KeyGenerator keygen = KeyGenerator.getInstance("AES"); | ||
keygen.init(256); // Use a 256-bit key (AES-256) | ||
SecretKey secretKey = keygen.generateKey(); | ||
|
||
// Convert the secret key to byte array | ||
byte[] secretKeyBytes = secretKey.getEncoded(); | ||
|
||
// Convert the text to compress into bytes | ||
String originalText = "Sample text to compress and encrypt using AES"; | ||
byte[] originalBytes = originalText.getBytes(); | ||
|
||
// Compress the text | ||
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); | ||
GZIPOutputStream gzipStream = new GZIPOutputStream(byteStream); | ||
gzipStream.write(originalBytes); | ||
gzipStream.close(); | ||
byte[] compressedBytes = byteStream.toByteArray(); | ||
|
||
// Encrypt the compressed text using AES | ||
Cipher cipher = Cipher.getInstance("AES"); | ||
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(secretKeyBytes, "AES")); | ||
byte[] encryptedBytes = cipher.doFinal(compressedBytes); | ||
|
||
// Encode the encrypted bytes to Base64 for easier storage or transmission | ||
String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes); | ||
|
||
// Decode the encrypted text into bytes | ||
byte[] decodedEncryptedBytes = Base64.getDecoder().decode(encryptedText); | ||
|
||
// Decrypt the encrypted text using AES | ||
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secretKeyBytes, "AES")); | ||
byte[] decryptedBytes = cipher.doFinal(decodedEncryptedBytes); | ||
|
||
// Decompress the decrypted text | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
outputStream.write(decryptedBytes); | ||
outputStream.close(); | ||
|
||
// Convert the decompressed text to a string | ||
String decompressedText = new String(outputStream.toByteArray()); | ||
|
||
return encryptedText; | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...main/resources/database/migrations/V2_6_0_20240307_1430__CreateTIPSecuritySpecificity.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
INSERT INTO parameters (global_unit_type_id, `key`, `description`, `format`, default_value, category) | ||
VALUES ( '1', 'tip_security_active', 'Enable TIP security', '1', 'false', '2'); | ||
INSERT INTO parameters (global_unit_type_id, `key`, `description`, `format`, default_value, category) | ||
VALUES ( '3', 'tip_security_active', 'Enable TIP security', '1', 'false', '2'); | ||
INSERT INTO parameters (global_unit_type_id, `key`, `description`, `format`, default_value, category) | ||
VALUES ( '4', 'tip_security_active', 'Enable TIP security', '1', 'false', '2'); |