Created
December 29, 2021 18:24
-
-
Save sgammon/a971f106edd03219e2e7878bb98ed6cc to your computer and use it in GitHub Desktop.
Example of Passbase metadata encryption in Kotlin
This file contains hidden or 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
class CryptoLogic { | |
companion object { | |
const val metadataCipher = "RSA/None/PKCS1Padding" | |
} | |
fun <R> encryptMetadata(plaintext: String, callback: (String) -> R): R { | |
return withPublicKey(/* key info... */) { publicKey -> | |
// initialize the cipher with the resolved public key | |
val cipher: Cipher = Cipher.getInstance(metadataCipher) | |
cipher.init(Cipher.ENCRYPT_MODE, publicKey) | |
// invoke the callback, after base64-encoding the encrypted output | |
callback.invoke(Base64.getEncoder().encodeToString( | |
cipher.doFinal(plaintext.toByteArray(StandardCharsets.UTF_8)) | |
)) | |
} | |
} | |
fun <I, R> encryptMetadataJSON(pojo: I, callback: (String) -> R): R { | |
return encryptMetadata( | |
objectMapper().writeValueAsString(pojo), | |
callback | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment