Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit ebd3b60

Browse files
committed
Code formatting
1 parent 34fe552 commit ebd3b60

7 files changed

Lines changed: 58 additions & 53 deletions

File tree

β€Ž.idea/dictionaries/andreblanke.xmlβ€Ž

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€ŽREADME.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ First, make sure you add the BouncyCastle security provider:
9494
Security.addProvider(new BouncyCastleProvider());
9595
```
9696

97-
Then, create an instance of the push service, either `nl.martijndwars.webpush.PushService` for synchronous blocking HTTP calls, or `nl.martijndwars.webpush.PushAsyncService` for asynchronous non-blocking HTTP calls:
97+
Then, create an instance of the push service, either `nl.martijndwars.webpush.JdkHttpClientPushService` for synchronous blocking HTTP calls, or `nl.martijndwars.webpush.PushAsyncService` for asynchronous non-blocking HTTP calls:
9898

9999
```java
100100
PushService pushService = new PushService(...);
Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
package nl.martijndwars.webpush.cli;
22

3+
import java.security.Security;
4+
35
import com.beust.jcommander.JCommander;
46
import com.beust.jcommander.ParameterException;
7+
58
import nl.martijndwars.webpush.cli.commands.GenerateKeyCommand;
69
import nl.martijndwars.webpush.cli.commands.SendNotificationCommand;
710
import nl.martijndwars.webpush.cli.handlers.GenerateKeyHandler;
811
import nl.martijndwars.webpush.cli.handlers.SendNotificationHandler;
12+
913
import org.bouncycastle.jce.provider.BouncyCastleProvider;
1014

