1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-24 04:52:05 +01:00

Include PGPainless and create encryption, decryption test

This commit is contained in:
Paul Schaub 2018-05-08 18:06:30 +02:00
parent d28ffda1c0
commit 583dfce7c0
2 changed files with 113 additions and 1 deletions

View file

@ -6,7 +6,9 @@ Smack API for OpenPGP using Bouncycastle."""
dependencies { dependencies {
compile project(':smack-core') compile project(':smack-core')
compile project(':smack-openpgp') compile project(':smack-openpgp')
compile 'org.bouncycastle:bcpg-jdk15on:1.56' compile 'org.bouncycastle:bcpg-jdk15on:1.59'
compile 'de.vanitasvitae.pgpainless:pgpainless:2.1.0'
testCompile project(path: ":smack-core", configuration: "testRuntime") testCompile project(path: ":smack-core", configuration: "testRuntime")
testCompile project(path: ":smack-core", configuration: "archives") testCompile project(path: ":smack-core", configuration: "archives")
testCompile project(path: ":smack-openpgp", configuration: "testRuntime")
} }

View file

@ -0,0 +1,110 @@
/**
*
* Copyright 2018 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.ox.bouncycastle;
import static junit.framework.TestCase.assertTrue;
import static org.jivesoftware.smackx.ox.TestKeys.JULIET_UID;
import static org.jivesoftware.smackx.ox.TestKeys.ROMEO_UID;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SignatureException;
import java.util.Arrays;
import java.util.Date;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.ox.TestKeys;
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.BouncyGPG;
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.algorithms.DefaultPGPAlgorithmSuites;
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.callbacks.KeyringConfigCallbacks;
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.callbacks.Xep0373KeySelectionStrategy;
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.keyrings.InMemoryKeyring;
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.keyrings.KeyringConfig;
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.keyrings.KeyringConfigs;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.util.io.Streams;
import org.junit.Test;
public class BasicEncryptionTest extends SmackTestSuite {
private static final Charset UTF8 = Charset.forName("UTF-8");
private final KeyringConfig keyringJuliet;
private final KeyringConfig keyringRomeo;
public BasicEncryptionTest() throws IOException, PGPException {
super();
// Prepare Juliets keyring
keyringJuliet = KeyringConfigs.forGpgExportedKeys(KeyringConfigCallbacks.withUnprotectedKeys());
((InMemoryKeyring) keyringJuliet).addSecretKey(TestKeys.JULIET_PRIV.getBytes(UTF8));
((InMemoryKeyring) keyringJuliet).addPublicKey(TestKeys.JULIET_PUB.getBytes(UTF8));
((InMemoryKeyring) keyringJuliet).addPublicKey(TestKeys.ROMEO_PUB.getBytes(UTF8));
// Prepare Romeos keyring
keyringRomeo = KeyringConfigs.forGpgExportedKeys(KeyringConfigCallbacks.withUnprotectedKeys());
((InMemoryKeyring) keyringRomeo).addSecretKey(TestKeys.ROMEO_PRIV.getBytes(UTF8));
((InMemoryKeyring) keyringRomeo).addPublicKey(TestKeys.ROMEO_PUB.getBytes(UTF8));
((InMemoryKeyring) keyringRomeo).addPublicKey(TestKeys.JULIET_PUB.getBytes(UTF8));
}
@Test
public void encryptionTest()
throws IOException, PGPException, NoSuchAlgorithmException, SignatureException, NoSuchProviderException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] message = "Hello World!!!!".getBytes(UTF8);
// Encrypt
OutputStream out = BouncyGPG.encryptToStream()
.withConfig(keyringJuliet)
.withKeySelectionStrategy(new Xep0373KeySelectionStrategy(new Date()))
.withAlgorithms(DefaultPGPAlgorithmSuites.defaultSuiteForGnuPG())
.toRecipients(ROMEO_UID, JULIET_UID)
.andSignWith(JULIET_UID)
.binaryOutput()
.andWriteTo(result);
out.write(message);
out.close();
byte[] encrypted = result.toByteArray();
// Decrypt
ByteArrayInputStream encIn = new ByteArrayInputStream(encrypted);
InputStream in = BouncyGPG.decryptAndVerifyStream()
.withConfig(keyringRomeo)
.withKeySelectionStrategy(new Xep0373KeySelectionStrategy(new Date()))
.andRequireSignatureFromAllKeys(JULIET_UID)
.fromEncryptedInputStream(encIn);
ByteArrayOutputStream decrypted = new ByteArrayOutputStream();
Streams.pipeAll(in, decrypted);
byte[] message2 = decrypted.toByteArray();
assertTrue(Arrays.equals(message, message2));
}
}