Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add key generotor tool #125

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,7 @@ project(":wedpr-components-service-publish").projectDir=file("wedpr-components/s

include ":wedpr-components-service-sdk-pir"
project(":wedpr-components-service-sdk-pir").projectDir=file("wedpr-components/service-sdk/pir")

include ':wedpr-components-key-generator'
project(':wedpr-components-key-generator').projectDir=file("wedpr-components/key-generator")

10 changes: 10 additions & 0 deletions wedpr-components/key-generator/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Apply the java-library plugin to add support for Java Library
plugins {
id 'org.springframework.boot' version '2.3.12.RELEASE'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}

dependencies {
implementation project(":wedpr-components-blockchain")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.webank.wedpr;

import java.security.SecureRandom;
import org.fisco.bcos.sdk.v3.crypto.CryptoSuite;
import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.v3.model.CryptoType;

public class KeyGeneratorMain {
public static void main(String[] args) {
generateECDSAKey();
generateSM2Key();
generateSymmetricKey();
}

private static void generateECDSAKey() {
CryptoSuite ecdsaCryptoSuite = new CryptoSuite(CryptoType.ECDSA_TYPE);
CryptoKeyPair ecdsaCryptoKeyPair = ecdsaCryptoSuite.getCryptoKeyPair();
String ecdsaHexPrivateKey = ecdsaCryptoKeyPair.getHexPrivateKey();
String ecdsaHexPublicKey = ecdsaCryptoKeyPair.getHexPublicKey();
System.out.println("ECDSA account is randomly generated as below:");
System.out.println("ecdsa_private_key:" + ecdsaHexPrivateKey);
System.out.println("ecdsa_public_key:" + ecdsaHexPublicKey);
}

private static void generateSM2Key() {
CryptoSuite sm2CryptoSuite = new CryptoSuite(CryptoType.SM_TYPE);
CryptoKeyPair sm2cryptoKeyPair = sm2CryptoSuite.getCryptoKeyPair();
String sm2HexPrivateKey = sm2cryptoKeyPair.getHexPrivateKey();
String sm2HexPublicKey = sm2cryptoKeyPair.getHexPublicKey();
System.out.println();
System.out.println("SM2 account is randomly generated as below:");
System.out.println("sm2_private_key:" + sm2HexPrivateKey);
System.out.println("sm2_public_key:" + sm2HexPublicKey);
}

private static void generateSymmetricKey() {
SecureRandom random = new SecureRandom();
byte[] bytes1 = new byte[16];
byte[] bytes2 = new byte[32];
random.nextBytes(bytes1);
random.nextBytes(bytes2);
String hexSymmetricKey1 = bytesToHex(bytes1);
String hexSymmetricKey2 = bytesToHex(bytes2);
System.out.println();
System.out.println("symmetric_key(16 bytes):" + hexSymmetricKey1);
System.out.println("symmetric_key(32 bytes):" + hexSymmetricKey2);
}

public static String bytesToHex(byte[] bytes) {
StringBuilder hexString = new StringBuilder();
for (byte b : bytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
}
Loading