All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.interledger.crypto.ImmutableEncryptedSecret Maven / Gradle / Ivy

Go to download

Encryption, decryption, signing and verification services using various underlying key-stores.

The newest version!
package org.interledger.crypto;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;

/**
 * Immutable implementation of {@link EncryptedSecret.AbstractEncryptedSecret}.
 * 

* Use the builder to create immutable instances: * {@code ImmutableEncryptedSecret.builder()}. */ @Generated(from = "EncryptedSecret.AbstractEncryptedSecret", generator = "Immutables") @SuppressWarnings({"all"}) @ParametersAreNonnullByDefault @javax.annotation.Generated("org.immutables.processor.ProxyProcessor") @Immutable @CheckReturnValue public final class ImmutableEncryptedSecret extends EncryptedSecret.AbstractEncryptedSecret { private final KeyMetadata keyMetadata; private final EncryptionAlgorithm encryptionAlgorithm; private final byte[] cipherMessage; private transient final String encodedValue; private ImmutableEncryptedSecret( KeyMetadata keyMetadata, EncryptionAlgorithm encryptionAlgorithm, byte[] cipherMessage) { this.keyMetadata = keyMetadata; this.encryptionAlgorithm = encryptionAlgorithm; this.cipherMessage = cipherMessage; this.encodedValue = Objects.requireNonNull(super.encodedValue(), "encodedValue"); } /** * Metadata about the key used to encrypt this secret. * @return A {@link KeyMetadata}. */ @Override public KeyMetadata keyMetadata() { return keyMetadata; } /** * The algorithm used to encrypt this secret. * @return A {@link EncryptionAlgorithm}. */ @Override public EncryptionAlgorithm encryptionAlgorithm() { return encryptionAlgorithm; } /** * The actual encrypted cipher-message as a byte-array. This byte array is a "message" as opposed to the actual * cipherText because it likely has padding to support AES/GCM mode. * @return A {@link String} that conforms to the format defined in {@link EncryptedSecret#encodedValue()}. */ @Override public byte[] cipherMessage() { return cipherMessage.clone(); } /** * @return The computed-at-construction value of the {@code encodedValue} attribute */ @Override public String encodedValue() { return encodedValue; } /** * Copy the current immutable object by setting a value for the {@link EncryptedSecret.AbstractEncryptedSecret#keyMetadata() keyMetadata} attribute. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for keyMetadata * @return A modified copy of the {@code this} object */ public final ImmutableEncryptedSecret withKeyMetadata(KeyMetadata value) { if (this.keyMetadata == value) return this; KeyMetadata newValue = Objects.requireNonNull(value, "keyMetadata"); return new ImmutableEncryptedSecret(newValue, this.encryptionAlgorithm, this.cipherMessage); } /** * Copy the current immutable object by setting a value for the {@link EncryptedSecret.AbstractEncryptedSecret#encryptionAlgorithm() encryptionAlgorithm} attribute. * A value equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for encryptionAlgorithm * @return A modified copy of the {@code this} object */ public final ImmutableEncryptedSecret withEncryptionAlgorithm(EncryptionAlgorithm value) { if (this.encryptionAlgorithm == value) return this; EncryptionAlgorithm newValue = Objects.requireNonNull(value, "encryptionAlgorithm"); if (this.encryptionAlgorithm.equals(newValue)) return this; return new ImmutableEncryptedSecret(this.keyMetadata, newValue, this.cipherMessage); } /** * Copy the current immutable object with elements that replace the content of {@link EncryptedSecret.AbstractEncryptedSecret#cipherMessage() cipherMessage}. * The array is cloned before being saved as attribute values. * @param elements The non-null elements for cipherMessage * @return A modified copy of {@code this} object */ public final ImmutableEncryptedSecret withCipherMessage(byte... elements) { byte[] newValue = elements.clone(); return new ImmutableEncryptedSecret(this.keyMetadata, this.encryptionAlgorithm, newValue); } /** * This instance is equal to all instances of {@code ImmutableEncryptedSecret} that have equal attribute values. * @return {@code true} if {@code this} is equal to {@code another} instance */ @Override public boolean equals(@Nullable Object another) { if (this == another) return true; return another instanceof ImmutableEncryptedSecret && equalTo((ImmutableEncryptedSecret) another); } private boolean equalTo(ImmutableEncryptedSecret another) { return keyMetadata.equals(another.keyMetadata) && encryptionAlgorithm.equals(another.encryptionAlgorithm) && Arrays.equals(cipherMessage, another.cipherMessage) && encodedValue.equals(another.encodedValue); } /** * Computes a hash code from attributes: {@code keyMetadata}, {@code encryptionAlgorithm}, {@code cipherMessage}, {@code encodedValue}. * @return hashCode value */ @Override public int hashCode() { @Var int h = 5381; h += (h << 5) + keyMetadata.hashCode(); h += (h << 5) + encryptionAlgorithm.hashCode(); h += (h << 5) + Arrays.hashCode(cipherMessage); h += (h << 5) + encodedValue.hashCode(); return h; } /** * Creates an immutable copy of a {@link EncryptedSecret.AbstractEncryptedSecret} value. * Uses accessors to get values to initialize the new immutable instance. * If an instance is already immutable, it is returned as is. * @param instance The instance to copy * @return A copied immutable EncryptedSecret instance */ public static ImmutableEncryptedSecret copyOf(EncryptedSecret.AbstractEncryptedSecret instance) { if (instance instanceof ImmutableEncryptedSecret) { return (ImmutableEncryptedSecret) instance; } return ImmutableEncryptedSecret.builder() .from(instance) .build(); } /** * Creates a builder for {@link ImmutableEncryptedSecret ImmutableEncryptedSecret}. *

   * ImmutableEncryptedSecret.builder()
   *    .keyMetadata(org.interledger.crypto.KeyMetadata) // required {@link EncryptedSecret.AbstractEncryptedSecret#keyMetadata() keyMetadata}
   *    .encryptionAlgorithm(org.interledger.crypto.EncryptionAlgorithm) // required {@link EncryptedSecret.AbstractEncryptedSecret#encryptionAlgorithm() encryptionAlgorithm}
   *    .cipherMessage(byte) // required {@link EncryptedSecret.AbstractEncryptedSecret#cipherMessage() cipherMessage}
   *    .build();
   * 
* @return A new ImmutableEncryptedSecret builder */ public static ImmutableEncryptedSecret.Builder builder() { return new ImmutableEncryptedSecret.Builder(); } /** * Builds instances of type {@link ImmutableEncryptedSecret ImmutableEncryptedSecret}. * Initialize attributes and then invoke the {@link #build()} method to create an * immutable instance. *

{@code Builder} is not thread-safe and generally should not be stored in a field or collection, * but instead used immediately to create instances. */ @Generated(from = "EncryptedSecret.AbstractEncryptedSecret", generator = "Immutables") @NotThreadSafe public static final class Builder { private static final long INIT_BIT_KEY_METADATA = 0x1L; private static final long INIT_BIT_ENCRYPTION_ALGORITHM = 0x2L; private static final long INIT_BIT_CIPHER_MESSAGE = 0x4L; private long initBits = 0x7L; private @Nullable KeyMetadata keyMetadata; private @Nullable EncryptionAlgorithm encryptionAlgorithm; private @Nullable byte[] cipherMessage; private Builder() { } /** * Fill a builder with attribute values from the provided {@code org.interledger.crypto.EncryptedSecret} instance. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder from(EncryptedSecret instance) { Objects.requireNonNull(instance, "instance"); from((Object) instance); return this; } /** * Fill a builder with attribute values from the provided {@code org.interledger.crypto.EncryptedSecret.AbstractEncryptedSecret} instance. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder from(EncryptedSecret.AbstractEncryptedSecret instance) { Objects.requireNonNull(instance, "instance"); from((Object) instance); return this; } private void from(Object object) { if (object instanceof EncryptedSecret) { EncryptedSecret instance = (EncryptedSecret) object; cipherMessage(instance.cipherMessage()); encryptionAlgorithm(instance.encryptionAlgorithm()); keyMetadata(instance.keyMetadata()); } } /** * Initializes the value for the {@link EncryptedSecret.AbstractEncryptedSecret#keyMetadata() keyMetadata} attribute. * @param keyMetadata The value for keyMetadata * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder keyMetadata(KeyMetadata keyMetadata) { this.keyMetadata = Objects.requireNonNull(keyMetadata, "keyMetadata"); initBits &= ~INIT_BIT_KEY_METADATA; return this; } /** * Initializes the value for the {@link EncryptedSecret.AbstractEncryptedSecret#encryptionAlgorithm() encryptionAlgorithm} attribute. * @param encryptionAlgorithm The value for encryptionAlgorithm * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder encryptionAlgorithm(EncryptionAlgorithm encryptionAlgorithm) { this.encryptionAlgorithm = Objects.requireNonNull(encryptionAlgorithm, "encryptionAlgorithm"); initBits &= ~INIT_BIT_ENCRYPTION_ALGORITHM; return this; } /** * Initializes the value for the {@link EncryptedSecret.AbstractEncryptedSecret#cipherMessage() cipherMessage} attribute. * @param cipherMessage The elements for cipherMessage * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder cipherMessage(byte... cipherMessage) { this.cipherMessage = cipherMessage.clone(); initBits &= ~INIT_BIT_CIPHER_MESSAGE; return this; } /** * Builds a new {@link ImmutableEncryptedSecret ImmutableEncryptedSecret}. * @return An immutable instance of EncryptedSecret * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableEncryptedSecret build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ImmutableEncryptedSecret(keyMetadata, encryptionAlgorithm, cipherMessage); } private String formatRequiredAttributesMessage() { List attributes = new ArrayList<>(); if ((initBits & INIT_BIT_KEY_METADATA) != 0) attributes.add("keyMetadata"); if ((initBits & INIT_BIT_ENCRYPTION_ALGORITHM) != 0) attributes.add("encryptionAlgorithm"); if ((initBits & INIT_BIT_CIPHER_MESSAGE) != 0) attributes.add("cipherMessage"); return "Cannot build EncryptedSecret, some of required attributes are not set " + attributes; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy