-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGenerateKeyTest.java
More file actions
42 lines (31 loc) · 1.33 KB
/
GenerateKeyTest.java
File metadata and controls
42 lines (31 loc) · 1.33 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
package io.token.banksample;
import com.google.common.hash.Hashing;
import io.token.security.crypto.Crypto;
import io.token.security.crypto.CryptoRegistry;
import io.token.security.crypto.CryptoType;
import java.security.KeyPair;
import io.token.util.codec.ByteEncoding;
import org.junit.Test;
public class GenerateKeyTest {
@Test
public void generate() {
Crypto crypto = CryptoRegistry.getInstance().cryptoFor(CryptoType.EDDSA);
KeyPair keyPair = crypto.generateKeyPair();
System.out.println("crypto: " + crypto.getAlgorithm().toUpperCase());
System.out.println("private-key: "
+ crypto.serialize(keyPair.getPrivate())
+ " // Used for signing bank auth payloads");
System.out.println("public-key: "
+ crypto.serialize(keyPair.getPublic())
+ " // Give to Token so that Token can verify bank auth payloads");
System.out.println("Key-ID for public key: " +keyIdFor(crypto.serialize(keyPair.getPublic())));
}
static String keyIdFor(String serializedKey) {
byte[] encoded = ByteEncoding.parse(serializedKey);
byte[] hash = Hashing.sha256().newHasher()
.putBytes(encoded)
.hash()
.asBytes();
return ByteEncoding.serialize(hash).substring(0, 16);
}
}