-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include standalone HKDF implementation for v1.local tokens (#6)
* Add standalone Java v1 local tokens implementation * Add BaseV1LocalCryptoProvider Now that we have more than one option for V1 Local tokens HKDF and BC, a `BaseV1LocalCryptoProvider` has been added. This reduces what is needed to implement a V1LocalCryptoProvider to a single method. Co-authored-by: Brian Demers <[email protected]>
- Loading branch information
Showing
17 changed files
with
354 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...roovy/dev/paseto/jpaseto/crypto/bouncycastle/BouncyCastleV1LocalCryptoProviderTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2020-Present paseto.dev | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package dev.paseto.jpaseto.crypto.bouncycastle | ||
|
||
import dev.paseto.jpaseto.impl.crypto.V1LocalCryptoProvider | ||
import dev.paseto.jpaseto.lang.Keys | ||
import dev.paseto.jpaseto.lang.Services | ||
import org.testng.annotations.Test | ||
|
||
import javax.crypto.SecretKey | ||
import java.nio.charset.StandardCharsets | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat | ||
import static org.hamcrest.Matchers.equalTo | ||
import static org.hamcrest.Matchers.instanceOf | ||
class BouncyCastleV1LocalCryptoProviderTest { | ||
|
||
@Test | ||
void loadServiceTest() { | ||
assertThat Services.loadFirst(V1LocalCryptoProvider), instanceOf(BouncyCastleV1LocalCryptoProvider) | ||
} | ||
|
||
@Test | ||
void hkdfSha384Test() { | ||
SecretKey secretKey = Keys.secretKey(decode("3nQBDXcLZRTcVZF0NS/6yZ3JO03i/Yv+C1CQRvPgmJk")) | ||
byte[] salt = decode("/bvrxpG04bMH2j98Sgm5ug") | ||
byte[] info = "test-info".getBytes(StandardCharsets.UTF_8) | ||
String expectedResult = "PtiIWzWkNywvjlnyv60Rtz2Zr7vQsgZivlj0Ys9HDy4" | ||
|
||
byte[] result = new BouncyCastleV1LocalCryptoProvider().hkdfSha384(secretKey, salt, info) | ||
assertThat encodeToString(result), equalTo(expectedResult) | ||
} | ||
|
||
private static String encodeToString(byte[] bytes) { | ||
return Base64.getEncoder().withoutPadding().encodeToString(bytes) | ||
} | ||
|
||
private static byte[] decode(String input) { | ||
return Base64.getDecoder().decode(input) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright 2019-Present paseto.dev, Inc. | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
~ you may not use this file except in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>dev.paseto</groupId> | ||
<artifactId>jpaseto-root</artifactId> | ||
<version>0.5.0-SNAPSHOT</version> | ||
<relativePath>../../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>jpaseto-hkdf</artifactId> | ||
<name>JPaseto :: Crypto :: HKDF</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>dev.paseto</groupId> | ||
<artifactId>jpaseto-impl</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>at.favre.lib</groupId> | ||
<artifactId>hkdf</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.google.auto.service</groupId> | ||
<artifactId>auto-service</artifactId> | ||
<scope>provided</scope> | ||
<optional>true</optional> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
37 changes: 37 additions & 0 deletions
37
...s/crypto/hkdf/src/main/java/dev/paseto/jpaseto/crypto/hkdf/HKDFV1LocalCryptoProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2020-Present paseto.dev | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package dev.paseto.jpaseto.crypto.hkdf; | ||
|
||
import at.favre.lib.crypto.HKDF; | ||
import at.favre.lib.crypto.HkdfMacFactory; | ||
import com.google.auto.service.AutoService; | ||
import dev.paseto.jpaseto.impl.crypto.BaseV1LocalCryptoProvider; | ||
import dev.paseto.jpaseto.impl.crypto.V1LocalCryptoProvider; | ||
|
||
import javax.crypto.SecretKey; | ||
|
||
/** | ||
* @since 0.5.0 | ||
*/ | ||
@AutoService(V1LocalCryptoProvider.class) | ||
public class HKDFV1LocalCryptoProvider extends BaseV1LocalCryptoProvider { | ||
|
||
@Override | ||
protected byte[] hkdfSha384(SecretKey sharedSecret, byte[] salt, byte[] info) { | ||
HKDF hkdfSha384 = HKDF.from(new HkdfMacFactory.Default("HmacSHA384")); | ||
return hkdfSha384.extractAndExpand(salt, sharedSecret.getEncoded(), info, 32); | ||
} | ||
} |
Oops, something went wrong.