001    /* SecretKeySpec.java -- Wrapper for secret keys.
002       Copyright (C) 2004  Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package javax.crypto.spec;
040    
041    import java.security.spec.KeySpec;
042    
043    import 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     */
057    public 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    }