Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,25 @@

<dependencies>
<dependency>
<groupId>io.github.nbaars</groupId>
<groupId>${project.groupId}</groupId>
<artifactId>paseto4j-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.github.nbaars</groupId>
<groupId>${project.groupId}</groupId>
<artifactId>paseto4j-version1</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Start: only for Paseto Version 2 -->
<dependency>
<groupId>io.github.nbaars</groupId>
<groupId>${project.groupId}</groupId>
<artifactId>paseto4j-version2</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>paseto4j-version3</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tuweni</groupId>
<artifactId>tuweni-bytes</artifactId>
Expand All @@ -51,6 +55,9 @@
<artifactId>tuweni-crypto</artifactId>
<version>2.3.1</version>
</dependency>
<!-- End: only for Paseto Version 2 -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
* SOFTWARE.
*/

package org.paseto4j.version2;
package org.paseto4j.examples;

import static org.paseto4j.commons.Version.V1;

import java.security.*;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.SignatureException;
import org.paseto4j.commons.PrivateKey;
import org.paseto4j.commons.PublicKey;
import org.paseto4j.commons.SecretKey;
Expand Down Expand Up @@ -85,7 +89,8 @@ private static KeyPair generateKeyPair() {
}

private static void exampleV1Local() {
byte[] secretKey = SecureRandom.getSeed(32);
byte[] secretKey = new byte[32];
secretKey = SecureRandom.getSeed(32);
String encryptedToken = Paseto.encrypt(new SecretKey(secretKey, V1), TOKEN, FOOTER);
System.out.println("Encrypted token is: " + encryptedToken);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.paseto4j.version2;
package org.paseto4j.examples;

import java.security.SignatureException;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.crypto.sodium.Signature;
import org.paseto4j.commons.PrivateKey;
import org.paseto4j.commons.PublicKey;
import org.paseto4j.commons.Version;
import org.paseto4j.version2.Paseto;

public class Version2 {

Expand Down
76 changes: 76 additions & 0 deletions examples/src/main/java/org/paseto4j/examples/Version3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.paseto4j.examples;

import static org.paseto4j.commons.Version.V3;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.SignatureException;
import java.security.spec.ECGenParameterSpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.paseto4j.commons.PrivateKey;
import org.paseto4j.commons.PublicKey;
import org.paseto4j.commons.SecretKey;
import org.paseto4j.version3.Paseto;

public class Version3 {

private static final String TOKEN =
"{\"data\":\"this is a signed message\",\"expires\":\"2019-01-01T00:00:00+00:00\"}";
private static final String FOOTER = "Paragon Initiative Enterprises";

public static void main(String... args) throws SignatureException {
exampleV3Local();
exampleV3Public();

try {
exampleV3PublicSignatureInvalid();
} catch (Exception e) {
System.out.println("Token is not valid");
}
}

private static void exampleV3Public() throws SignatureException {
KeyPair keyPair = generateKeyPair();

String signedToken = Paseto.sign(new PrivateKey(keyPair.getPrivate(), V3), TOKEN, FOOTER);
System.out.println("Signed token is: " + signedToken);

String token = Paseto.parse(new PublicKey(keyPair.getPublic(), V3), signedToken, FOOTER);
System.out.println("Signature is valid, token is: " + token);
}

private static void exampleV3PublicSignatureInvalid() throws SignatureException {
KeyPair keyPair1 = generateKeyPair();
KeyPair keyPair2 = generateKeyPair();

String signedToken = Paseto.sign(new PrivateKey(keyPair1.getPrivate(), V3), TOKEN, FOOTER);
System.out.println("Signed token is: " + signedToken);

String token = Paseto.parse(new PublicKey(keyPair2.getPublic(), V3), signedToken, FOOTER);
System.out.println("Signature is valid, token is: " + token);
}

private static KeyPair generateKeyPair() {
try {
var generator = KeyPairGenerator.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME);
var spec = new ECGenParameterSpec("secp384r1");
generator.initialize(spec);
return generator.generateKeyPair();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private static void exampleV3Local() {
byte[] secretKey = new byte[32];
new SecureRandom().nextBytes(secretKey);

SecretKey key = new SecretKey(secretKey, V3);
String encryptedToken = Paseto.encrypt(key, TOKEN, FOOTER);
System.out.println("Encrypted token is: " + encryptedToken);

String decryptedToken = Paseto.decrypt(new SecretKey(secretKey, V3), encryptedToken, FOOTER);
System.out.println("Decrypted token is: " + decryptedToken);
}
}