001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not use this file except in compliance with the License. 006// You may obtain a copy of the License at 007// 008// http://www.apache.org/licenses/LICENSE-2.0 009// 010// Unless required by applicable law or agreed to in writing, software 011// distributed under the License is distributed on an "AS IS" BASIS, 012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.util.common.persistence; 018 019import java.io.Serializable; 020import java.util.Arrays; 021import java.util.Objects; 022import javax.crypto.SecretKey; 023 024public class BaseKey 025 implements Serializable { 026 027 private SecretKey key; 028 private byte[] iv; 029 030 /** Creates a new instance of BaseKey */ 031 public BaseKey(SecretKey key, byte[] iv) { 032 this.setKey(key); 033 this.setIv(iv); 034 } 035 036 public SecretKey getKey() { 037 return key; 038 } 039 040 public final void setKey(SecretKey key) { 041 this.key = key; 042 } 043 044 public byte[] getIv() { 045 return iv; 046 } 047 048 public final void setIv(byte[] iv) { 049 this.iv = iv; 050 } 051 052 @Override 053 public boolean equals(Object other) { 054 if(this == other) 055 return true; 056 057 if(other instanceof BaseKey that) { 058 var thatEncoded = that.getKey().getEncoded(); 059 var thatIv = that.getIv(); 060 061 var objectsEqual = Arrays.equals(key.getEncoded(), thatEncoded); 062 if(objectsEqual) { 063 objectsEqual = Arrays.equals(iv, thatIv); 064 } 065 066 return objectsEqual; 067 } else { 068 return false; 069 } 070 } 071 072 @Override 073 public int hashCode() { 074 var hash = 7; 075 076 hash = 53 * hash + Objects.hashCode(this.key); 077 hash = 53 * hash + Arrays.hashCode(this.iv); 078 079 return hash; 080 } 081 082}