8000 #320: implemented simple cache to get around the underlying S/MIME li… · zdfsir/simple-java-mail@e93123f · GitHub
[go: up one dir, main page]

Skip to content

Commit e93123f

Browse files
committed
bbottema#320: implemented simple cache to get around the underlying S/MIME library's performance issues (including BouncyCastle)
1 parent f9fc691 commit e93123f

File tree

1 file changed

+12
-2
lines changed
  • modules/smime-module/src/main/java/org/simplejavamail/internal/smimesupport

1 file changed

+12
-2
lines changed

modules/smime-module/src/main/java/org/simplejavamail/internal/smimesupport/SMIMESupport.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
import java.security.cert.CertificateException;
5757
import java.security.cert.X509Certificate;
5858
import java.util.ArrayList;
59+
import java.util.HashMap;
5960
import java.util.List;
61+
import java.util.Map;
6062

6163
import static java.lang.String.format;
6264
import static java.util.Arrays.asList;
@@ -78,6 +80,7 @@ public class SMIMESupport implements SMIMEModule {
7880

7981
private static final Logger LOGGER = LoggerFactory.getLogger(SMIMESupport.class);
8082
private static final List<String> SMIME_MIMETYPES = asList("application/pkcs7-mime", "application/x-pkcs7-mime", "multipart/signed");
83+
private static final Map<Pkcs12Config, SmimeKey> SIMPLE_SMIMESTORE_CACHE = new HashMap<>();
8184

8285
static {
8386
Security.addProvider(new BouncyCastleProvider());
@@ -456,7 +459,14 @@ public MimeMessage encryptMessage(@Nullable Session session, @NotNull MimeMessag
456459
}
457460

458461
private SmimeKey retrieveSmimeKeyFromPkcs12Keystore(@NotNull Pkcs12Config pkcs12) {
459-
SmimeKeyStore store = new SmimeKeyStore(new ByteArrayInputStream(pkcs12.getPkcs12StoreData()), pkcs12.getStorePassword());
460-
return store.getPrivateKey(pkcs12.getKeyAlias(), pkcs12.getKeyPassword());
462+
if (!SIMPLE_SMIMESTORE_CACHE.containsKey(pkcs12)) {
463+
SIMPLE_SMIMESTORE_CACHE.put(pkcs12, produceSmimeKey(pkcs12));
464+
}
465+
return SIMPLE_SMIMESTORE_CACHE.get(pkcs12);
466+
}
467+
468+
private SmimeKey produceSmimeKey(final @NotNull Pkcs12Config pkcs12) {
469+
return new SmimeKeyStore(new ByteArrayInputStream(pkcs12.getPkcs12StoreData()), pkcs12.getStorePassword())
470+
.getPrivateKey(pkcs12.getKeyAlias(), pkcs12.getKeyPassword());
461471
}
462472
}

0 commit comments

Comments
 (0)
0