-
Notifications
You must be signed in to change notification settings - Fork 61
add crypto interface and basic implementation #11
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
43
src/main/java/org/fisco/bcos/sdk/crypto/hash/Keccak256.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
105
src/main/java/org/fisco/bcos/sdk/crypto/keypair/CryptoKeyPair.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
| 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
45
src/main/java/org/fisco/bcos/sdk/crypto/keypair/ECDSAKeyPair.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
35
src/main/java/org/fisco/bcos/sdk/crypto/keypair/SM2KeyPair.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
src/main/java/org/fisco/bcos/sdk/crypto/signature/ECDSASignature.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.