Skip to content
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
2 changes: 1 addition & 1 deletion .ci/ci_check.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

set -e
# check code format
bash gradlew verifyGoogleJavaFormat
bash gradlew build

8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ dependencies {
compile 'org.apache.commons:commons-lang3:3.1'
compile 'io.netty:netty-all:4.1.50.Final'
compile 'com.fasterxml.jackson.core:jackson-databind:2.11.0'
compile group: 'commons-codec', name: 'commons-codec', version: '1.14'


compile 'org.slf4j:slf4j-api:1.7.30'
compile files('lib/pkey-sign.jar')

testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.23.0'

}

archivesBaseName = 'java-sdk'
group = 'org.fisco-bcos'
version = '1.0.0-SNAPSHOT'
version = '1.0.0-SNAPSHOT'
Binary file added lib/pkey-sign.jar
Binary file not shown.
22 changes: 22 additions & 0 deletions src/main/java/org/fisco/bcos/sdk/crypto/hash/Hash.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright 2014-2020 [fisco-dev]
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/

/** interface for hash calculation */
package org.fisco.bcos.sdk.crypto.hash;

public interface Hash {
String hash(final String inputData);

byte[] hash(final byte[] inputBytes);
}
43 changes: 43 additions & 0 deletions src/main/java/org/fisco/bcos/sdk/crypto/hash/Keccak256.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright 2014-2020 [fisco-dev]
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fisco.bcos.sdk.crypto.hash;

import com.webank.wedpr.crypto.CryptoResult;
import com.webank.wedpr.crypto.NativeInterface;
import org.fisco.bcos.sdk.exceptions.HashException;
import org.fisco.bcos.sdk.utils.Hex;

public class Keccak256 implements Hash {

@Override
public String hash(final String inputData) {
return calculateHash(inputData.getBytes());
}

@Override
public byte[] hash(final byte[] inputBytes) {
return Hex.decode(calculateHash(inputBytes));
}

private String calculateHash(final byte[] inputBytes) {
// Note: the exceptions should be handled by the caller
CryptoResult hashResult = NativeInterface.keccak256(Hex.toHexString(inputBytes));
if (hashResult.wedprErrorMessage != null && !hashResult.wedprErrorMessage.isEmpty()) {
throw new HashException(
"Calculate hash with keccak256 failed! error message:"
+ hashResult.wedprErrorMessage);
}
return hashResult.hash;
}
}
43 changes: 43 additions & 0 deletions src/main/java/org/fisco/bcos/sdk/crypto/hash/SM3Hash.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright 2014-2020 [fisco-dev]
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fisco.bcos.sdk.crypto.hash;

import com.webank.wedpr.crypto.CryptoResult;
import com.webank.wedpr.crypto.NativeInterface;
import org.fisco.bcos.sdk.exceptions.HashException;
import org.fisco.bcos.sdk.utils.Hex;

public class SM3Hash implements Hash {
@Override
public String hash(final String inputData) {
return calcualteHash(inputData.getBytes());
}

@Override
public byte[] hash(final byte[] inputBytes) {
// Considering inefficient string conversion, this interface is not recommended
return Hex.decode(calcualteHash(inputBytes));
}

private String calcualteHash(final byte[] inputBytes) {
CryptoResult hashResult = NativeInterface.sm3(Hex.toHexString(inputBytes));
// call sm3 failed
if (hashResult.wedprErrorMessage != null && !hashResult.wedprErrorMessage.isEmpty()) {
throw new HashException(
"calculate hash with sm3 failed, error message:"
+ hashResult.wedprErrorMessage);
}
return hashResult.hash;
}
}
105 changes: 105 additions & 0 deletions src/main/java/org/fisco/bcos/sdk/crypto/keypair/CryptoKeyPair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* Copyright 2014-2020 [fisco-dev]
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fisco.bcos.sdk.crypto.keypair;

import com.webank.wedpr.crypto.CryptoResult;
import java.math.BigInteger;
import java.util.Objects;