11-
import java.security.Security;
15+
public final class Cli {
1216

13-
/**
14-
* Command-line interface
15-
*/
16-
public class Cli {
1717
private static final String GENERATE_KEY = "generate-key";
1818
private static final String SEND_NOTIFICATION = "send-notification";
1919

20-
public static void main(String[] args) {
20+
private Cli() {
21+
}
22+
23+
public static void main(final String[] args) {
2124
Security.addProvider(new BouncyCastleProvider());
2225

23-
GenerateKeyCommand generateKeyCommand = new GenerateKeyCommand();
24-
SendNotificationCommand sendNotificationCommand = new SendNotificationCommand();
26+
final var generateKeyCommand = new GenerateKeyCommand();
27+
final var sendNotificationCommand = new SendNotificationCommand();
2528

26-
JCommander jCommander = JCommander.newBuilder()
27-
.addCommand(GENERATE_KEY, generateKeyCommand)
28-
.addCommand(SEND_NOTIFICATION, sendNotificationCommand)
29-
.build();
29+
final var jCommander = JCommander.newBuilder()
30+
.addCommand(GENERATE_KEY, generateKeyCommand)
31+
.addCommand(SEND_NOTIFICATION, sendNotificationCommand)
32+
.build();
3033

3134
try {
3235
jCommander.parse(args);
@@ -39,10 +42,10 @@ public static void main(String[] args) {
3942
} else {
4043
jCommander.usage();
4144
}
42-
} catch (ParameterException e) {
43-
e.usage();
44-
} catch (Exception e) {
45-
e.printStackTrace();
45+
} catch (final ParameterException exception) {
46+
exception.usage();
47+
} catch (final Exception exception) {
48+
exception.printStackTrace();
4649
}
4750
}
4851
}

β€Žwebpush/src/main/java/nl/martijndwars/webpush/AbstractPushService.javaβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ private static Encrypted encrypt(final byte[] payload, final ECPublicKey userPub
176176
* Generates the local (ephemeral) keys.
177177
*/
178178
private static KeyPair generateLocalKeyPair() throws GeneralSecurityException {
179-
final var parameterSpec = ECNamedCurveTable.getParameterSpec("prime256v1");
179+
final var parameterSpec = ECNamedCurveTable.getParameterSpec(Utils.CURVE);
180180

181-
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ECDH", BouncyCastleProvider.PROVIDER_NAME);
181+
final var keyPairGenerator = KeyPairGenerator.getInstance("ECDH", BouncyCastleProvider.PROVIDER_NAME);
182182
keyPairGenerator.initialize(parameterSpec);
183183
return keyPairGenerator.generateKeyPair();
184184
}

β€Žwebpush/src/main/java/nl/martijndwars/webpush/HttpEce.javaβ€Ž

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
package nl.martijndwars.webpush;
22

3-
import org.bouncycastle.crypto.digests.SHA256Digest;
4-
import org.bouncycastle.crypto.generators.HKDFBytesGenerator;
5-
import org.bouncycastle.crypto.params.HKDFParameters;
6-
import org.bouncycastle.jce.interfaces.ECPrivateKey;
7-
import org.bouncycastle.jce.interfaces.ECPublicKey;
8-
93
import javax.crypto.*;
104
import javax.crypto.spec.GCMParameterSpec;
115
import javax.crypto.spec.SecretKeySpec;
126
import java.nio.ByteBuffer;
137
import java.security.*;
148
import java.util.Arrays;
159
import java.util.Base64;
16-
import java.util.HashMap;
1710
import java.util.Map;
1811

12+
import org.bouncycastle.crypto.digests.SHA256Digest;
13+
import org.bouncycastle.crypto.generators.HKDFBytesGenerator;
14+
import org.bouncycastle.crypto.params.HKDFParameters;
15+
import org.bouncycastle.jce.interfaces.ECPrivateKey;
16+
import org.bouncycastle.jce.interfaces.ECPublicKey;
17+
1918
import static java.nio.charset.StandardCharsets.UTF_8;
19+
2020
import static javax.crypto.Cipher.DECRYPT_MODE;
2121
import static javax.crypto.Cipher.ENCRYPT_MODE;
22+
2223
import static nl.martijndwars.webpush.Utils.*;
2324

2425
// TODO: Support multiple records (not needed for Web Push)
@@ -32,7 +33,8 @@
3233
* [1] <a href="https://tools.ietf.org/html/draft-ietf-httpbis-encryption-encoding-01">...</a>
3334
* [2] <a href="https://tools.ietf.org/html/draft-ietf-httpbis-encryption-encoding-09">...</a>
3435
*/
35-
public class HttpEce {
36+
public final class HttpEce {
37+
3638
public static final int KEY_LENGTH = 16;
3739
public static final int SHA_256_LENGTH = 32;
3840
public static final int TAG_SIZE = 16;
@@ -43,11 +45,11 @@ public class HttpEce {
4345
private final Map<String, String> labels;
4446

4547
public HttpEce() {
46-
this(new HashMap<>(), new HashMap<>());
48+
this(Map.of(), Map.of());
4749
}
4850

49-
public HttpEce(Map<String, KeyPair> keys, Map<String, String> labels) {
50-
this.keys = keys;
51+
public HttpEce(final Map<String, KeyPair> keys, final Map<String, String> labels) {
52+
this.keys = keys;
5153
this.labels = labels;
5254
}
5355

@@ -57,15 +59,15 @@ public HttpEce(Map<String, KeyPair> keys, Map<String, String> labels) {
5759
* @param plaintext Payload to encrypt.
5860
* @param salt A random 16-byte buffer
5961
* @param privateKey A private key to encrypt this message with (Web Push: the local private key)
60-
* @param keyid An identifier for the local key. Only applies to AESGCM. For AES128GCM, the header contains the keyid.
62+
* @param keyId An identifier for the local key. Only applies to AESGCM. For AES128GCM, the header contains the keyId.
6163
* @param dh An Elliptic curve Diffie-Hellman public privateKey on the P-256 curve (Web Push: the user's keys.p256dh)
6264
* @param authSecret An authentication secret (Web Push: the user's keys.auth)
6365
*/
64-
public byte[] encrypt(byte[] plaintext, byte[] salt, byte[] privateKey, String keyid, ECPublicKey dh,
66+
public byte[] encrypt(byte[] plaintext, byte[] salt, byte[] privateKey, String keyId, ECPublicKey dh,
6567
byte[] authSecret, Encoding version) throws GeneralSecurityException {
6668
log("encrypt", plaintext);
6769

68-
byte[][] keyAndNonce = deriveKeyAndNonce(salt, privateKey, keyid, dh, authSecret, version, ENCRYPT_MODE);
70+
byte[][] keyAndNonce = deriveKeyAndNonce(salt, privateKey, keyId, dh, authSecret, version, ENCRYPT_MODE);
6971
byte[] key = keyAndNonce[0];
7072
byte[] nonce = keyAndNonce[1];
7173

@@ -76,7 +78,7 @@ public byte[] encrypt(byte[] plaintext, byte[] salt, byte[] privateKey, String k
7678

7779
// For AES128GCM suffix {0x02}, for AESGCM prefix {0x00, 0x00}.
7880
if (version == Encoding.AES_128_GCM) {
79-
byte[] header = buildHeader(salt, keyid);
81+
byte[] header = buildHeader(salt, keyId);
8082
log("header", header);
8183

8284
byte[] padding = new byte[] { 2 };
@@ -127,10 +129,10 @@ public byte[][] parseHeader(byte[] payload) {
127129
byte[] body = Arrays.copyOfRange(payload, 21 + keyIdLength, payload.length);
128130

129131
return new byte[][] {
130-
salt,
131-
recordSize,
132-
keyId,
133-
body
132+
salt,
133+
recordSize,
134+
keyId,
135+
body
134136
};
135137
}
136138

@@ -353,18 +355,17 @@ private byte[][] extractDH(String keyid, ECPublicKey publicKey) throws NoSuchAl
353355
/**
354356
* Get the public key for the given keyid.
355357
*/
356-
private ECPublicKey getPublicKey(String keyid) {
357-
return (ECPublicKey) keys.get(keyid).getPublic();
358+
private ECPublicKey getPublicKey(String keyId) {
359+
return (ECPublicKey) keys.get(keyId).getPublic();
358360
}
359361

360362
/**
361363
* Get the private key for the given keyid.
362364
*/
363-
private ECPrivateKey getPrivateKey(String keyid) {
364-
return (ECPrivateKey) keys.get(keyid).getPrivate();
365+
private ECPrivateKey getPrivateKey(String keyId) {
366+
return (ECPrivateKey) keys.get(keyId).getPrivate();
365367
}
366368

367-
368369
/**
369370
* Encode the public key as a byte array and prepend its length in two bytes.
370371
*/

β€Žwebpush/src/main/java/nl/martijndwars/webpush/Notification.javaβ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* @param userPublicKey The client's public key
1818
* @param userAuth The client's auth
1919
* @param payload An arbitrary payload
20-
* @param urgency Push Message Urgency <a href="https://tools.ietf.org/html/rfc8030#section-5.3">Push Message Urgency</a>
21-
* @param topic Push Message Topic <a href="https://tools.ietf.org/html/rfc8030#section-5.4">Replacing Push Messages</a>
20+
* @param urgency <a href="https://tools.ietf.org/html/rfc8030#section-5.3">Push Message Urgency</a>
21+
* @param topic <a href="https://tools.ietf.org/html/rfc8030#section-5.4">Replacing Push Messages</a>
2222
* @param ttl Time in seconds that the push message is retained by the push service
2323
*/
2424
public record Notification(URI endpoint, ECPublicKey userPublicKey, byte[] userAuth, byte[] payload, Urgency urgency,
@@ -41,7 +41,7 @@ public boolean hasTopic() {
4141
}
4242

4343
/**
44-
* Detect if the notification is for a GCM-based subscription
44+
* Detects if this {@code Notification} is for a GCM-based subscription.
4545
*/
4646
public boolean isGcm() {
4747
return endpoint().toString().startsWith("https://android.googleapis.com/gcm/send");

β€Žwebpush/src/main/java/nl/martijndwars/webpush/Utils.javaβ€Ž

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package nl.martijndwars.webpush;
22

3+
import java.math.BigInteger;
4+
import java.nio.ByteBuffer;
5+
import java.security.*;
6+
import java.security.spec.InvalidKeySpecException;
7+
import java.util.Base64;
8+
39
import org.bouncycastle.jce.ECNamedCurveTable;
410
import org.bouncycastle.jce.interfaces.ECPrivateKey;
511
import org.bouncycastle.jce.interfaces.ECPublicKey;
@@ -11,15 +17,9 @@
1117
import org.bouncycastle.math.ec.ECPoint;
1218
import org.bouncycastle.util.BigIntegers;
1319

14-
import java.math.BigInteger;
15-
import java.nio.ByteBuffer;
16-
import java.security.*;
17-
import java.security.spec.InvalidKeySpecException;
18-
import java.util.Base64;
19-
2020
import static org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME;
2121

22-
public class Utils {
22+
public final class Utils {
2323

2424
public static final String CURVE = "prime256v1";
2525

0 commit comments

Comments
 (0)