Created
March 9, 2014 09:42
-
-
Save v6ak/9445287 to your computer and use it in GitHub Desktop.
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
| val key: PGPPublicKey = ... | |
| def encrypt(plaintext: String): String = { | |
| val baos = new ByteArrayOutputStream() | |
| val literalDataGenerator = new PGPLiteralDataGenerator() | |
| val comData = new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZLIB) // I've also tried other compression algorithms | |
| val bytes = plaintext.getBytes("utf-8") | |
| val BufferSize = 1 << 16 | |
| import resource.managed // The "managed" method with for is just a nice replacement of try/catch/finally{x.close()} | |
| for{ | |
| edg <- managed(new PGPEncryptedDataGenerator(SymmetricKeyAlgorithmTags.CAST5, new SecureRandom(), "BC")) | |
| _ = edg.addMethod(key) | |
| armoredOut <- managed(new ArmoredOutputStream(baos)) | |
| encryptedOut <- managed(edg.open(armoredOut, bytes.length)) | |
| compressedOut <- managed(comData.open(encryptedOut)) | |
| literalOut <- managed(literalDataGenerator.open(compressedOut, PGPLiteralData.BINARY, "x.txt", new Date(), new Array[Byte](BufferSize))) | |
| }{ | |
| literalOut.write(bytes) | |
| literalOut.flush(); | |
| } | |
| baos.toString | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment