Skip to content

Commit 9eec0dd

Browse files
author
tk
committed
replace external library with java native base64 implementation
1 parent 3971a72 commit 9eec0dd

File tree

5 files changed

+11
-20
lines changed

5 files changed

+11
-20
lines changed

pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,6 @@
6262
</properties>
6363

6464
<dependencies>
65-
<dependency>
66-
<groupId>net.iharder</groupId>
67-
<artifactId>base64</artifactId>
68-
<version>2.3.9</version>
69-
</dependency>
70-
7165
<dependency>
7266
<groupId>net.markenwerk</groupId>
7367
<artifactId>utils-data-fetcher</artifactId>
@@ -244,7 +238,6 @@
244238
<dependencySourceInclude>net.markenwerk:*</dependencySourceInclude>
245239
</dependencySourceIncludes>
246240
<links>
247-
<link>http://iharder.sourceforge.net/current/java/base64/api/</link>
248241
<link>https://eclipse-ee4j.github.io/mail/docs/api/</link>
249242
</links>
250243
</configuration>

src/main/java/net/markenwerk/utils/mail/dkim/DkimSigner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.security.interfaces.RSAPrivateKey;
3535
import java.security.spec.InvalidKeySpecException;
3636
import java.security.spec.PKCS8EncodedKeySpec;
37+
import java.util.Base64;
3738
import java.util.Collection;
3839
import java.util.Date;
3940
import java.util.Enumeration;
@@ -55,7 +56,6 @@
5556
import com.sun.mail.util.QPEncoderStream;
5657

5758
import net.i2p.crypto.eddsa.EdDSAPrivateKey;
58-
import net.iharder.Base64;
5959
import net.markenwerk.utils.data.fetcher.BufferedDataFetcher;
6060
import net.markenwerk.utils.data.fetcher.DataFetchException;
6161

@@ -690,7 +690,7 @@ private static String quotedPrintable(String s) {
690690
}
691691

