-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathKeysExamples.java
More file actions
103 lines (82 loc) · 3.92 KB
/
KeysExamples.java
File metadata and controls
103 lines (82 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package waltid;
import id.walt.crypto.keys.Key;
import id.walt.crypto.keys.KeyType;
import id.walt.crypto.keys.jwk.JWKKey;
import kotlin.Result;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
public class KeysExamples {
private static final byte[] plaintext = "< this is my plaintext>".getBytes(StandardCharsets.UTF_8);
// The following two functions generate a new (local) JWK Key and use it to sign raw data,
// with the first function demonstrating the Java Blocking API, and the second function
// demonstrating the Java (async) CompletableFuture API.
// Replace KeyType.Ed25519 with a different KeyType if desired.
public static void signBlocking() throws Exception {
System.out.println("Generating key synchronous...");
JWKKey k = (JWKKey) JWKKey.Companion.generateBlocking(KeyType.Ed25519, null);
System.out.println("Sync generated key: " + k);
System.out.println("Signing with key synchronous...");
var signed = (byte[]) k.signRawBlocking(plaintext, null);
System.out.println("Signed synchronous: " + Arrays.toString(signed));
verifyAsync(k, signed, plaintext, "Test sync verification");
System.out.println("Test a verification failure...");
byte[] invalid = new byte[signed.length + 1];
System.arraycopy(signed, 0, invalid, 0, signed.length);
invalid[signed.length] = Integer.valueOf(1).byteValue();
verifyAsync(k, signed, invalid, "Test verification failure");
}
public static void signAsync() {
System.out.println("Generating key asynchronous...");
// join Futures to make sure they execute even when the program terminates earlier
JWKKey.Companion.generateAsync(KeyType.Ed25519, null).thenAccept(key -> {
System.out.println("Async generated key: " + key);
System.out.println("Signing with key asynchronous...");
try {
key.signRawAsync(plaintext, null).thenAccept(signed -> {
System.out.println("Signed asynchronous: " + Arrays.toString((byte[]) signed));
try {
verifyAsync(key, (byte[]) signed, plaintext, "Test async verification");
} catch (Exception e) {
throw new RuntimeException(e);
}
});
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
public static void verifyAsync(Key key, byte[] signed, byte[] plaintext, String message) throws Exception {
key.getPublicKeyAsync().thenAccept(publicKey -> {
try {
publicKey.verifyRawAsync(signed, plaintext, null).thenAccept(result -> {
System.out.println("Verification result (" + message + "): " + result);
}).join();
} catch (Exception e) {
throw new RuntimeException(e);
}
}).join();
}
public static String exportKey() throws Exception {
// Other KeyTypes:
var key2 = JWKKey.Companion.generateBlocking(KeyType.secp256r1, null);
System.out.println("Export key...");
String jwkExport = key2.exportJWKBlocking();
System.out.println("Different KeyType, exported: " + jwkExport);
return jwkExport;
}
public static void importKey(String jwk) throws ExecutionException, InterruptedException {
System.out.println("Import key...");
Result<JWKKey> keyImport = JWKKey.Companion.importJWKAsync(jwk).get();
System.out.println("Import result: " + keyImport);
}
public static void runKeyExample() throws Exception {
KeysExamples.signAsync();
KeysExamples.signBlocking();
var jwk = exportKey();
importKey(jwk);
}
public static void main(String[] args) throws Exception {
runKeyExample();
}
}