Move Crypto.kt into core/crypto/CryptoUtilities.kt

This commit is contained in:
Mike Hearn 2016-02-05 16:54:45 +01:00
parent b023e570fc
commit 262124385d
26 changed files with 47 additions and 19 deletions

View File

@ -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) {

View File

@ -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;

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -9,6 +9,7 @@
package contracts
import core.*
import core.crypto.SecureHash
import java.security.PublicKey
import java.time.Instant
import java.util.*

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -8,6 +8,7 @@
package core
import core.crypto.SecureHash
import java.util.*
class TransactionResolutionException(val hash: SecureHash) : Exception()

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -9,6 +9,7 @@
package contracts
import core.*
import core.crypto.SecureHash
import core.node.TimestampingError
import core.testutils.*
import org.junit.Test

View File

@ -9,6 +9,7 @@
package contracts
import core.*
import core.crypto.SecureHash
import core.testutils.*
import org.junit.Test
import java.time.Instant

View File

@ -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

View File

@ -9,6 +9,7 @@
package core
import contracts.Cash
import core.crypto.SecureHash
import core.testutils.*
import org.junit.Test
import kotlin.test.assertEquals

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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