001/* SecretKeySpec.java -- Wrapper for secret keys. 002 Copyright (C) 2004 Free Software Foundation, Inc. 003 004This file is part of GNU Classpath. 005 006GNU Classpath is free software; you can redistribute it and/or modify 007it under the terms of the GNU General Public License as published by 008the Free Software Foundation; either version 2, or (at your option) 009any later version. 010 011GNU Classpath is distributed in the hope that it will be useful, but 012WITHOUT ANY WARRANTY; without even the implied warranty of 013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014General Public License for more details. 015 016You should have received a copy of the GNU General Public License 017along with GNU Classpath; see the file COPYING. If not, write to the 018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 01902110-1301 USA. 020 021Linking this library statically or dynamically with other modules is 022making a combined work based on this library. Thus, the terms and 023conditions of the GNU General Public License cover the whole 024combination. 025 026As a special exception, the copyright holders of this library give you 027permission to link this library with independent modules to produce an 028executable, regardless of the license terms of these independent 029modules, and to copy and distribute the resulting executable under 030terms of your choice, provided that you also meet, for each linked 031independent module, the terms and conditions of the license of that 032module. An independent module is a module which is not derived from 033or based on this library. If you modify this library, you may extend 034this exception to your version of the library, but you are not 035obligated to do so. If you do not wish to do so, delete this 036exception statement from your version. */ 037 038 039package javax.crypto.spec; 040 041import java.security.spec.KeySpec; 042 043import javax.crypto.SecretKey; 044 045/** 046 * This is a simple wrapper around a raw byte array, for ciphers that do 047 * not require any key parameters other than the bytes themselves. 048 * 049 * <p>Since this class implements {@link javax.crypto.SecretKey}, which 050 * in turn extends {@link java.security.Key}, so instances of this class 051 * may be passed directly to the <code>init()</code> methods of {@link 052 * javax.crypto.Cipher}. 053 * 054 * @see javax.crypto.SecretKey 055 * @see javax.crypto.SecretKeyFactory 056 */ 057public class SecretKeySpec implements KeySpec, SecretKey 058{ 059 060 // Constants and fields. 061 // ------------------------------------------------------------------------ 062 063 /** Compatible with JDK1.4. */ 064 private static final long serialVersionUID = 6577238317307289933L; 065 066 /** The key bytes. */ 067 private byte[] key; 068 069 /** The algorithm's name. */ 070 private String algorithm; 071 072 // Constructors. 073 // ------------------------------------------------------------------------ 074 075 /** 076 * Create a new secret key spec from an entire byte array. 077 * 078 * @param key The key material. 079 * @param algorithm The name of the algorithm using this key. 080 */ 081 public SecretKeySpec(byte[] key, String algorithm) 082 { 083 this(key, 0, key.length, algorithm); 084 } 085 086 /** 087 * Create a new secret key spec from part of a byte array. 088 * 089 * @param key The key material. 090 * @param off The offset at which key material begins. 091 * @param len The length of key material. 092 * @param algorithm The name of the algorithm using this key. 093 */ 094 public SecretKeySpec(byte[] key, int off, int len, String algorithm) 095 { 096 this.key = new byte[len]; 097 this.algorithm = algorithm; 098 System.arraycopy(key, off, this.key, 0, len); 099 } 100 101 // Instance methods. 102 // ------------------------------------------------------------------------ 103 104 /** 105 * Return the name of the algorithm associated with this secret key. 106 * 107 * @return The algorithm's name. 108 */ 109 public String getAlgorithm() 110 { 111 return algorithm; 112 } 113 114 /** 115 * Return the key as a byte array. 116 * 117 * @return The key material. 118 */ 119 public byte[] getEncoded() 120 { 121 return key; 122 } 123 124 /** 125 * This key's format, which is always "RAW". 126 * 127 * @return "RAW" 128 */ 129 public String getFormat() 130 { 131 return "RAW"; 132 } 133 134 public boolean equals(Object o) 135 { 136 if (o instanceof SecretKeySpec) 137 { 138 byte[] okey = ((SecretKeySpec) o).getEncoded(); 139 if (key.length != okey.length) 140 return false; 141 for (int i = 0; i < key.length; i++) 142 { 143 if (key[i] != okey[i]) 144 return false; 145 } 146 return algorithm.equals(((SecretKeySpec) o).getAlgorithm()); 147 } 148 else 149 { 150 return false; 151 } 152 } 153 154 public int hashCode() 155 { 156 int code = 0; 157 for (int i = 0; i < key.length; i++) 158 { 159 code ^= (key[i] & 0xff) << (i << 3 & 31); 160 } 161 return code ^ algorithm.hashCode(); 162 } 163}