From 7f42a9b48c17792ce84aac09275ca52208b042bf Mon Sep 17 00:00:00 2001 From: Chris Rankin Date: Thu, 12 Jul 2018 14:52:44 +0100 Subject: [PATCH] ENT-1565: Remove Guava code from core. (#3569) Remove Guava code from core. Note that forcing this project to use Guava `$guava_version` throughout breaks SimmValuationTest. --- core-deterministic/build.gradle | 3 --- .../net/corda/core/identity/CordaX500Name.kt | 3 +-- .../net/corda/core/internal/InternalUtils.kt | 16 +++++++++++----- .../net/corda/core/internal/InternalUtilsTest.kt | 15 +++++++++++++++ serialization-deterministic/build.gradle | 2 +- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/core-deterministic/build.gradle b/core-deterministic/build.gradle index b3f4718c50..508ecda18b 100644 --- a/core-deterministic/build.gradle +++ b/core-deterministic/build.gradle @@ -17,8 +17,6 @@ configurations { dependencies { compileOnly project(':core') - compileOnly "com.google.guava:guava:$guava_version" - compileOnly "$quasar_group:quasar-core:$quasar_version:jdk8" // Configure these by hand. It should be a minimal subset of core's dependencies, // and without any obviously non-deterministic ones such as Hibernate. @@ -28,7 +26,6 @@ dependencies { runtimeLibraries "org.bouncycastle:bcprov-jdk15on:$bouncycastle_version" runtimeLibraries "org.bouncycastle:bcpkix-jdk15on:$bouncycastle_version" runtimeLibraries "com.google.code.findbugs:jsr305:$jsr305_version" - runtimeLibraries "com.google.guava:guava:$guava_version" runtimeLibraries "net.i2p.crypto:eddsa:$eddsa_version" runtimeLibraries "org.slf4j:slf4j-api:$slf4j_version" } diff --git a/core/src/main/kotlin/net/corda/core/identity/CordaX500Name.kt b/core/src/main/kotlin/net/corda/core/identity/CordaX500Name.kt index 24a3f97b7c..213e5670aa 100644 --- a/core/src/main/kotlin/net/corda/core/identity/CordaX500Name.kt +++ b/core/src/main/kotlin/net/corda/core/identity/CordaX500Name.kt @@ -1,6 +1,5 @@ package net.corda.core.identity -import com.google.common.collect.ImmutableSet import net.corda.core.KeepForDJVM import net.corda.core.internal.LegalNameValidator import net.corda.core.internal.toAttributesMap @@ -79,7 +78,7 @@ data class CordaX500Name(val commonName: String?, const val MAX_LENGTH_COMMON_NAME = 64 private val supportedAttributes = setOf(BCStyle.O, BCStyle.C, BCStyle.L, BCStyle.CN, BCStyle.ST, BCStyle.OU) - private val countryCodes: Set = ImmutableSet.copyOf(Locale.getISOCountries() + unspecifiedCountry) + private val countryCodes: Set = setOf(*Locale.getISOCountries(), unspecifiedCountry) @JvmStatic fun build(principal: X500Principal): CordaX500Name { diff --git a/core/src/main/kotlin/net/corda/core/internal/InternalUtils.kt b/core/src/main/kotlin/net/corda/core/internal/InternalUtils.kt index 778b4dfc11..6a53f25755 100644 --- a/core/src/main/kotlin/net/corda/core/internal/InternalUtils.kt +++ b/core/src/main/kotlin/net/corda/core/internal/InternalUtils.kt @@ -2,8 +2,6 @@ @file:KeepForDJVM package net.corda.core.internal -import com.google.common.hash.Hashing -import com.google.common.hash.HashingInputStream import net.corda.core.DeleteForDJVM import net.corda.core.KeepForDJVM import net.corda.core.cordapp.Cordapp @@ -43,6 +41,7 @@ import java.nio.file.Files import java.nio.file.Path import java.nio.file.Paths import java.security.KeyPair +import java.security.MessageDigest import java.security.PrivateKey import java.security.PublicKey import java.security.cert.* @@ -132,9 +131,16 @@ fun InputStream.readFully(): ByteArray = use { it.readBytes() } /** Calculate the hash of the remaining bytes in this input stream. The stream is closed at the end. */ fun InputStream.hash(): SecureHash { return use { - val his = HashingInputStream(Hashing.sha256(), it) - his.copyTo(NullOutputStream) // To avoid reading in the entire stream into memory just write out the bytes to /dev/null - SecureHash.SHA256(his.hash().asBytes()) + val md = MessageDigest.getInstance("SHA-256") + val buffer = ByteArray(DEFAULT_BUFFER_SIZE) + while (true) { + val count = it.read(buffer) + if (count == -1) { + break + } + md.update(buffer, 0, count) + } + SecureHash.SHA256(md.digest()) } } diff --git a/core/src/test/kotlin/net/corda/core/internal/InternalUtilsTest.kt b/core/src/test/kotlin/net/corda/core/internal/InternalUtilsTest.kt index 21f89c34af..8ad6af291e 100644 --- a/core/src/test/kotlin/net/corda/core/internal/InternalUtilsTest.kt +++ b/core/src/test/kotlin/net/corda/core/internal/InternalUtilsTest.kt @@ -1,9 +1,11 @@ package net.corda.core.internal import net.corda.core.contracts.TimeWindow +import net.corda.core.crypto.SecureHash import org.assertj.core.api.Assertions.assertThat import org.junit.Assert.assertArrayEquals import org.junit.Test +import java.util.* import java.util.stream.IntStream import java.util.stream.Stream import kotlin.test.assertEquals @@ -99,6 +101,19 @@ open class InternalUtilsTest { assertThat(PrivateClass::class.java.kotlinObjectInstance).isNull() } + @Test + fun `test SHA-256 hash for InputStream`() { + val contents = arrayOfJunk(DEFAULT_BUFFER_SIZE * 2 + DEFAULT_BUFFER_SIZE / 2) + assertThat(contents.inputStream().hash()) + .isEqualTo(SecureHash.parse("A4759E7AA20338328866A2EA17EAF8C7FE4EC6BBE3BB71CEE7DF7C0461B3C22F")) + } + + private fun arrayOfJunk(size: Int) = ByteArray(size).apply { + for (i in 0 until size) { + this[i] = (i and 0xFF).toByte() + } + } + object PublicObject private object PrivateObject protected object ProtectedObject diff --git a/serialization-deterministic/build.gradle b/serialization-deterministic/build.gradle index c0c8ccb600..c19e128a46 100644 --- a/serialization-deterministic/build.gradle +++ b/serialization-deterministic/build.gradle @@ -17,13 +17,13 @@ configurations { dependencies { compileOnly project(':serialization') - compileOnly "$quasar_group:quasar-core:$quasar_version:jdk8" // Configure these by hand. It should be a minimal subset of dependencies, // and without any obviously non-deterministic ones such as Hibernate. runtimeLibraries project(path: ':core-deterministic', configuration: 'runtimeArtifacts') runtimeLibraries "org.apache.qpid:proton-j:$protonj_version" runtimeLibraries "org.iq80.snappy:snappy:$snappy_version" + runtimeLibraries "com.google.guava:guava:$guava_version" } jar {