use of org.jivesoftware.smackx.ox.element.OpenPgpElement in project Smack by igniterealtime.
the class PainlessOpenPgpProvider method encrypt.
@Override
public OpenPgpElementAndMetadata encrypt(CryptElement element, OpenPgpSelf self, Collection<OpenPgpContact> recipients) throws IOException, PGPException {
InputStream plainText = element.toInputStream();
ByteArrayOutputStream cipherText = new ByteArrayOutputStream();
EncryptionOptions encOpts = EncryptionOptions.encryptCommunications();
for (OpenPgpContact contact : recipients) {
PGPPublicKeyRingCollection keys = contact.getTrustedAnnouncedKeys();
if (keys == null) {
LOGGER.log(Level.WARNING, "There are no suitable keys for contact " + contact.getJid());
}
encOpts.addRecipients(keys);
}
encOpts.addRecipients(self.getTrustedAnnouncedKeys());
EncryptionStream cipherStream = PGPainless.encryptAndOrSign().onOutputStream(cipherText).withOptions(ProducerOptions.encrypt(encOpts).setAsciiArmor(false));
Streams.pipeAll(plainText, cipherStream);
plainText.close();
cipherStream.flush();
cipherStream.close();
cipherText.close();
String base64 = Base64.encodeToString(cipherText.toByteArray());
OpenPgpElement openPgpElement = new OpenPgpElement(base64);
return new OpenPgpElementAndMetadata(openPgpElement, cipherStream.getResult());
}
use of org.jivesoftware.smackx.ox.element.OpenPgpElement in project Smack by igniterealtime.
the class PainlessOpenPgpProvider method signAndEncrypt.
@Override
public OpenPgpElementAndMetadata signAndEncrypt(SigncryptElement element, OpenPgpSelf self, Collection<OpenPgpContact> recipients) throws IOException, PGPException {
InputStream plainText = element.toInputStream();
ByteArrayOutputStream cipherText = new ByteArrayOutputStream();
EncryptionOptions encOpts = EncryptionOptions.encryptCommunications();
for (OpenPgpContact contact : recipients) {
PGPPublicKeyRingCollection keys = contact.getTrustedAnnouncedKeys();
if (keys == null) {
LOGGER.log(Level.WARNING, "There are no suitable keys for contact " + contact.getJid());
}
encOpts.addRecipients(keys);
}
encOpts.addRecipients(self.getTrustedAnnouncedKeys());
SigningOptions signOpts = new SigningOptions();
signOpts.addInlineSignature(getStore().getKeyRingProtector(), self.getSigningKeyRing(), DocumentSignatureType.BINARY_DOCUMENT);
EncryptionStream cipherStream = PGPainless.encryptAndOrSign().onOutputStream(cipherText).withOptions(ProducerOptions.signAndEncrypt(encOpts, signOpts).setAsciiArmor(false));
Streams.pipeAll(plainText, cipherStream);
plainText.close();
cipherStream.flush();
cipherStream.close();
cipherText.close();
String base64 = Base64.encodeToString(cipherText.toByteArray());
OpenPgpElement openPgpElement = new OpenPgpElement(base64);
return new OpenPgpElementAndMetadata(openPgpElement, cipherStream.getResult());
}
use of org.jivesoftware.smackx.ox.element.OpenPgpElement in project Smack by igniterealtime.
the class PainlessOpenPgpProvider method sign.
@Override
public OpenPgpElementAndMetadata sign(SignElement element, OpenPgpSelf self) throws IOException, PGPException {
InputStream plainText = element.toInputStream();
ByteArrayOutputStream cipherText = new ByteArrayOutputStream();
EncryptionStream cipherStream = PGPainless.encryptAndOrSign().onOutputStream(cipherText).withOptions(ProducerOptions.sign(new SigningOptions().addInlineSignature(getStore().getKeyRingProtector(), self.getSigningKeyRing(), "xmpp:" + self.getJid().toString(), DocumentSignatureType.BINARY_DOCUMENT)).setAsciiArmor(false));
Streams.pipeAll(plainText, cipherStream);
plainText.close();
cipherStream.flush();
cipherStream.close();
cipherText.close();
String base64 = Base64.encodeToString(cipherText.toByteArray());
OpenPgpElement openPgpElement = new OpenPgpElement(base64);
return new OpenPgpElementAndMetadata(openPgpElement, cipherStream.getResult());
}
use of org.jivesoftware.smackx.ox.element.OpenPgpElement in project Smack by igniterealtime.
the class OpenPgpElementTest method providerTest.
@Test
public void providerTest() throws Exception {
String expected = "<openpgp xmlns='urn:xmpp:openpgp:0'>" + "BASE64_OPENPGP_MESSAGE" + "</openpgp>";
OpenPgpElement element = new OpenPgpElement("BASE64_OPENPGP_MESSAGE");
assertXmlSimilar(expected, element.toXML().toString());
XmlPullParser parser = TestUtils.getParser(expected);
OpenPgpElement parsed = OpenPgpElementProvider.TEST_INSTANCE.parse(parser);
assertEquals(element.getEncryptedBase64MessageContent(), parsed.getEncryptedBase64MessageContent());
}
Aggregations