|
| 1 | +package io.onixlabs.corda.core.contract |
| 2 | + |
| 3 | +import net.corda.core.crypto.Crypto |
| 4 | +import net.corda.core.crypto.DigitalSignature |
| 5 | +import net.corda.core.crypto.sign |
| 6 | +import net.corda.core.node.ServiceHub |
| 7 | +import net.corda.core.serialization.CordaSerializable |
| 8 | +import net.corda.core.utilities.toBase64 |
| 9 | +import java.security.PrivateKey |
| 10 | +import java.security.PublicKey |
| 11 | +import java.util.* |
| 12 | + |
| 13 | +/** |
| 14 | + * Represents an array of unsigned bytes, and its signed equivalent. |
| 15 | + * |
| 16 | + * @property content The unsigned signature content. |
| 17 | + * @property signature The digital signature representing the signed content. |
| 18 | + */ |
| 19 | +@CordaSerializable |
| 20 | +data class SignatureData(private val content: ByteArray, private val signature: DigitalSignature) { |
| 21 | + |
| 22 | + companion object { |
| 23 | + fun create(content: ByteArray, privateKey: PrivateKey): SignatureData { |
| 24 | + val signature = privateKey.sign(content) |
| 25 | + return SignatureData(content, signature) |
| 26 | + } |
| 27 | + |
| 28 | + fun create(content: ByteArray, publicKey: PublicKey, serviceHub: ServiceHub): SignatureData { |
| 29 | + val signature = serviceHub.keyManagementService.sign(content, publicKey) |
| 30 | + return SignatureData(content, signature.withoutKey()) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Verifies the signature data using the specified public key. |
| 36 | + * |
| 37 | + * @param publicKey The public key to verify against the signature. |
| 38 | + * @return Returns true if the public key was used to sign the data; otherwise, false. |
| 39 | + */ |
| 40 | + fun verify(publicKey: PublicKey): Boolean { |
| 41 | + return Crypto.isValid(publicKey, signature.bytes, content) |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Determines whether the specified object is equal to the current object. |
| 46 | + * |
| 47 | + * @param other The object to compare with the current object. |
| 48 | + * @return Returns true if the specified object is equal to the current object; otherwise, false. |
| 49 | + */ |
| 50 | + override fun equals(other: Any?): Boolean { |
| 51 | + return this === other || (other is SignatureData |
| 52 | + && content.contentEquals(other.content) |
| 53 | + && signature == other.signature) |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Serves as the default hash function. |
| 58 | + * |
| 59 | + * @return Returns a hash code for the current object. |
| 60 | + */ |
| 61 | + override fun hashCode(): Int { |
| 62 | + return Objects.hash(signature, content.contentHashCode()) |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Returns a string that represents the current object. |
| 67 | + * |
| 68 | + * @return Returns a string that represents the current object. |
| 69 | + */ |
| 70 | + override fun toString(): String = buildString { |
| 71 | + appendln("Unsigned bytes: ${content.toBase64()}") |
| 72 | + appendln("Signed bytes: ${signature.bytes.toBase64()}") |
| 73 | + } |
| 74 | +} |
0 commit comments