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

Fix checkstyle issues and android compatibility

This commit is contained in:
Paul Schaub 2018-07-09 16:52:38 +02:00
parent 0031f7f5f3
commit 319f65ea15
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
5 changed files with 48 additions and 29 deletions

View file

@ -65,7 +65,7 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider {
} }
EncryptionStream cipherStream = PGPainless.createEncryptor().onOutputStream(cipherText) EncryptionStream cipherStream = PGPainless.createEncryptor().onOutputStream(cipherText)
.toRecipients(recipientKeys.toArray(new PGPPublicKeyRingCollection[]{})) .toRecipients(recipientKeys.toArray(new PGPPublicKeyRingCollection[] {}))
.andToSelf(self.getAnnouncedPublicKeys()) .andToSelf(self.getAnnouncedPublicKeys())
.usingSecureAlgorithms() .usingSecureAlgorithms()
.signWith(store.getKeyRingProtector(), self.getSigningKeyRing()) .signWith(store.getKeyRingProtector(), self.getSigningKeyRing())
@ -115,7 +115,7 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider {
} }
EncryptionStream cipherStream = PGPainless.createEncryptor().onOutputStream(cipherText) EncryptionStream cipherStream = PGPainless.createEncryptor().onOutputStream(cipherText)
.toRecipients(recipientKeys.toArray(new PGPPublicKeyRingCollection[]{})) .toRecipients(recipientKeys.toArray(new PGPPublicKeyRingCollection[] {}))
.andToSelf(self.getAnnouncedPublicKeys()) .andToSelf(self.getAnnouncedPublicKeys())
.usingSecureAlgorithms() .usingSecureAlgorithms()
.doNotSign() .doNotSign()

View file

@ -0,0 +1,20 @@
/**
*
* 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.
*/
/**
* Crypto Providers for XEP-0373: OpenPGP for XMPP using Bouncycastle.
*/
package org.jivesoftware.smackx.ox.crypto;

View file

@ -18,10 +18,7 @@ package org.jivesoftware.smackx.ox.selection_strategy;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint;
@ -32,31 +29,17 @@ public class AnnouncedKeys {
public static class PubKeyRingSelectionStrategy extends PublicKeyRingSelectionStrategy<Map<OpenPgpV4Fingerprint, Date>> { public static class PubKeyRingSelectionStrategy extends PublicKeyRingSelectionStrategy<Map<OpenPgpV4Fingerprint, Date>> {
private static final Logger LOGGER = Logger.getLogger(PubKeyRingSelectionStrategy.class.getName());
@Override @Override
public boolean accept(Map<OpenPgpV4Fingerprint, Date> announcedKeys, PGPPublicKeyRing publicKeys) { public boolean accept(Map<OpenPgpV4Fingerprint, Date> announcedKeys, PGPPublicKeyRing publicKeys) {
try {
return announcedKeys.keySet().contains(new OpenPgpV4Fingerprint(publicKeys)); return announcedKeys.keySet().contains(new OpenPgpV4Fingerprint(publicKeys));
} catch (PGPException e) {
LOGGER.log(Level.WARNING, "Key might be damaged.", e);
return false;
}
} }
} }
public static class SecKeyRingSelectionStrategy extends SecretKeyRingSelectionStrategy<Map<OpenPgpV4Fingerprint, Date>> { public static class SecKeyRingSelectionStrategy extends SecretKeyRingSelectionStrategy<Map<OpenPgpV4Fingerprint, Date>> {
private static final Logger LOGGER = Logger.getLogger(SecKeyRingSelectionStrategy.class.getName());
@Override @Override
public boolean accept(Map<OpenPgpV4Fingerprint, Date> announcedKeys, PGPSecretKeyRing secretKeys) { public boolean accept(Map<OpenPgpV4Fingerprint, Date> announcedKeys, PGPSecretKeyRing secretKeys) {
try {
return announcedKeys.keySet().contains(new OpenPgpV4Fingerprint(secretKeys)); return announcedKeys.keySet().contains(new OpenPgpV4Fingerprint(secretKeys));
} catch (PGPException e) {
LOGGER.log(Level.WARNING, "Key might be damaged.", e);
return false;
}
} }
} }
} }

View file