692692
private static String base64Encode(byte[] bytes) {
693-
String encoded = Base64.encodeBytes(bytes);
693+
String encoded = Base64.getEncoder().encodeToString(bytes);
694694

695695
// remove unnecessary line feeds after 76 characters
696696
encoded = encoded.replace("\n", "");

src/main/java/net/markenwerk/utils/mail/dkim/DomainKey.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818
package net.markenwerk.utils.mail.dkim;
1919

20-
import java.io.IOException;
2120
import java.security.InvalidKeyException;
2221
import java.security.KeyFactory;
2322
import java.security.NoSuchAlgorithmException;
@@ -29,6 +28,7 @@
2928
import java.security.interfaces.RSAPublicKey;
3029
import java.security.spec.InvalidKeySpecException;
3130
import java.security.spec.X509EncodedKeySpec;
31+
import java.util.Base64;
3232
import java.util.Collections;
3333
import java.util.HashSet;
3434
import java.util.Map;
@@ -39,7 +39,6 @@
3939
import net.i2p.crypto.eddsa.EdDSAPublicKey;
4040
import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable;
4141
import net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec;
42-
import net.iharder.Base64;
4342

4443
/**
4544
* A {@code DomainKey} holds the information about a domain key.
@@ -153,11 +152,11 @@ private PublicKey getPublicKey(String publicKeyTagValue) throws DkimException {
153152
private RSAPublicKey getRsaPublicKey(String publicKeyTagValue) {
154153
try {
155154
KeyFactory keyFactory = KeyFactory.getInstance(KeyPairType.RSA.getJavaNotation());
156-
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.decode(publicKeyTagValue));
155+
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyTagValue));
157156
return (RSAPublicKey) keyFactory.generatePublic(publicKeySpec);
158157
} catch (NoSuchAlgorithmException nsae) {
159158
throw new DkimException("RSA algorithm not found by JVM");
160-
} catch (IOException e) {
159+
} catch (IllegalArgumentException e) {
161160
throw new DkimException("The public key " + publicKeyTagValue + " couldn't be read.", e);
162161
} catch (InvalidKeySpecException e) {
163162
throw new DkimException("The public key " + publicKeyTagValue + " couldn't be decoded.", e);
@@ -167,12 +166,12 @@ private RSAPublicKey getRsaPublicKey(String publicKeyTagValue) {
167166
private EdDSAPublicKey getEd25519PublicKey(String publicKeyTagValue) {
168167
try {
169168
KeyFactory keyFactory = KeyFactory.getInstance(KeyPairType.ED25519.getJavaNotation());
170-
EdDSAPublicKeySpec publicKeySpec = new EdDSAPublicKeySpec(Base64.decode(publicKeyTagValue),
169+
EdDSAPublicKeySpec publicKeySpec = new EdDSAPublicKeySpec(Base64.getDecoder().decode(publicKeyTagValue),
171170
EdDSANamedCurveTable.ED_25519_CURVE_SPEC);
172171
return (EdDSAPublicKey) keyFactory.generatePublic(publicKeySpec);
173172
} catch (NoSuchAlgorithmException nsae) {
174173
throw new DkimException("Ed25519 algorithm not found by JVM");
175-
} catch (IOException e) {
174+
} catch (IllegalArgumentException e) {
176175
throw new DkimException("The public key " + publicKeyTagValue + " couldn't be read.", e);
177176
} catch (InvalidKeySpecException e) {
178177
throw new DkimException("The public key " + publicKeyTagValue + " couldn't be decoded.", e);

src/test/java/net/markenwerk/utils/mail/dkim/DomainKeyTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.junit.Assert.assertNotNull;
66

77
import java.io.ByteArrayOutputStream;
8+
import java.util.Base64;
89
import java.util.HashMap;
910
import java.util.Map;
1011
import java.util.Properties;
@@ -18,8 +19,6 @@
1819

1920
import org.junit.Test;
2021

21-
import net.iharder.Base64;
22-
2322
public class DomainKeyTest {
2423

2524
private static final String EXAMPLE_DOMAIN_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDwIRP/UC3SBsEmGqZ9ZJW3/DkMoGeLnQg1fWn7/zYtIxN2SnFCjxOCKG9v3b4jYfcTNh5ijSsq631uBItLa7od+v/RtdC2UzJ1lWT947qR+Rcac2gbto/NMqJ0fzfVjH4OuKhitdY9tf6mcwGjaNBcWToIMmPSPDdQPNUYckcQ2QIDAQAB";
@@ -37,7 +36,7 @@ public void checkDomainKeyRecognizesPublicKey() throws Exception {
3736
DomainKey domainKey = new DomainKey(tags);
3837

3938
assertNotNull(domainKey);
40-
assertArrayEquals(Base64.decode(EXAMPLE_DOMAIN_KEY), domainKey.getPublicKey().getEncoded());
39+
assertArrayEquals(Base64.getDecoder().decode(EXAMPLE_DOMAIN_KEY), domainKey.getPublicKey().getEncoded());
4140

4241
}
4342

src/test/java/net/markenwerk/utils/mail/dkim/Utils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import java.security.DigestOutputStream;
99
import java.security.MessageDigest;
1010
import java.security.NoSuchAlgorithmException;
11+
import java.util.Base64;
1112
import java.util.Random;
1213

13-
import net.iharder.Base64;
1414
import net.markenwerk.commons.nulls.NullOutputStream;
1515
import net.markenwerk.utils.data.fetcher.BufferedDataFetcher;
1616

@@ -53,7 +53,7 @@ static String digest(String string, String algorithm) throws IOException, NoSuch
5353
MessageDigest digest = MessageDigest.getInstance(algorithm);
5454
DigestOutputStream out = new DigestOutputStream(new NullOutputStream(), digest);
5555
new BufferedDataFetcher().copy(new ByteArrayInputStream(string.getBytes()), out, true, true);
56-
return Base64.encodeBytes(digest.digest());
56+
return Base64.getEncoder().encodeToString(digest.digest());
5757
}
5858

5959
static DkimSigner getSigner(Canonicalization canonicalization, SigningAlgorithm algorithm) throws Exception {

0 commit comments

Comments
 (0)