Skip to content

Commit 9e8545c

Browse files
authored
Merge pull request #122 from elmatadorinho/master
Adding methods to load Public/Private keys from a decoded byte array.…
2 parents 84a3c28 + 5efee11 commit 9e8545c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/main/java/nl/martijndwars/webpush/Notification.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ public NotificationBuilder userPublicKey(String publicKey) throws NoSuchAlgorith
194194
return this;
195195
}
196196

197+
public NotificationBuilder userPublicKey(byte[] publicKey) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
198+
this.userPublicKey = (ECPublicKey) Utils.loadPublicKey(publicKey);
199+
return this;
200+
}
201+
197202
public NotificationBuilder userAuth(String userAuth) {
198203
this.userAuth = Base64Encoder.decode(userAuth);
199204
return this;

src/main/java/nl/martijndwars/webpush/Utils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ public static byte[] encode(ECPrivateKey privateKey) {
4646
*/
4747
public static PublicKey loadPublicKey(String encodedPublicKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
4848
byte[] decodedPublicKey = Base64Encoder.decode(encodedPublicKey);
49+
return loadPublicKey(decodedPublicKey);
50+
}
51+
52+
/**
53+
* Load the public key from a byte array.
54+
*
55+
* @param decodedPublicKey
56+
*/
57+
public static PublicKey loadPublicKey(byte[] decodedPublicKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
4958
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER_NAME);
5059
ECParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE);
5160
ECCurve curve = parameterSpec.getCurve();
@@ -66,6 +75,19 @@ public static PublicKey loadPublicKey(String encodedPublicKey) throws NoSuchProv
6675
*/
6776
public static PrivateKey loadPrivateKey(String encodedPrivateKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
6877
byte[] decodedPrivateKey = Base64Encoder.decode(encodedPrivateKey);
78+
return loadPrivateKey(decodedPrivateKey);
79+
}
80+
81+
/**
82+
* Load the private key from a byte array
83+
*
84+
* @param decodedPrivateKey
85+
* @return
86+
* @throws NoSuchProviderException
87+
* @throws NoSuchAlgorithmException
88+
* @throws InvalidKeySpecException
89+
*/
90+
public static PrivateKey loadPrivateKey(byte[] decodedPrivateKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
6991
BigInteger s = BigIntegers.fromUnsignedByteArray(decodedPrivateKey);
7092
ECParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE);
7193
ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(s, parameterSpec);

0 commit comments

Comments
 (0)