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 * http://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 */ 017 018package org.apache.commons.vfs2.provider.sftp; 019 020import java.util.Arrays; 021import java.util.Objects; 022 023import com.jcraft.jsch.JSch; 024import com.jcraft.jsch.JSchException; 025 026/** 027 * Structure for an identity based on byte arrays. 028 * 029 * @since 2.4 030 */ 031public class BytesIdentityInfo implements IdentityProvider { 032 033 private final byte[] passphrase; 034 035 private final byte[] privateKey; 036 037 private final byte[] publicKey; 038 039 /** 040 * Constructs an identity info with private and passphrase for the private key. 041 * 042 * @param privateKey Private key bytes 043 * @param passphrase The passphrase to decrypt the private key (can be {@code null} if no passphrase is used) 044 */ 045 public BytesIdentityInfo(final byte[] privateKey, final byte[] passphrase) { 046 this(privateKey, null, passphrase); 047 } 048 049 /** 050 * Constructs an identity info with private and public key and passphrase for the private key. 051 * 052 * @param privateKey Private key bytes 053 * @param publicKey The public key part used for connections with exchange of certificates (can be {@code null}) 054 * @param passphrase The passphrase to decrypt the private key (can be {@code null} if no passphrase is used) 055 */ 056 public BytesIdentityInfo(final byte[] privateKey, final byte[] publicKey, final byte[] passphrase) { 057 this.privateKey = Utils.clone(privateKey); 058 this.publicKey = Utils.clone(publicKey); 059 this.passphrase = Utils.clone(passphrase); 060 } 061 062 @Override 063 public void addIdentity(final JSch jsch) throws JSchException { 064 jsch.addIdentity("PrivateKey", privateKey, publicKey, passphrase); 065 } 066 067 @Override 068 public boolean equals(final Object obj) { 069 if (this == obj) { 070 return true; 071 } 072 if (!(obj instanceof BytesIdentityInfo)) { 073 return false; 074 } 075 final BytesIdentityInfo other = (BytesIdentityInfo) obj; 076 return Arrays.equals(passphrase, other.passphrase) && Arrays.equals(privateKey, other.privateKey) && Arrays.equals(publicKey, other.publicKey); 077 } 078 079 /** 080 * Gets the passphrase. 081 * 082 * @return the passphrase. 083 * @since 2.10.0 084 */ 085 public byte[] getPassphrase() { 086 return Utils.clone(passphrase); 087 } 088 089 /** 090 * Gets the passphrase. 091 * 092 * @return the passphrase. 093 * @deprecated Use {@link #getPassphrase()}. 094 */ 095 @Deprecated 096 public byte[] getPassPhrase() { 097 return Utils.clone(passphrase); 098 } 099 100 /** 101 * Gets the private key. 102 * 103 * @return the private key. 104 */ 105 public byte[] getPrivateKeyBytes() { 106 return Utils.clone(privateKey); 107 } 108 109 /** 110 * Gets the public key. 111 * 112 * @return the public key. 113 */ 114 public byte[] getPublicKeyBytes() { 115 return Utils.clone(publicKey); 116 } 117 118 @Override 119 public int hashCode() { 120 return Objects.hash(Arrays.hashCode(passphrase), Arrays.hashCode(privateKey), Arrays.hashCode(publicKey)); 121 } 122}