From 262124385dea6bf6cca306d642c86d94d3186734 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Fri, 5 Feb 2016 16:54:45 +0100 Subject: [PATCH] Move Crypto.kt into core/crypto/CryptoUtilities.kt --- src/main/java/contracts/JavaCommercialPaper.java | 5 +++-- src/main/java/core/crypto/Base58.java | 4 +--- .../core/crypto/CryptoUtilities.kt} | 15 ++++++++------- src/main/kotlin/contracts/Cash.kt | 2 ++ src/main/kotlin/contracts/CommercialPaper.kt | 3 +++ src/main/kotlin/contracts/CrowdFund.kt | 1 + src/main/kotlin/contracts/DummyContract.kt | 2 +- .../contracts/protocols/TwoPartyTradeProtocol.kt | 1 + src/main/kotlin/core/Services.kt | 1 + src/main/kotlin/core/Structures.kt | 2 ++ src/main/kotlin/core/TransactionVerification.kt | 1 + src/main/kotlin/core/Transactions.kt | 3 +++ src/main/kotlin/core/messaging/InMemoryNetwork.kt | 2 +- src/main/kotlin/core/messaging/StateMachines.kt | 4 ++-- .../kotlin/core/node/TimestamperNodeService.kt | 2 ++ src/main/kotlin/core/node/TraderDemo.kt | 1 + src/main/kotlin/core/serialization/Kryo.kt | 4 ++-- src/test/kotlin/contracts/CashTests.kt | 1 + src/test/kotlin/contracts/CommercialPaperTests.kt | 1 + src/test/kotlin/contracts/CrowdFundTests.kt | 1 + src/test/kotlin/core/MockServices.kt | 2 ++ src/test/kotlin/core/TransactionGroupTests.kt | 1 + .../core/node/TimestamperNodeServiceTest.kt | 1 + .../TransactionSerializationTests.kt | 1 + src/test/kotlin/core/testutils/TestUtils.kt | 3 +++ .../core/visualiser/GroupToGraphConversion.kt | 2 +- 26 files changed, 47 insertions(+), 19 deletions(-) rename src/main/{kotlin/core/Crypto.kt => java/core/crypto/CryptoUtilities.kt} (90%) diff --git a/src/main/java/contracts/JavaCommercialPaper.java b/src/main/java/contracts/JavaCommercialPaper.java index faa4e54dbe..a2b69e02e3 100644 --- a/src/main/java/contracts/JavaCommercialPaper.java +++ b/src/main/java/contracts/JavaCommercialPaper.java @@ -10,6 +10,7 @@ package contracts; import core.*; import core.TransactionForVerification.*; +import core.crypto.*; import org.jetbrains.annotations.*; import java.security.*; @@ -26,7 +27,7 @@ import static kotlin.collections.CollectionsKt.*; * */ public class JavaCommercialPaper implements Contract { - public static SecureHash JCP_PROGRAM_ID = SecureHash.Companion.sha256("java commercial paper (this should be a bytecode hash)"); + public static SecureHash JCP_PROGRAM_ID = SecureHash.sha256("java commercial paper (this should be a bytecode hash)"); public static class State implements ContractState, ICommercialPaperState { private PartyReference issuance; @@ -217,7 +218,7 @@ public class JavaCommercialPaper implements Contract { @Override public SecureHash getLegalContractReference() { // TODO: Should return hash of the contract's contents, not its URI - return SecureHash.Companion.sha256("https://en.wikipedia.org/wiki/Commercial_paper"); + return SecureHash.sha256("https://en.wikipedia.org/wiki/Commercial_paper"); } public TransactionBuilder generateIssue(@NotNull PartyReference issuance, @NotNull Amount faceValue, @Nullable Instant maturityDate) { diff --git a/src/main/java/core/crypto/Base58.java b/src/main/java/core/crypto/Base58.java index a943d2500d..c7c7a3ca48 100644 --- a/src/main/java/core/crypto/Base58.java +++ b/src/main/java/core/crypto/Base58.java @@ -8,8 +8,6 @@ package core.crypto; -import core.*; - import java.math.*; import java.util.*; @@ -145,7 +143,7 @@ public class Base58 { throw new AddressFormatException("Input too short"); byte[] data = Arrays.copyOfRange(decoded, 0, decoded.length - 4); byte[] checksum = Arrays.copyOfRange(decoded, decoded.length - 4, decoded.length); - byte[] actualChecksum = Arrays.copyOfRange(SecureHash.Companion.sha256Twice(data).getBits(), 0, 4); + byte[] actualChecksum = Arrays.copyOfRange(SecureHash.sha256Twice(data).getBits(), 0, 4); if (!Arrays.equals(checksum, actualChecksum)) throw new AddressFormatException("Checksum does not validate"); return data; diff --git a/src/main/kotlin/core/Crypto.kt b/src/main/java/core/crypto/CryptoUtilities.kt similarity index 90% rename from src/main/kotlin/core/Crypto.kt rename to src/main/java/core/crypto/CryptoUtilities.kt index 57900d756e..01d8ff5d4d 100644 --- a/src/main/kotlin/core/Crypto.kt +++ b/src/main/java/core/crypto/CryptoUtilities.kt @@ -6,10 +6,10 @@ * All other rights reserved. */ -package core +package core.crypto import com.google.common.io.BaseEncoding -import core.crypto.Base58 +import core.Party import core.serialization.OpaqueBytes import java.math.BigInteger import java.security.* @@ -28,18 +28,19 @@ sealed class SecureHash(bits: ByteArray) : OpaqueBytes(bits) { // Like static methods in Java, except the 'companion' is a singleton that can have state. companion object { + @JvmStatic fun parse(str: String) = BaseEncoding.base16().decode(str.toLowerCase()).let { when (it.size) { - 32 -> SecureHash.SHA256(it) + 32 -> SHA256(it) else -> throw IllegalArgumentException("Provided string is not 32 bytes in base 16 (hex): $str") } } - fun sha256(bits: ByteArray) = SHA256(MessageDigest.getInstance("SHA-256").digest(bits)) - fun sha256Twice(bits: ByteArray) = sha256(sha256(bits).bits) - fun sha256(str: String) = sha256(str.toByteArray()) + @JvmStatic fun sha256(bits: ByteArray) = SHA256(MessageDigest.getInstance("SHA-256").digest(bits)) + @JvmStatic fun sha256Twice(bits: ByteArray) = sha256(sha256(bits).bits) + @JvmStatic fun sha256(str: String) = sha256(str.toByteArray()) - fun randomSHA256() = sha256(SecureRandom.getInstanceStrong().generateSeed(32)) + @JvmStatic fun randomSHA256() = sha256(SecureRandom.getInstanceStrong().generateSeed(32)) } abstract val signatureAlgorithmName: String diff --git a/src/main/kotlin/contracts/Cash.kt b/src/main/kotlin/contracts/Cash.kt index 4b2769321f..fc75f2a904 100644 --- a/src/main/kotlin/contracts/Cash.kt +++ b/src/main/kotlin/contracts/Cash.kt @@ -9,6 +9,8 @@ package contracts import core.* +import core.crypto.SecureHash +import core.crypto.toStringShort import core.utilities.Emoji import java.security.PublicKey import java.security.SecureRandom diff --git a/src/main/kotlin/contracts/CommercialPaper.kt b/src/main/kotlin/contracts/CommercialPaper.kt index bf905f7db1..91645c2a7d 100644 --- a/src/main/kotlin/contracts/CommercialPaper.kt +++ b/src/main/kotlin/contracts/CommercialPaper.kt @@ -9,6 +9,9 @@ package contracts import core.* +import core.crypto.NullPublicKey +import core.crypto.SecureHash +import core.crypto.toStringShort import core.utilities.Emoji import java.security.PublicKey import java.time.Instant diff --git a/src/main/kotlin/contracts/CrowdFund.kt b/src/main/kotlin/contracts/CrowdFund.kt index c6e796b881..5294e049ae 100644 --- a/src/main/kotlin/contracts/CrowdFund.kt +++ b/src/main/kotlin/contracts/CrowdFund.kt @@ -9,6 +9,7 @@ package contracts import core.* +import core.crypto.SecureHash import java.security.PublicKey import java.time.Instant import java.util.* diff --git a/src/main/kotlin/contracts/DummyContract.kt b/src/main/kotlin/contracts/DummyContract.kt index dd3bc4528c..45c0705f47 100644 --- a/src/main/kotlin/contracts/DummyContract.kt +++ b/src/main/kotlin/contracts/DummyContract.kt @@ -10,7 +10,7 @@ package contracts import core.Contract import core.ContractState -import core.SecureHash +import core.crypto.SecureHash import core.TransactionForVerification // The dummy contract doesn't do anything useful. It exists for testing purposes. diff --git a/src/main/kotlin/contracts/protocols/TwoPartyTradeProtocol.kt b/src/main/kotlin/contracts/protocols/TwoPartyTradeProtocol.kt index 14ac83b13b..87da3f432c 100644 --- a/src/main/kotlin/contracts/protocols/TwoPartyTradeProtocol.kt +++ b/src/main/kotlin/contracts/protocols/TwoPartyTradeProtocol.kt @@ -13,6 +13,7 @@ import com.google.common.util.concurrent.ListenableFuture import contracts.Cash import contracts.sumCashBy import core.* +import core.crypto.signWithECDSA import core.messaging.LegallyIdentifiableNode import core.messaging.ProtocolStateMachine import core.messaging.SingleMessageRecipient diff --git a/src/main/kotlin/core/Services.kt b/src/main/kotlin/core/Services.kt index 8630bd4509..4f2d18160e 100644 --- a/src/main/kotlin/core/Services.kt +++ b/src/main/kotlin/core/Services.kt @@ -9,6 +9,7 @@ package core import co.paralleluniverse.fibers.Suspendable +import core.crypto.DigitalSignature import core.messaging.MessagingService import core.messaging.NetworkMap import core.serialization.SerializedBytes diff --git a/src/main/kotlin/core/Structures.kt b/src/main/kotlin/core/Structures.kt index 2d955e3801..2195f0564c 100644 --- a/src/main/kotlin/core/Structures.kt +++ b/src/main/kotlin/core/Structures.kt @@ -8,6 +8,8 @@ package core +import core.crypto.SecureHash +import core.crypto.toStringShort import core.serialization.OpaqueBytes import core.serialization.serialize import java.security.PublicKey diff --git a/src/main/kotlin/core/TransactionVerification.kt b/src/main/kotlin/core/TransactionVerification.kt index 4aa79a80e1..6f038205a8 100644 --- a/src/main/kotlin/core/TransactionVerification.kt +++ b/src/main/kotlin/core/TransactionVerification.kt @@ -8,6 +8,7 @@ package core +import core.crypto.SecureHash import java.util.* class TransactionResolutionException(val hash: SecureHash) : Exception() diff --git a/src/main/kotlin/core/Transactions.kt b/src/main/kotlin/core/Transactions.kt index a7ce3337bc..e9c443a3b4 100644 --- a/src/main/kotlin/core/Transactions.kt +++ b/src/main/kotlin/core/Transactions.kt @@ -9,6 +9,9 @@ package core import co.paralleluniverse.fibers.Suspendable +import core.crypto.DigitalSignature +import core.crypto.SecureHash +import core.crypto.signWithECDSA import core.node.TimestampingError import core.serialization.SerializedBytes import core.serialization.deserialize diff --git a/src/main/kotlin/core/messaging/InMemoryNetwork.kt b/src/main/kotlin/core/messaging/InMemoryNetwork.kt index 143d1a2d7e..bf7795b6d4 100644 --- a/src/main/kotlin/core/messaging/InMemoryNetwork.kt +++ b/src/main/kotlin/core/messaging/InMemoryNetwork.kt @@ -12,8 +12,8 @@ import com.google.common.util.concurrent.Futures import com.google.common.util.concurrent.ListenableFuture import com.google.common.util.concurrent.MoreExecutors import core.Party +import core.crypto.sha256 import core.node.TimestamperNodeService -import core.sha256 import core.utilities.loggerFor import java.security.KeyPairGenerator import java.time.Instant diff --git a/src/main/kotlin/core/messaging/StateMachines.kt b/src/main/kotlin/core/messaging/StateMachines.kt index 6576972788..853a3e5937 100644 --- a/src/main/kotlin/core/messaging/StateMachines.kt +++ b/src/main/kotlin/core/messaging/StateMachines.kt @@ -17,13 +17,13 @@ import com.esotericsoftware.kryo.io.Output import com.google.common.util.concurrent.ListenableFuture import com.google.common.util.concurrent.MoreExecutors import com.google.common.util.concurrent.SettableFuture -import core.SecureHash +import core.crypto.SecureHash import core.ServiceHub import core.serialization.THREAD_LOCAL_KRYO import core.serialization.createKryo import core.serialization.deserialize import core.serialization.serialize -import core.sha256 +import core.crypto.sha256 import core.utilities.trace import org.slf4j.Logger import org.slf4j.LoggerFactory diff --git a/src/main/kotlin/core/node/TimestamperNodeService.kt b/src/main/kotlin/core/node/TimestamperNodeService.kt index 535698727e..d0e37b1c1a 100644 --- a/src/main/kotlin/core/node/TimestamperNodeService.kt +++ b/src/main/kotlin/core/node/TimestamperNodeService.kt @@ -11,6 +11,8 @@ package core.node import co.paralleluniverse.common.util.VisibleForTesting import co.paralleluniverse.fibers.Suspendable import core.* +import core.crypto.DigitalSignature +import core.crypto.signWithECDSA import core.messaging.LegallyIdentifiableNode import core.messaging.MessageRecipients import core.messaging.MessagingService diff --git a/src/main/kotlin/core/node/TraderDemo.kt b/src/main/kotlin/core/node/TraderDemo.kt index 0620755af6..339dc034a4 100644 --- a/src/main/kotlin/core/node/TraderDemo.kt +++ b/src/main/kotlin/core/node/TraderDemo.kt @@ -12,6 +12,7 @@ import com.google.common.net.HostAndPort import contracts.CommercialPaper import contracts.protocols.TwoPartyTradeProtocol import core.* +import core.crypto.SecureHash import core.messaging.LegallyIdentifiableNode import core.messaging.SingleMessageRecipient import core.messaging.runOnNextMessage diff --git a/src/main/kotlin/core/serialization/Kryo.kt b/src/main/kotlin/core/serialization/Kryo.kt index e5acdd7abb..45c2f4f91e 100644 --- a/src/main/kotlin/core/serialization/Kryo.kt +++ b/src/main/kotlin/core/serialization/Kryo.kt @@ -16,9 +16,9 @@ import com.esotericsoftware.kryo.Serializer import com.esotericsoftware.kryo.io.Input import com.esotericsoftware.kryo.io.Output import com.esotericsoftware.kryo.serializers.JavaSerializer -import core.SecureHash +import core.crypto.SecureHash import core.SignedWireTransaction -import core.sha256 +import core.crypto.sha256 import de.javakaffee.kryoserializers.ArraysAsListSerializer import org.objenesis.strategy.StdInstantiatorStrategy import java.io.ByteArrayOutputStream diff --git a/src/test/kotlin/contracts/CashTests.kt b/src/test/kotlin/contracts/CashTests.kt index d049982b62..38c2159f3f 100644 --- a/src/test/kotlin/contracts/CashTests.kt +++ b/src/test/kotlin/contracts/CashTests.kt @@ -14,6 +14,7 @@ import contracts.Cash import contracts.DummyContract import contracts.InsufficientBalanceException import core.* +import core.crypto.SecureHash import core.serialization.OpaqueBytes import core.testutils.* import org.junit.Test diff --git a/src/test/kotlin/contracts/CommercialPaperTests.kt b/src/test/kotlin/contracts/CommercialPaperTests.kt index 28bf54d6c4..e8b212553d 100644 --- a/src/test/kotlin/contracts/CommercialPaperTests.kt +++ b/src/test/kotlin/contracts/CommercialPaperTests.kt @@ -9,6 +9,7 @@ package contracts import core.* +import core.crypto.SecureHash import core.node.TimestampingError import core.testutils.* import org.junit.Test diff --git a/src/test/kotlin/contracts/CrowdFundTests.kt b/src/test/kotlin/contracts/CrowdFundTests.kt index 0aed163b67..bf8a4528ef 100644 --- a/src/test/kotlin/contracts/CrowdFundTests.kt +++ b/src/test/kotlin/contracts/CrowdFundTests.kt @@ -9,6 +9,7 @@ package contracts import core.* +import core.crypto.SecureHash import core.testutils.* import org.junit.Test import java.time.Instant diff --git a/src/test/kotlin/core/MockServices.kt b/src/test/kotlin/core/MockServices.kt index ec31ad3b4d..6a61ab7178 100644 --- a/src/test/kotlin/core/MockServices.kt +++ b/src/test/kotlin/core/MockServices.kt @@ -8,6 +8,8 @@ package core +import core.crypto.DigitalSignature +import core.crypto.signWithECDSA import core.messaging.MessagingService import core.messaging.MockNetworkMap import core.messaging.NetworkMap diff --git a/src/test/kotlin/core/TransactionGroupTests.kt b/src/test/kotlin/core/TransactionGroupTests.kt index 9be61293e5..cfbfeb8502 100644 --- a/src/test/kotlin/core/TransactionGroupTests.kt +++ b/src/test/kotlin/core/TransactionGroupTests.kt @@ -9,6 +9,7 @@ package core import contracts.Cash +import core.crypto.SecureHash import core.testutils.* import org.junit.Test import kotlin.test.assertEquals diff --git a/src/test/kotlin/core/node/TimestamperNodeServiceTest.kt b/src/test/kotlin/core/node/TimestamperNodeServiceTest.kt index 3e6189c5d4..375e0db3d1 100644 --- a/src/test/kotlin/core/node/TimestamperNodeServiceTest.kt +++ b/src/test/kotlin/core/node/TimestamperNodeServiceTest.kt @@ -10,6 +10,7 @@ package core.node import co.paralleluniverse.fibers.Suspendable import core.* +import core.crypto.SecureHash import core.messaging.* import core.serialization.serialize import core.testutils.ALICE diff --git a/src/test/kotlin/core/serialization/TransactionSerializationTests.kt b/src/test/kotlin/core/serialization/TransactionSerializationTests.kt index 7944500cab..6b7fc24e6c 100644 --- a/src/test/kotlin/core/serialization/TransactionSerializationTests.kt +++ b/src/test/kotlin/core/serialization/TransactionSerializationTests.kt @@ -10,6 +10,7 @@ package core.serialization import contracts.Cash import core.* +import core.crypto.SecureHash import core.testutils.* import org.junit.Before import org.junit.Test diff --git a/src/test/kotlin/core/testutils/TestUtils.kt b/src/test/kotlin/core/testutils/TestUtils.kt index 37fc7b2d88..f9c97148a8 100644 --- a/src/test/kotlin/core/testutils/TestUtils.kt +++ b/src/test/kotlin/core/testutils/TestUtils.kt @@ -12,6 +12,9 @@ package core.testutils import contracts.* import core.* +import core.crypto.DummyPublicKey +import core.crypto.NullPublicKey +import core.crypto.SecureHash import core.visualiser.GraphVisualiser import java.security.KeyPairGenerator import java.security.PublicKey diff --git a/src/test/kotlin/core/visualiser/GroupToGraphConversion.kt b/src/test/kotlin/core/visualiser/GroupToGraphConversion.kt index 7a3149499d..03f81f8ce2 100644 --- a/src/test/kotlin/core/visualiser/GroupToGraphConversion.kt +++ b/src/test/kotlin/core/visualiser/GroupToGraphConversion.kt @@ -10,7 +10,7 @@ package core.visualiser import core.CommandData import core.ContractState -import core.SecureHash +import core.crypto.SecureHash import core.testutils.TransactionGroupDSL import org.graphstream.graph.Edge import org.graphstream.graph.Node