mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-06-26 13:34:49 +02:00
Refactoring
This commit is contained in:
parent
5402306cca
commit
70be302b1b
|
@ -0,0 +1,16 @@
|
|||
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package pgp.cert_d;
|
||||
|
||||
import pgp.certificate_store.CertificateReaderBackend;
|
||||
import pgp.certificate_store.MergeCallback;
|
||||
|
||||
public abstract class BackendProvider {
|
||||
|
||||
public abstract CertificateReaderBackend provideCertificateReaderBackend();
|
||||
|
||||
public abstract MergeCallback provideDefaultMergeCallback();
|
||||
|
||||
}
|
|
@ -28,6 +28,11 @@ public class SharedPGPCertificateDirectoryImpl implements SharedPGPCertificateDi
|
|||
private final LockingMechanism writeLock;
|
||||
private final CertificateReaderBackend certificateReaderBackend;
|
||||
|
||||
public SharedPGPCertificateDirectoryImpl(BackendProvider backendProvider)
|
||||
throws NotAStoreException {
|
||||
this(backendProvider.provideCertificateReaderBackend());
|
||||
}
|
||||
|
||||
public SharedPGPCertificateDirectoryImpl(CertificateReaderBackend certificateReaderBackend)
|
||||
throws NotAStoreException {
|
||||
this(
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.certificate_store;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.util.encoders.Base64;
|
||||
import org.pgpainless.key.OpenPgpFingerprint;
|
||||
import pgp.certificate_store.Certificate;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
public class CertificateFactory {
|
||||
|
||||
public static Certificate certificateFromPublicKeyRing(PGPPublicKeyRing publicKeyRing) {
|
||||
return new Certificate() {
|
||||
@Override
|
||||
public String getFingerprint() {
|
||||
return OpenPgpFingerprint.of(publicKeyRing).toString().toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return new ByteArrayInputStream(publicKeyRing.getEncoded());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTag() throws IOException {
|
||||
MessageDigest digest;
|
||||
try {
|
||||
digest = MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new AssertionError("No MessageDigest for SHA-256 instantiated, although BC is on the classpath: " + e.getMessage());
|
||||
}
|
||||
digest.update(publicKeyRing.getEncoded());
|
||||
return Base64.toBase64String(digest.digest());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -4,16 +4,11 @@
|
|||
|
||||
package org.pgpainless.certificate_store;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.util.encoders.Base64;
|
||||
import org.pgpainless.PGPainless;
|
||||
import org.pgpainless.key.OpenPgpFingerprint;
|
||||
import pgp.certificate_store.Certificate;
|
||||
import pgp.certificate_store.CertificateReaderBackend;
|
||||
|
||||
|
@ -22,28 +17,6 @@ public class CertificateReader implements CertificateReaderBackend {
|
|||
@Override
|
||||
public Certificate readCertificate(InputStream inputStream) throws IOException {
|
||||
final PGPPublicKeyRing certificate = PGPainless.readKeyRing().publicKeyRing(inputStream);
|
||||
return new Certificate() {
|
||||
@Override
|
||||
public String getFingerprint() {
|
||||
return OpenPgpFingerprint.of(certificate).toString().toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return new ByteArrayInputStream(certificate.getEncoded());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTag() throws IOException {
|
||||
MessageDigest digest;
|
||||
try {
|
||||
digest = MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new AssertionError("No MessageDigest for SHA-256 instantiated, although BC is on the classpath: " + e.getMessage());
|
||||
}
|
||||
digest.update(certificate.getEncoded());
|
||||
return Base64.toBase64String(digest.digest());
|
||||
}
|
||||
};
|
||||
return CertificateFactory.certificateFromPublicKeyRing(certificate);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ include 'pgpainless-core',
|
|||
'pgpainless-sop',
|
||||
'pgpainless-cli',
|
||||
'pgpainless-cert-d',
|
||||
'pgpainless-cert-d-cli',
|
||||
'pgp-certificate-store',
|
||||
'pgp-cert-d-java',
|
||||
'pgp-cert-d-cli'
|
||||
'pgp-cert-d-java'
|
||||
|
||||
|
|
Loading…
Reference in a new issue