Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace J-U-L with slf4j #10

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ dependencies {
compile 'org.bouncycastle:bcpg-jdk15on:1.58'
testCompile 'org.codehaus.groovy:groovy-all:2.4.7'
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.6'

}

javadoc {
Expand Down
43 changes: 18 additions & 25 deletions src/main/java/org/c02e/jpgpj/Decryptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.openpgp.PGPCompressedData;
import org.bouncycastle.openpgp.PGPDataValidationException;
Expand Down Expand Up @@ -71,7 +71,8 @@ public class Decryptor {
protected boolean verificationRequired;
protected String symmetricPassphrase;
protected Ring ring;
protected Logger log = Logger.getLogger(Decryptor.class.getName());
protected Logger log = LoggerFactory.getLogger(Decryptor.class.getName());


/** Constructs a decryptor with an empty key ring. */
public Decryptor() {
Expand Down Expand Up @@ -173,8 +174,7 @@ public FileMetadata decrypt(File ciphertext, File plaintext)
output.close();
plaintext.delete();
} catch (Exception ee) {
log.log(Level.SEVERE, "failed to delete bad output file "
+ plaintext, ee);
log.error("failed to delete bad output file {}", plaintext, ee);
}
throw e;
} finally {
Expand Down Expand Up @@ -229,8 +229,7 @@ protected List<FileMetadata> unpack(Iterator packets,
while (packets.hasNext()) {
Object packet = packets.next();

if (log.isLoggable(Level.FINEST))
log.finest("unpack " + packet.getClass());
log.trace("unpack {} ", packet.getClass());

if (packet instanceof PGPMarker) {
// no-op
Expand Down Expand Up @@ -273,7 +272,7 @@ protected List<FileMetadata> unpack(Iterator packets,
throw new PGPException("unexpected packet: " + packet.getClass());
}
}
log.finest("unpacked all");
log.trace("unpacked all");

// fail if verification required and any signature is bad
verify(verifiers, meta);
Expand Down Expand Up @@ -337,12 +336,10 @@ protected InputStream decrypt(Iterator data)
!Util.isEmpty(subkey.passphrase))
return decrypt(pke, subkey);

else if (log.isLoggable(Level.INFO))
log.info("not using decryption key " + subkey);
log.info("not using decryption key {} ", subkey);

} else {
if (log.isLoggable(Level.INFO))
log.info("not found decryption key " +
log.info("not found decryption key {} ",
Util.formatKeyId(pke.getKeyID()));
}

Expand All @@ -364,8 +361,8 @@ protected InputStream decrypt(PGPPublicKeyEncryptedData data, Subkey subkey)
if (data == null || subkey == null)
throw new DecryptionException("no suitable decryption key found");

if (log.isLoggable(Level.INFO))
log.info("using decryption key " + subkey);
log.info("using decryption key {} ", subkey);

return data.getDataStream(buildPublicKeyDecryptor(subkey));
}

Expand Down Expand Up @@ -430,9 +427,9 @@ protected void verify(List<Verifier> verifiers, List<FileMetadata> meta)
for (Verifier verifier : verifiers) {
if (!verifier.verify())
throw new VerificationException(
"bad signature for key " + verifier.key);
else if (log.isLoggable(Level.FINE))
log.fine("good signature for key " + verifier.key);
"bad signature for key " + verifier.key);
else
log.debug("good signature for key {} ", verifier.key);

Key key = verifier.getSignedBy();
for (FileMetadata file : meta)
Expand Down Expand Up @@ -549,8 +546,7 @@ public void setSig(PGPSignature s) throws PGPException {

key = getRing().findById(s.getKeyID());
if (key == null) {
if (Decryptor.this.log.isLoggable(Level.INFO))
Decryptor.this.log.info("not found verification key " +
Decryptor.this.log.info("not found verification key {} ",
Util.formatKeyId(s.getKeyID()));
return;
}
Expand All @@ -561,8 +557,7 @@ public void setSig(PGPSignature s) throws PGPException {
else
s.init(getVerifierProvider(), subkey.getPublicKey());

if (Decryptor.this.log.isLoggable(Level.INFO))
Decryptor.this.log.info((key == null ? "not " : "") +
Decryptor.this.log.info((key == null ? "not " : "") +
"using verification key " + subkey);
}

Expand All @@ -571,8 +566,7 @@ public void setSig1(PGPOnePassSignature s) throws PGPException {

key = getRing().findById(s.getKeyID());
if (key == null) {
if (Decryptor.this.log.isLoggable(Level.INFO))
Decryptor.this.log.info("not found verification key " +
Decryptor.this.log.info("not found verification key {}",
Util.formatKeyId(s.getKeyID()));
return;
}
Expand All @@ -583,8 +577,7 @@ public void setSig1(PGPOnePassSignature s) throws PGPException {
else
s.init(getVerifierProvider(), subkey.getPublicKey());

if (Decryptor.this.log.isLoggable(Level.INFO))
Decryptor.this.log.info((key == null ? "not " : "") +
Decryptor.this.log.info((key == null ? "not " : "") +
"using verification key " + subkey);
}

Expand Down
38 changes: 14 additions & 24 deletions src/main/java/org/c02e/jpgpj/Encryptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.bcpg.BCPGOutputStream;
import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
Expand Down Expand Up @@ -80,7 +80,7 @@ public class Encryptor {
protected int keyDerivationWorkFactor;

protected Ring ring;
protected Logger log = Logger.getLogger(Encryptor.class.getName());
protected Logger log = LoggerFactory.getLogger(Encryptor.class.getName());

/** Constructs an encryptor with an empty key ring. */
public Encryptor() {
Expand Down Expand Up @@ -278,8 +278,8 @@ public void encrypt(File plaintext, File ciphertext)
output.close();
ciphertext.delete();
} catch (Exception ee) {
log.log(Level.SEVERE, "failed to delete bad output file "
+ plaintext, ee);
log.error("failed to delete bad output file {} ",
plaintext, ee);
}
throw e;
} finally {
Expand Down Expand Up @@ -379,8 +379,7 @@ protected OutputStream armor(OutputStream out) {
*/
protected OutputStream encrypt(OutputStream out, FileMetadata meta)
throws IOException, PGPException {
if (log.isLoggable(Level.FINEST))
log.finest("using encryption algorithm " + encryptionAlgorithm);
log.trace("using encryption algorithm {} ", encryptionAlgorithm);

if (encryptionAlgorithm == EncryptionAlgorithm.Unencrypted)
return null;
Expand All @@ -404,9 +403,7 @@ protected OutputStream encrypt(OutputStream out, FileMetadata meta)
*/
protected OutputStream compress(OutputStream out, FileMetadata meta)
throws IOException, PGPException {
if (log.isLoggable(Level.FINEST))
log.finest("using compression algorithm " + compressionAlgorithm +
" -" + compressionLevel);
log.trace("using compression algorithm {} - {} ", compressionAlgorithm, compressionLevel);

if (compressionAlgorithm == CompressionAlgorithm.Uncompressed ||
compressionLevel < 1 || compressionLevel > 9)
Expand Down Expand Up @@ -436,8 +433,7 @@ protected OutputStream packet(OutputStream out, FileMetadata meta)
*/
protected SigningOutputStream sign(OutputStream out, FileMetadata meta)
throws IOException, PGPException {
if (log.isLoggable(Level.FINEST))
log.finest("using signing algorithm " + signingAlgorithm);
log.trace("using signing algorithm {} ", signingAlgorithm);

if (signingAlgorithm == HashingAlgorithm.Unsigned)
return null;
Expand All @@ -447,8 +443,7 @@ protected SigningOutputStream sign(OutputStream out, FileMetadata meta)
for (int i = signers.size() - 1; i >= 0; i--) {
Subkey subkey = signers.get(i).getSigning();
if (subkey == null || Util.isEmpty(subkey.passphrase)) {
if (log.isLoggable(Level.INFO))
log.info("not using signing key " + subkey);
log.info("not using signing key {}",subkey);
signers.remove(i);
}
}
Expand Down Expand Up @@ -492,8 +487,7 @@ protected PGPEncryptedDataGenerator buildEncryptor() {
*/
protected PublicKeyKeyEncryptionMethodGenerator buildPublicKeyEncryptor(
Key key) {
if (log.isLoggable(Level.INFO))
log.info("using encryption key " + key.getEncryption());
log.info("using encryption key {}", key.getEncryption());

PGPPublicKey publicKey = key.getEncryption().getPublicKey();
return new BcPublicKeyKeyEncryptionMethodGenerator(publicKey);
Expand All @@ -505,10 +499,8 @@ protected PublicKeyKeyEncryptionMethodGenerator buildPublicKeyEncryptor(
*/
protected PBEKeyEncryptionMethodGenerator buildSymmetricKeyEncryptor()
throws PGPException {
if (log.isLoggable(Level.INFO))
log.info("using symmetric encryption with " +
keyDerivationAlgorithm + " hash, work factor " +
keyDerivationWorkFactor);
log.info("using symmetric encryption with {} hash, work factor {} ",
keyDerivationAlgorithm, keyDerivationWorkFactor);

int algo = keyDerivationAlgorithm.ordinal();
return new BcPBEKeyEncryptionMethodGenerator(
Expand All @@ -524,8 +516,7 @@ protected PGPSignatureGenerator buildSigner(Key key, FileMetadata meta)
throws PGPException {
Subkey subkey = key.getSigning();

if (log.isLoggable(Level.INFO))
log.info("using signing key " + subkey);
log.info("using signing key {} ", subkey);

PGPContentSignerBuilder builder = buildSignerBuilder(
subkey.getPublicKey().getAlgorithm(),
Expand All @@ -537,8 +528,7 @@ protected PGPSignatureGenerator buildSigner(Key key, FileMetadata meta)

String uid = key.getSigningUid();
if (!Util.isEmpty(uid)) {
if (log.isLoggable(Level.FINE))
log.fine("using signing uid " + uid);
log.debug("using signing uid {}", uid);

PGPSignatureSubpacketGenerator signer =
new PGPSignatureSubpacketGenerator();
Expand Down
7 changes: 2 additions & 5 deletions src/test/groovy/org/c02e/jpgpj/DecryptorSpec.groovy
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package org.c02e.jpgpj

import java.text.SimpleDateFormat
import java.util.logging.Level
import java.util.logging.Logger
import spock.lang.Specification

class DecryptorSpec extends Specification {

/*
static {
Logger.getLogger('').handlers*.level = Level.FINEST
Logger.getLogger('org.c02e.jpgpj.Decryptor').level = Level.FINEST
static{
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "TRACE");
}
*/

Expand Down
8 changes: 2 additions & 6 deletions src/test/groovy/org/c02e/jpgpj/EncryptorSpec.groovy
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package org.c02e.jpgpj

import java.util.logging.Level
import java.util.logging.Logger
import org.bouncycastle.openpgp.PGPException
import spock.lang.Specification

class EncryptorSpec extends Specification {

/*
static {
Logger.getLogger('').handlers*.level = Level.FINEST
Logger.getLogger('org.c02e.jpgpj.Decryptor').level = Level.FINEST
Logger.getLogger('org.c02e.jpgpj.Encryptor').level = Level.FINEST
static{
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "TRACE");
}
*/

Expand Down