-
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.
- Loading branch information
1 parent
3105708
commit a72345a
Showing
12 changed files
with
135 additions
and
49 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
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
40 changes: 40 additions & 0 deletions
40
util/src/main/kotlin/com/simiacryptus/skyenet/util/AwsUtil.kt
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,40 @@ | ||
package com.simiacryptus.skyenet.util | ||
|
||
import com.amazonaws.services.kms.AWSKMSClientBuilder | ||
import com.amazonaws.services.kms.model.DecryptRequest | ||
import com.amazonaws.services.kms.model.EncryptRequest | ||
import java.nio.ByteBuffer | ||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
import java.util.* | ||
|
||
object AwsUtil { | ||
|
||
fun encryptFile(inputFilePath: String, outputFilePath: String) { | ||
val filePath = Paths.get(inputFilePath) | ||
val fileBytes = Files.readAllBytes(filePath) | ||
val kmsClient = AWSKMSClientBuilder.standard().build() | ||
val encryptRequest = | ||
EncryptRequest().withKeyId("arn:aws:kms:us-east-1:470240306861:key/a1340b89-64e6-480c-a44c-e7bc0c70dcb1") | ||
.withPlaintext(ByteBuffer.wrap(fileBytes)) | ||
val result = kmsClient.encrypt(encryptRequest) | ||
val cipherTextBlob = result.ciphertextBlob | ||
val encryptedData = Base64.getEncoder().encodeToString(cipherTextBlob.array()) | ||
val outputPath = Paths.get(outputFilePath) | ||
Files.write(outputPath, encryptedData.toByteArray()) | ||
} | ||
|
||
fun decryptResource(resourceFile: String): String { | ||
val encryptedData = javaClass.classLoader.getResourceAsStream(resourceFile)?.readAllBytes() | ||
if (null == encryptedData) { | ||
throw RuntimeException("Unable to load resource: $resourceFile") | ||
} | ||
val decodedData = Base64.getDecoder().decode(encryptedData) | ||
val kmsClient = AWSKMSClientBuilder.defaultClient() | ||
val decryptRequest = DecryptRequest().withCiphertextBlob(ByteBuffer.wrap(decodedData)) | ||
val decryptResult = kmsClient.decrypt(decryptRequest) | ||
val decryptedData = decryptResult.plaintext.array() | ||
return String(decryptedData, StandardCharsets.UTF_8) | ||
} | ||
} |
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
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
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