001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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) {
058            BaseKey that = (BaseKey)other;
059            
060            byte[] thatEncoded = that.getKey().getEncoded();
061            byte[] thatIv = that.getIv();
062            
063            boolean objectsEqual = Arrays.equals(key.getEncoded(), thatEncoded);
064            if(objectsEqual) {
065                objectsEqual = Arrays.equals(iv, thatIv);
066            }
067            
068            return objectsEqual;
069        } else {
070            return false;
071        }
072    }
073
074    @Override
075    public int hashCode() {
076        int hash = 7;
077        
078        hash = 53 * hash + Objects.hashCode(this.key);
079        hash = 53 * hash + Arrays.hashCode(this.iv);
080        
081        return hash;
082    }
083    
084}