public abstract class CryptoKeyPair {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead to identify a "CryptoKeyPair" by yourself, you can consider use 'java.security.KeyPair'.
The 'org.bouncycastle' package can help you read KeyPair from PEM file and P12 file. If you would like to use this function, maybe you can consider to use 'java.security.KeyPair' directly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I will fix it in the next PR.

private BigInteger privateKey;
protected BigInteger publicKey;

protected String hexPrivateKey;
protected String hexPublicKey;

public CryptoKeyPair() {}

public CryptoKeyPair(final BigInteger privateKey) {
this.privateKey = privateKey;
/**
* todo: get publicKey according to privateKey this.publicKey =
* privateKeyToPublic(privateKey);
*/
calculateHexedKeyPair();
}

public CryptoKeyPair(final BigInteger privateKey, final BigInteger publicKey) {
this.privateKey = privateKey;
this.publicKey = publicKey;
calculateHexedKeyPair();
}

private void calculateHexedKeyPair() {
this.hexPrivateKey = this.privateKey.toString(16);
this.hexPublicKey = this.publicKey.toString(16);
}

/**
* get CryptoKeyPair information from CryptoResult
*
* @param nativeResult
*/
CryptoKeyPair(final CryptoResult nativeResult) {
this.hexPrivateKey = nativeResult.privteKey;
this.hexPublicKey = nativeResult.publicKey;
this.privateKey = new BigInteger(this.hexPrivateKey, 16);
this.publicKey = new BigInteger(this.hexPublicKey, 16);
}

public BigInteger getPrivateKey() {
return privateKey;
}

public BigInteger getPublicKey() {
return publicKey;
}

public String getHexPrivateKey() {
return hexPrivateKey;
}

public String getHexPublicKey() {
return hexPublicKey;
}

/**
* todo: get the public key from the given private key
*
* @param privateKey
* @return: the public key calculated from the private key public abstract BigInteger
* privateKeyToPublic(BigInteger privateKey);
*/

/**
* generate keyPair randomly
*
* @return: the generated keyPair
*/
public abstract CryptoKeyPair generateKeyPair();

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CryptoKeyPair keyPair = (CryptoKeyPair) o;
return Objects.equals(privateKey, keyPair.privateKey)
&& Objects.equals(publicKey, keyPair.publicKey);
}

@Override
public int hashCode() {
return Objects.hash(privateKey, publicKey);
}
}
45 changes: 45 additions & 0 deletions src/main/java/org/fisco/bcos/sdk/crypto/keypair/ECDSAKeyPair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright 2014-2020 [fisco-dev]
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fisco.bcos.sdk.crypto.keypair;

import com.webank.wedpr.crypto.CryptoResult;
import com.webank.wedpr.crypto.NativeInterface;
import java.math.BigInteger;

public class ECDSAKeyPair extends CryptoKeyPair {

public ECDSAKeyPair() {}

public ECDSAKeyPair(BigInteger privateKey) {
super(privateKey);
}

public ECDSAKeyPair(final BigInteger privateKey, final BigInteger publicKey) {
super(privateKey, publicKey);
}

protected ECDSAKeyPair(final CryptoResult ecKeyPairInfo) {
super(ecKeyPairInfo);
}

/**
* generate keyPair randomly
*
* @return: the generated keyPair
*/
@Override
public CryptoKeyPair generateKeyPair() {
return new ECDSAKeyPair(NativeInterface.secp256k1keyPair());
}
}
35 changes: 35 additions & 0 deletions src/main/java/org/fisco/bcos/sdk/crypto/keypair/SM2KeyPair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 2014-2020 [fisco-dev]
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fisco.bcos.sdk.crypto.keypair;

import com.webank.wedpr.crypto.CryptoResult;
import com.webank.wedpr.crypto.NativeInterface;

public class SM2KeyPair extends CryptoKeyPair {
public SM2KeyPair() {}

protected SM2KeyPair(CryptoResult sm2keyPairInfo) {
super(sm2keyPairInfo);
}

/**
* generate keyPair randomly
*
* @return: the generated keyPair
*/
@Override
public CryptoKeyPair generateKeyPair() {
return new SM2KeyPair(NativeInterface.sm2keyPair());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2014-2020 [fisco-dev]
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fisco.bcos.sdk.crypto.signature;

import com.webank.pkeysign.service.ECCSignService;
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.exceptions.SignatureException;

public class ECDSASignature implements Signature {

public static final ECCSignService eccSignService = new ECCSignService();

@Override
public SignatureResult sign(final String message, final CryptoKeyPair keyPair) {
String signature = eccSignService.sign(message, keyPair.getHexPrivateKey());
if (signature == null) {
throw new SignatureException("Sign with secp256k1 failed");
}
// convert signature string to SignatureResult struct
return new ECDSASignatureResult(signature);
}

@Override
public SignatureResult sign(final byte[] message, final CryptoKeyPair keyPair) {
return sign(new String(message), keyPair);
}

@Override
public boolean verify(final String publicKey, final String message, final String signature) {
return eccSignService.verify(message, signature, publicKey);
}

@Override
public boolean verify(final String publicKey, final byte[] message, final byte[] signature) {
return verify(publicKey, new String(message), new String(signature));
}
}
Loading