corda / net.corda.core.utilities / OpaqueBytes

OpaqueBytes

open class OpaqueBytes : ByteSequence

A simple class that wraps a byte array and makes the equals/hashCode/toString methods work as you actually expect. In an ideal JVM this would be a value type and be completely overhead free. Project Valhalla is adding such functionality to Java, but it won't arrive for a few years yet!

Constructors

<init>

OpaqueBytes(bytes: ByteArray)

A simple class that wraps a byte array and makes the equals/hashCode/toString methods work as you actually expect. In an ideal JVM this would be a value type and be completely overhead free. Project Valhalla is adding such functionality to Java, but it won't arrive for a few years yet!

Properties

bytes

val bytes: ByteArray

The bytes are always cloned so that this object becomes immutable. This has been done to prevent tampering with entities such as net.corda.core.crypto.SecureHash and net.corda.core.contracts.PrivacySalt, as well as preserve the integrity of our hash constants net.corda.core.crypto.SecureHash.zeroHash and net.corda.core.crypto.SecureHash.allOnesHash.

Inherited Properties

offset

val offset: Int

The start position of the sequence within the byte array.

size

val size: Int

The number of bytes this sequence represents.

Inherited Functions

compareTo

open fun compareTo(other: ByteSequence): Int

Compare byte arrays byte by byte. Arrays that are shorter are deemed less than longer arrays if all the bytes of the shorter array equal those in the same position of the longer array.

copy

fun copy(): ByteSequence

Copy this sequence, complete with new backing array. This can be helpful to break references to potentially large backing arrays from small sub-sequences.

copyBytes

fun copyBytes(): ByteArray

Same as copy but returns just the new byte array.

equals

open fun equals(other: Any?): Boolean

hashCode

open fun hashCode(): Int

open

fun open(): ByteArrayInputStream

Returns a ByteArrayInputStream of the bytes.

putTo

fun putTo(buffer: ByteBuffer): ByteBuffer

Write this sequence to a ByteBuffer.

slice

fun slice(start: Int = 0, end: Int = size): ByteBuffer

A new read-only ByteBuffer view of this sequence or part of it. If start or end are negative then IllegalArgumentException is thrown, otherwise they are clamped if necessary. This method cannot be used to get bytes before offset or after offset+size, and never makes a new array.

subSequence

fun subSequence(offset: Int, size: Int): ByteSequence

Create a sub-sequence of this sequence. A copy of the underlying array may be made, if a subclass overrides bytes to do so, as OpaqueBytes does.

take

fun take(n: Int): ByteSequence

Take the first n bytes of this sequence as a sub-sequence. See subSequence for further semantics.

toString

open fun toString(): String

writeTo

fun writeTo(output: OutputStream): Unit

Write this sequence to an OutputStream.

Companion Object Functions

of

fun of(vararg b: Byte): OpaqueBytes

Create OpaqueBytes from a sequence of Byte values.

Extension Properties

isZero

val OpaqueBytes.isZero: Boolean

Extension Functions

contextLogger

fun Any.contextLogger(): <ERROR CLASS>

When called from a companion object, returns the logger for the enclosing class.

deserialize

fun <T : Any> ByteSequence.deserialize(serializationFactory: SerializationFactory = SerializationFactory.defaultFactory, context: SerializationContext = serializationFactory.defaultContext): T

Convenience extension method for deserializing a ByteSequence, utilising the defaults.

deserializeWithCompatibleContext

fun <T : Any> ByteSequence.deserializeWithCompatibleContext(serializationFactory: SerializationFactory = SerializationFactory.defaultFactory, context: SerializationContext = serializationFactory.defaultContext): ObjectWithCompatibleContext<T>

Additionally returns SerializationContext which was used for encoding. It might be helpful to know SerializationContext to use the same encoding in the reply.

hashAs

fun OpaqueBytes.hashAs(algorithm: String): SecureHash

Compute the algorithm hash for the contents of the OpaqueBytes.

sha256

fun OpaqueBytes.sha256(): SHA256

Compute the SHA-256 hash for the contents of the OpaqueBytes.

Inheritors

DigitalSignature

open class DigitalSignature : OpaqueBytes

A wrapper around a digital signature.

PrivacySalt

class PrivacySalt : OpaqueBytes

A privacy salt is required to compute nonces per transaction component in order to ensure that an adversary cannot use brute force techniques and reveal the content of a Merkle-leaf hashed value. Because this salt serves the role of the seed to compute nonces, its size and entropy should be equal to the underlying hash function used for Merkle tree generation, currently SecureHash.SHA256, which has an output of 32 bytes. There are two constructors, one that generates a new 32-bytes random salt, and another that takes a ByteArray input. The latter is required in cases where the salt value needs to be pre-generated (agreed between transacting parties), but it is highlighted that one should always ensure it has sufficient entropy.

SecureHash

sealed class SecureHash : OpaqueBytes

Container for a cryptographically secure hash value. Provides utilities for generating a cryptographic hash using different algorithms (currently only SHA-256 supported).

SerializedBytes

class SerializedBytes<T : Any> : OpaqueBytes

A type safe wrapper around a byte array that contains a serialised object. You can call SerializedBytes.deserialize to get the original object back.