View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.vfs2.provider.sftp;
19  
20  import java.util.Arrays;
21  import java.util.Objects;
22  
23  import com.jcraft.jsch.JSch;
24  import com.jcraft.jsch.JSchException;
25  
26  /**
27   * Structure for an identity based on byte arrays.
28   *
29   * @since 2.4
30   */
31  public class BytesIdentityInfo implements IdentityProvider {
32  
33      private final byte[] passphrase;
34  
35      private final byte[] privateKey;
36  
37      private final byte[] publicKey;
38  
39      /**
40       * Constructs an identity info with private and passphrase for the private key.
41       *
42       * @param privateKey Private key bytes
43       * @param passphrase The passphrase to decrypt the private key (can be {@code null} if no passphrase is used)
44       */
45      public BytesIdentityInfo(final byte[] privateKey, final byte[] passphrase) {
46          this(privateKey, null, passphrase);
47      }
48  
49      /**
50       * Constructs an identity info with private and public key and passphrase for the private key.
51       *
52       * @param privateKey Private key bytes
53       * @param publicKey The public key part used for connections with exchange of certificates (can be {@code null})
54       * @param passphrase The passphrase to decrypt the private key (can be {@code null} if no passphrase is used)
55       */
56      public BytesIdentityInfo(final byte[] privateKey, final byte[] publicKey, final byte[] passphrase) {
57          this.privateKey = Utils.clone(privateKey);
58          this.publicKey = Utils.clone(publicKey);
59          this.passphrase = Utils.clone(passphrase);
60      }
61  
62      @Override
63      public void addIdentity(final JSch jsch) throws JSchException {
64          jsch.addIdentity("PrivateKey", privateKey, publicKey, passphrase);
65      }
66  
67      @Override
68      public boolean equals(final Object obj) {
69          if (this == obj) {
70              return true;
71          }
72          if (!(obj instanceof BytesIdentityInfo)) {
73              return false;
74          }
75          final BytesIdentityInfo other = (BytesIdentityInfo) obj;
76          return Arrays.equals(passphrase, other.passphrase) && Arrays.equals(privateKey, other.privateKey) && Arrays.equals(publicKey, other.publicKey);
77      }
78  
79      /**
80       * Gets the passphrase.
81       *
82       * @return the passphrase.
83       * @since 2.10.0
84       */
85      public byte[] getPassphrase() {
86          return Utils.clone(passphrase);
87      }
88  
89      /**
90       * Gets the passphrase.
91       *
92       * @return the passphrase.
93       * @deprecated Use {@link #getPassphrase()}.
94       */
95      @Deprecated
96      public byte[] getPassPhrase() {
97          return Utils.clone(passphrase);
98      }
99  
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 }