|
| 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 | +} |
0 commit comments