001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program 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 version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: * NOTE:
027: * Because of various external restrictions (i.e. US export
028: * regulations, etc.), the actual source code can not be provided
029: * at this time. This file represents the skeleton of the source
030: * file, so that javadocs of the API can be created.
031: */
032:
033: package javax.crypto.spec;
034:
035: import com.sun.midp.crypto.Util;
036:
037: import java.security.Key;
038: import java.security.spec.KeySpec;
039:
040: /**
041: * This class specifies a secret key in a provider-independent fashion.
042: *
043: * <p>It can be used to construct a <code>SecretKey</code> from a byte array.
044: *
045: * <p>This class is only useful for raw secret keys that can be represented as
046: * a byte array and have no key parameters associated with them, e.g., DES or
047: * Triple DES keys.
048: *
049: *
050: * @version 1.16, 07/31/01
051: *
052: */
053: public class SecretKeySpec implements KeySpec, Key {
054: /**
055: * The secret key.
056: */
057: private byte[] keyData;
058:
059: /**
060: * The name of the algorithm associated with this key.
061: */
062: private String algorithm;
063:
064: /**
065: * Constructs a secret key from the given byte array, using the first
066: * <code>len</code> bytes of <code>key</code>, starting at
067: * <code>offset</code> inclusive.
068: *
069: * <p> The bytes that constitute the secret key are
070: * those between <code>key[offset]</code> and
071: * <code>key[offset+len-1]</code> inclusive.
072: *
073: * <p>This constructor does not check if the given bytes indeed specify a
074: * secret key of the specified algorithm. For example, if the algorithm is
075: * DES, this constructor does not check if <code>key</code> is 8 bytes
076: * long, and also does not check for weak or semi-weak keys.
077: * In order for those checks to be performed, an algorithm-specific key
078: * specification class
079: * must be used.
080: *
081: * @param key the key material of the secret key.
082: * @param offset the offset in <code>key</code> where the key material
083: * starts.
084: * @param len the length of the key material.
085: * @param algorithm the name of the secret-key algorithm to be associated
086: * with the given key material.
087: * See Appendix A in the
088: * Java Cryptography Extension Reference Guide
089: * for information about standard algorithm names.
090: */
091: public SecretKeySpec(byte[] key, int offset, int len,
092: String algorithm) {
093: keyData = new byte[len];
094: System.arraycopy(key, offset, keyData, 0, len);
095: this .algorithm = algorithm.toUpperCase();
096: }
097:
098: /**
099: * Returns the name of the algorithm associated with this secret key.
100: *
101: * @return the secret key algorithm.
102: */
103: public String getAlgorithm() {
104: return algorithm;
105: }
106:
107: /**
108: * Returns the name of the encoding format for this secret key.
109: *
110: * @return the string "RAW".
111: */
112: public String getFormat() {
113: return "RAW";
114: }
115:
116: /**
117: * Returns the key material of this secret key.
118: *
119: * @return the key material
120: */
121: public byte[] getEncoded() {
122: return Util.cloneArray(keyData);
123: }
124: }
|