|
| 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 | + |
| 24 | + /** |
| 25 | + * Creates a signature from the specified content and private key. |
| 26 | + * |
| 27 | + * @param content The content to sign. |
| 28 | + * @param privateKey The private key to sign the content. |
| 29 | + * @return Returns a new signature containing the content and signed data. |
| 30 | + */ |
| 31 | + fun create(content: ByteArray, privateKey: PrivateKey): SignatureData { |
| 32 | + val signature = privateKey.sign(content) |
| 33 | + return SignatureData(content, signature) |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates a signature from the specified content by resolving the signing key from the service hub. |
| 38 | + * |
| 39 | + * @param content The content to sign. |
| 40 | + * @param publicKey The public key to resolve from the service hub. |
| 41 | + * @param serviceHub The service hub to resolve the public key. |
| 42 | + * @return Returns a new signature containing the content and signed data. |
| 43 | + */ |
| 44 | + fun create(content: ByteArray, publicKey: PublicKey, serviceHub: ServiceHub): SignatureData { |
| 45 | + val signature = serviceHub.keyManagementService.sign(content, publicKey) |
| 46 | + return SignatureData(content, signature.withoutKey()) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Verifies the signature data using the specified public key. |
| 52 | + * |
| 53 | + * @param publicKey The public key to verify against the signature. |
| 54 | + * @return Returns true if the public key was used to sign the data; otherwise, false. |
| 55 | + */ |
| 56 | + fun verify(publicKey: PublicKey): Boolean { |
| 57 | + return Crypto.isValid(publicKey, signature.bytes, content) |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Determines whether the specified object is equal to the current object. |
| 62 | + * |
| 63 | + * @param other The object to compare with the current object. |
| 64 | + * @return Returns true if the specified object is equal to the current object; otherwise, false. |
| 65 | + */ |
| 66 | + override fun equals(other: Any?): Boolean { |
| 67 | + return this === other || (other is SignatureData |
| 68 | + && content.contentEquals(other.content) |
| 69 | + && signature == other.signature) |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Serves as the default hash function. |
| 74 | + * |
| 75 | + * @return Returns a hash code for the current object. |
| 76 | + */ |
| 77 | + override fun hashCode(): Int { |
| 78 | + return Objects.hash(signature, content.contentHashCode()) |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Returns a string that represents the current object. |
| 83 | + * |
| 84 | + * @return Returns a string that represents the current object. |
| 85 | + */ |
| 86 | + override fun toString(): String = buildString { |
| 87 | + appendln("Unsigned bytes: ${content.toBase64()}") |
| 88 | + appendln("Signed bytes: ${signature.bytes.toBase64()}") |
| 89 | + } |
| 90 | +} |
0 commit comments