Skip to content

Commit 26d24b8

Browse files
committed
add crypto interface
1 parent df98a55 commit 26d24b8

File tree

11 files changed

+207
-5
lines changed

11 files changed

+207
-5
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ jobs:
3131
cp ./lab-core-dynamic-libs/* ./solution/vcl/src/main/resources/WeDPR_dynamic_lib/
3232
- name: compile and run ut
3333
run: bash ./gradlew clean build
34-
- name: run demo
35-
run: cd solution/vcl/dist/ && java -cp "apps/*:conf/:libs/*" com.webank.wedpr.vcl.DemoMain
34+
# - name: run demo
35+
# run: cd solution/vcl/dist/ && java -cp "apps/*:conf/:libs/*" com.webank.wedpr.vcl.DemoMain

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*.dylib
2525
*.dll
2626
*.so
27+
.DS_Store
28+
*/.DS_Store
2729

2830
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2931
hs_err_pid*

release_note.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v1.0.0

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
rootProject.name = 'WeDPR-Lab-Java-SDK'
1111
include 'common'
1212
include 'solution:vcl'
13+
include 'solution:crypto'

solution/crypto/build.gradle

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
plugins {
2+
id 'java'
3+
id 'com.github.sherter.google-java-format' version '0.8'
4+
}
5+
6+
group 'com.webank'
7+
version '1.0.0-SNAPSHOT'
8+
9+
sourceCompatibility = 1.8
10+
11+
repositories {
12+
mavenCentral()
13+
}
14+
15+
test.onlyIf { project.hasProperty('runTest') }
16+
verifyGoogleJavaFormat.onlyIf { project.hasProperty('runVerifyGoogleJavaFormat') }
17+
18+
dependencies {
19+
compile project(':common')
20+
testCompile group: 'junit', name: 'junit', version: '4.12'
21+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0.
2+
package com.webank.wedpr.crypto;
3+
4+
import com.webank.wedpr.common.WedprException;
5+
6+
/**
7+
* Client class used by WeDPR crypto. This is the main interface class for Java apps using VCL
8+
* functions.
9+
*/
10+
public class CryptoClient {
11+
12+
/**
13+
* @param pubKey
14+
* @param plaintext
15+
* @return
16+
* @throws WedprException
17+
*/
18+
public CryptoResult secp256k1EciesEncrypt(String pubKey, String plaintext) throws WedprException {
19+
return NativeInterface.secp256k1EciesEncrypt(pubKey, plaintext).expectNoError();
20+
}
21+
22+
public CryptoResult secp256k1EciesDecrypt(String priKey, String ciphertext)
23+
throws WedprException {
24+
return NativeInterface.secp256k1EciesDecrypt(priKey, ciphertext).expectNoError();
25+
}
26+
27+
public CryptoResult secp256k1GenKeyPair() throws WedprException {
28+
return NativeInterface.secp256k1GenKeyPair().expectNoError();
29+
}
30+
31+
public CryptoResult keccak256Hash(String message) throws WedprException {
32+
return NativeInterface.keccak256Hash(message).expectNoError();
33+
}
34+
35+
public CryptoResult secp256k1Sign(String priKey, String messageHash) throws WedprException {
36+
return NativeInterface.secp256k1Sign(priKey, messageHash).expectNoError();
37+
}
38+
39+
public CryptoResult secp256k1Verify(String pubKey, String messageHash, String signature)
40+
throws WedprException {
41+
return NativeInterface.secp256k1Verify(pubKey, messageHash, signature).expectNoError();
42+
}
43+
44+
// TODO: Add a getVclConfig function to expose the value of RANGE_SIZE_IN_BITS.
45+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0.
2+
3+
package com.webank.wedpr.crypto;
4+
5+
import com.webank.wedpr.common.WedprException;
6+
import com.webank.wedpr.common.WedprResult;
7+
8+
/**
9+
* Result class used by Crypto client.
10+
*
11+
* <p>This is an easy way to return multiple data from a single JNI interface.
12+
*/
13+
public class CryptoResult extends WedprResult {
14+
public String signature;
15+
public String publicKey;
16+
public String privateKey;
17+
public String hash;
18+
public boolean booleanResult;
19+
public String encryptedData;
20+
public String decryptedData;
21+
22+
/** Expects no error occurred, otherwise throws an Exception. */
23+
public CryptoResult expectNoError() throws WedprException {
24+
if (hasError()) {
25+
throw new WedprException(wedprErrorMessage);
26+
}
27+
return this;
28+
}
29+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0.
2+
3+
package com.webank.wedpr.crypto;
4+
5+
import com.webank.wedpr.common.WedprException;
6+
import java.util.Base64;
7+
8+
/**
9+
* Minimalist demo of WeDPR Crypto.
10+
*
11+
* <p>For a better interactive demo, please try our Rust version at
12+
* https://github.com/WeBankBlockchain/WeDPR-Lab-Core
13+
*/
14+
public class DemoMain {
15+
16+
public static void main(String[] args) throws Exception {
17+
CryptoClient cryptoClient = new CryptoClient();
18+
runCryptoDemo(cryptoClient);
19+
}
20+
21+
private static void runCryptoDemo(CryptoClient cryptoClient) throws WedprException {
22+
System.out.println("\n*******\nWeDPR CRYPTO RUN\n*******");
23+
System.out.println("Generate EC keyPair\n");
24+
25+
CryptoResult cryptoResult = cryptoClient.secp256k1GenKeyPair();
26+
String publicKey = cryptoResult.publicKey;
27+
String privateKey = cryptoResult.privateKey;
28+
System.out.println("public key = " + publicKey);
29+
System.out.println("private key = " + privateKey);
30+
31+
String message = Base64.getEncoder().encodeToString("WeDPR Crypto Demo".getBytes());
32+
System.out.println("Hash message = " + message + " with keccak256" + "\n");
33+
String messageHash = cryptoClient.keccak256Hash(message).hash;
34+
System.out.println("messageHash = " + messageHash);
35+
36+
System.out.println(
37+
"Sign data with message hash = " + messageHash + " by EC keyPair private key" + "\n");
38+
39+
String signature = cryptoClient.secp256k1Sign(privateKey, messageHash).signature;
40+
41+
System.out.println("Signature = " + signature);
42+
43+
System.out.println(
44+
"Verify Signature with message hash = " + messageHash + " by EC keyPair public key" + "\n");
45+
boolean result = cryptoClient.secp256k1Verify(publicKey, messageHash, signature).booleanResult;
46+
System.out.println("result = " + result);
47+
48+
System.out.println(
49+
"Encrypt data with messageHash = " + messageHash + " by EC keyPair public key" + "\n");
50+
String cipherData = cryptoClient.secp256k1EciesEncrypt(publicKey, messageHash).encryptedData;
51+
System.out.println("cipherData = " + cipherData);
52+
53+
System.out.println("Decrypt data by EC keyPair private key" + "\n");
54+
String plainData = cryptoClient.secp256k1EciesDecrypt(privateKey, cipherData).decryptedData;
55+
System.out.println("plainData = " + plainData);
56+
}
57+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0.
2+
3+
package com.webank.wedpr.crypto;
4+
5+
import com.webank.wedpr.common.NativeUtils;
6+
import java.io.IOException;
7+
8+
/** Native interface for VCL client. */
9+
public class NativeInterface {
10+
11+
static {
12+
try {
13+
// Load the dynamic library of VCL on different operating systems.
14+
String osName = System.getProperty("os.name").toLowerCase();
15+
String libPathInJar;
16+
if (osName.contains("windows")) {
17+
libPathInJar = "/WeDPR_dynamic_lib/ffi_java_crypto.dll";
18+
} else if (osName.contains("linux")) {
19+
libPathInJar = "/WeDPR_dynamic_lib/libffi_java_crypto.so";
20+
} else if (osName.contains("mac")) {
21+
libPathInJar = "/WeDPR_dynamic_lib/libffi_java_crypto.dylib";
22+
} else {
23+
throw new IOException(String.format("Operating system %s is not supported.", osName));
24+
}
25+
NativeUtils.loadLibraryFromJar(libPathInJar);
26+
} catch (IOException e) {
27+
// TODO: Provide more instructions on resolving dynamic library loading errors.
28+
throw new RuntimeException(e);
29+
}
30+
}
31+
32+
// JNI function section.
33+
public static native CryptoResult secp256k1EciesEncrypt(String pubKey, String plaintext);
34+
35+
public static native CryptoResult secp256k1EciesDecrypt(String priKey, String ciphertext);
36+
37+
public static native CryptoResult secp256k1GenKeyPair();
38+
39+
public static native CryptoResult keccak256Hash(String message);
40+
41+
public static native CryptoResult secp256k1Sign(String priKey, String messageHash);
42+
43+
public static native CryptoResult secp256k1Verify(
44+
String pubKey, String messageHash, String signature);
45+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Have a good day :D

0 commit comments

Comments
 (0)