A Java library that provides cryptographic utilities for generating keypairs, signing and verifying JSON Web Signatures (JWS), and deriving account IDs for use with Fluree.
- Generate Keypair: Generate a secp256k1 keypair.
- Create JWS: Sign a payload using a private key and create a JWS.
- Verify JWS: Verify a JWS and retrieve the payload and public key used to sign it.
- Derive Public Key: Derive a public key from a private key.
- Derive Account ID from Private Key: Derive a Fluree-compatible account ID from a private key.
- Derive Account ID from Public Key: Derive a Fluree-compatible account ID from a public key.
Add the following dependency to your pom.xml
:
<dependency>
<groupId>com.fluree</groupId>
<artifactId>crypto</artifactId>
<version>1.0.0</version>
</dependency>
Add the following dependency to your build.gradle
:
implementation 'com.fluree:crypto:1.0.0'
You can generate a secp256k1 keypair using the generateKeyPair
method.
import com.fluree.crypto.Crypto;
import com.fluree.crypto.Keypair;
public class Example {
public static void main(String[] args) throws Exception {
Keypair keypair = Crypto.generateKeyPair();
System.out.println("Private Key: " + keypair.getPrivateKey());
System.out.println("Public Key: " + keypair.getPublicKey());
}
}
Sign a payload using a private key and create a JWS.
import com.fluree.crypto.Crypto;
public class Example {
public static void main(String[] args) throws Exception {
Keypair keypair = Crypto.generateKeyPair();
String privateKey = keypair.getPrivateKey();
String payload = "{\"foo\":\"bar\"}";
String jws = Crypto.createJws(payload, privateKey);
System.out.println("JWS: " + jws);
}
}
Verify a JWS and retrieve the payload and public key used to sign it.
import com.fluree.crypto.Crypto;
import java.util.HashMap;
public class Example {
public static void main(String[] args) throws Exception {
Keypair keypair = Crypto.generateKeyPair();
String privateKey = keypair.getPrivateKey();
String publicKey = keypair.getPublicKey();
String payload = "{\"foo\":\"bar\"}";
String jws = Crypto.createJws(payload, privateKey);
HashMap<String, String> result = Crypto.verifyJws(jws);
System.out.println("Payload: " + result.get("payload"));
System.out.println("Public Key: " + result.get("pubkey"));
Assert.assertEquals(result.get("payload"), payload);
Assert.assertEquals(result.get("pubkey"), publicKey);
}
}
Derive the public key from a private key in hexadecimal format.
import com.fluree.crypto.Crypto;
public class Example {
public static void main(String[] args) {
Keypair keypair = Crypto.generateKeyPair();
String privateKey = keypair.getPrivateKey();
String publicKey = Crypto.pubKeyFromPrivate(privateKey);
System.out.println("Public Key: " + publicKey);
}
}
Derive a Fluree-compatible account ID from a private key.
import com.fluree.crypto.Crypto;
public class Example {
public static void main(String[] args) throws Exception {
Keypair keypair = Crypto.generateKeyPair();
String privateKey = keypair.getPrivateKey();
String accountId = Crypto.accountIdFromPrivateKey(privateKey);
System.out.println("Account ID: " + accountId);
}
}
Derive a Fluree-compatible account ID from a public key.
import com.fluree.crypto.Crypto;
public class Example {
public static void main(String[] args) throws Exception {
Keypair keypair = Crypto.generateKeyPair();
String privateKey = keypair.getPrivateKey();
String publicKey = Crypto.pubKeyFromPrivate(privateKey);
String accountId = Crypto.accountIdFromPublicKey(publicKey);
System.out.println("Account ID: " + accountId);
}
}
- Description: Creates a JWS (JSON Web Signature) using the provided payload and private key.
- Parameters:
payload
: The stringified payload to sign.privateKeyHex
: The private key used to sign the payload (in hexadecimal format).
- Returns: A JWS string.
- Throws: Exception if the JWS creation fails.
- Description: Verifies a JWS and returns a map containing the payload and public key.
- Parameters:
jwsString
: The JWS string to verify.
- Returns:
- A
HashMap<String, String>
containing thepayload
andpubkey
. null
if the verification fails.
- A
- Description: Generates a secp256k1 keypair.
- Returns: A
Keypair
object containing the public and private keys. - Throws: Exception if keypair generation fails.
- Description: Derives the public key from a private key.
- Parameters:
privateKeyHex
: The private key (in hexadecimal format).
- Returns: The public key (in hexadecimal format).
- Description: Derives a Fluree-compatible account ID from a private key.
- Parameters:
privateKeyHex
: The private key (in hexadecimal format).
- Returns: The account ID (in base58 encoding).
- Throws: Exception if the account ID derivation fails.
- Description: Derives a Fluree-compatible account ID from a public key.
- Parameters:
publicKey
: The public key (in hexadecimal format).
- Returns: The account ID (in base58 encoding).
- Throws: Exception if the account ID derivation fails.
To build the project, use Maven:
mvn clean install
To run tests, use:
mvn test
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
Happy coding! 🌟