Skip to content

Instantly share code, notes, and snippets.

@sgammon
Created December 29, 2021 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgammon/a971f106edd03219e2e7878bb98ed6cc to your computer and use it in GitHub Desktop.
Save sgammon/a971f106edd03219e2e7878bb98ed6cc to your computer and use it in GitHub Desktop.
Example of Passbase metadata encryption in Kotlin
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