001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.release.plugin.slsa.v1_2;
018
019import java.util.Arrays;
020import java.util.Objects;
021
022import com.fasterxml.jackson.annotation.JsonInclude;
023import com.fasterxml.jackson.annotation.JsonProperty;
024
025/**
026 * A single cryptographic signature within a DSSE envelope.
027 *
028 * @see <a href="https://github.com/secure-systems-lab/dsse/blob/v1.0.2/envelope.md">DSSE Envelope specification</a>
029 * @since 1.10.0
030 */
031@JsonInclude(JsonInclude.Include.NON_NULL)
032public class Signature {
033
034    /**
035     * Hint for which key was used to sign; unset is treated as empty.
036     *
037     * <p>
038     * Consumers MUST NOT require this field to be set, and MUST NOT use it for security decisions.
039     * </p>
040     */
041    @JsonProperty("keyid")
042    private String keyid;
043
044    /** Raw signature bytes of the PAE-encoded payload. */
045    @JsonProperty("sig")
046    private byte[] sig;
047
048    /** Creates a new Signature instance. */
049    public Signature() {
050    }
051
052    @Override
053    public boolean equals(final Object o) {
054        if (!(o instanceof Signature)) {
055            return false;
056        }
057        final Signature signature = (Signature) o;
058        return Objects.equals(keyid, signature.keyid) && Arrays.equals(sig, signature.sig);
059    }
060
061    /**
062     * Gets the key identifier hint, or {@code null} if not set.
063     *
064     * @return The key identifier, or {@code null}
065     */
066    public String getKeyid() {
067        return keyid;
068    }
069
070    /**
071     * Gets the raw signature bytes.
072     *
073     * @return The signature bytes, or {@code null} if not set.
074     */
075    public byte[] getSig() {
076        return sig;
077    }
078
079    @Override
080    public int hashCode() {
081        return Objects.hash(keyid, Arrays.hashCode(sig));
082    }
083
084    /**
085     * Sets the key identifier hint.
086     *
087     * @param keyid The key identifier, or {@code null} to leave unset.
088     * @return this for chaining.
089     */
090    public Signature setKeyid(final String keyid) {
091        this.keyid = keyid;
092        return this;
093    }
094
095    /**
096     * Sets the raw signature bytes.
097     *
098     * @param sig The signature bytes.
099     * @return this for chaining.
100     */
101    public Signature setSig(final byte[] sig) {
102        this.sig = sig;
103        return this;
104    }
105
106    @Override
107    public String toString() {
108        return "Signature{keyid='" + keyid + "', sig=<" + (sig != null ? sig.length : 0) + " bytes>}";
109    }
110}