forked from opensearch-project/security
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security/Extension] Role encryption/decryption (opensearch-project#2620
) * Encryption/Decryption of `roles` Signed-off-by: Ryan Liang <jiallian@amazon.com> Signed-off-by: Maciej Mierzwa <dev.maciej.mierzwa@gmail.com>
- Loading branch information
1 parent
451a249
commit dc107cd
Showing
3 changed files
with
117 additions
and
6 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/main/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtil.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,55 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.security.authtoken.jwt; | ||
|
||
import java.util.Arrays; | ||
import java.util.Base64; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
public class EncryptionDecryptionUtil { | ||
|
||
public static String encrypt(final String secret, final String data) { | ||
|
||
byte[] decodedKey = Base64.getDecoder().decode(secret); | ||
|
||
try { | ||
Cipher cipher = Cipher.getInstance("AES"); | ||
// rebuild key using SecretKeySpec | ||
SecretKey originalKey = new SecretKeySpec(Arrays.copyOf(decodedKey, 16), "AES"); | ||
cipher.init(Cipher.ENCRYPT_MODE, originalKey); | ||
byte[] cipherText = cipher.doFinal(data.getBytes("UTF-8")); | ||
return Base64.getEncoder().encodeToString(cipherText); | ||
} catch (Exception e) { | ||
throw new RuntimeException( | ||
"Error occured while encrypting data", e); | ||
} | ||
} | ||
|
||
public static String decrypt(final String secret, final String encryptedString) { | ||
|
||
byte[] decodedKey = Base64.getDecoder().decode(secret); | ||
|
||
try { | ||
Cipher cipher = Cipher.getInstance("AES"); | ||
// rebuild key using SecretKeySpec | ||
SecretKey originalKey = new SecretKeySpec(Arrays.copyOf(decodedKey, 16), "AES"); | ||
cipher.init(Cipher.DECRYPT_MODE, originalKey); | ||
byte[] cipherText = cipher.doFinal(Base64.getDecoder().decode(encryptedString)); | ||
return new String(cipherText); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Error occured while decrypting data", e); | ||
} | ||
} | ||
} |
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