@ -20,7 +20,10 @@ import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.ParseException; import java.text.ParseException;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
@ -29,9 +32,9 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpMetadataStore; import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpMetadataStore;
import org.jivesoftware.smackx.ox.util.FileUtils;
import org.jivesoftware.smackx.ox.util.Util; import org.jivesoftware.smackx.ox.util.Util;
import org.bouncycastle.openpgp.PGPException;
import org.jxmpp.jid.BareJid; import org.jxmpp.jid.BareJid;
import org.jxmpp.util.XmppDateTime; import org.jxmpp.util.XmppDateTime;
import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint;
@ -68,7 +71,9 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore
BufferedReader reader = null; BufferedReader reader = null;
try { try {
reader = Files.newBufferedReader(source.toPath(), Util.UTF8); InputStream inputStream = FileUtils.prepareFileInputStream(source);
InputStreamReader isr = new InputStreamReader(inputStream, Util.UTF8);
reader = new BufferedReader(isr);
Map<OpenPgpV4Fingerprint, Date> fingerprintDateMap = new HashMap<>(); Map<OpenPgpV4Fingerprint, Date> fingerprintDateMap = new HashMap<>();
String line; int lineNr = 0; String line; int lineNr = 0;
@ -86,7 +91,7 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore
OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(split[0]); OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(split[0]);
Date date = XmppDateTime.parseXEP0082Date(split[1]); Date date = XmppDateTime.parseXEP0082Date(split[1]);
fingerprintDateMap.put(fingerprint, date); fingerprintDateMap.put(fingerprint, date);
} catch (PGPException | ParseException e) { } catch (IllegalArgumentException | ParseException e) {
LOGGER.log(Level.WARNING, "Error parsing fingerprint/date touple in line " + lineNr + LOGGER.log(Level.WARNING, "Error parsing fingerprint/date touple in line " + lineNr +
" of file " + source.getAbsolutePath(), e); " of file " + source.getAbsolutePath(), e);
} }
@ -127,7 +132,9 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore
BufferedWriter writer = null; BufferedWriter writer = null;
try { try {
writer = Files.newBufferedWriter(destination.toPath(), Util.UTF8); OutputStream outputStream = FileUtils.prepareFileOutputStream(destination);
OutputStreamWriter osw = new OutputStreamWriter(outputStream, Util.UTF8);
writer = new BufferedWriter(osw);
for (OpenPgpV4Fingerprint fingerprint : data.keySet()) { for (OpenPgpV4Fingerprint fingerprint : data.keySet()) {
Date date = data.get(fingerprint); Date date = data.get(fingerprint);
String line = fingerprint.toString() + " " + String line = fingerprint.toString() + " " +

View file

@ -21,11 +21,15 @@ import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpTrustStore; import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpTrustStore;
import org.jivesoftware.smackx.ox.util.FileUtils;
import org.jivesoftware.smackx.ox.util.Util; import org.jivesoftware.smackx.ox.util.Util;
import org.jxmpp.jid.BareJid; import org.jxmpp.jid.BareJid;
@ -50,7 +54,9 @@ public class FileBasedOpenPgpTrustStore extends AbstractOpenPgpTrustStore {
File file = getTrustPath(owner, fingerprint); File file = getTrustPath(owner, fingerprint);
BufferedReader reader = null; BufferedReader reader = null;
try { try {
reader = Files.newBufferedReader(file.toPath(), Util.UTF8); InputStream inputStream = FileUtils.prepareFileInputStream(file);
InputStreamReader isr = new InputStreamReader(inputStream, Util.UTF8);
reader = new BufferedReader(isr);
Trust trust = null; Trust trust = null;
String line; int lineNr = 0; String line; int lineNr = 0;
@ -98,7 +104,10 @@ public class FileBasedOpenPgpTrustStore extends AbstractOpenPgpTrustStore {
BufferedWriter writer = null; BufferedWriter writer = null;
try { try {
writer = Files.newBufferedWriter(file.toPath(), Util.UTF8); OutputStream outputStream = FileUtils.prepareFileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(outputStream, Util.UTF8);
writer = new BufferedWriter(osw);
writer.write(trust.toString()); writer.write(trust.toString());
writer.flush(); writer.flush();
writer.close(); writer.close();