01: /*
02: *
03: *
04: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
05: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License version
09: * 2 only, as published by the Free Software Foundation.
10: *
11: * This program is distributed in the hope that it will be useful, but
12: * WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * General Public License version 2 for more details (a copy is
15: * included at /legal/license.txt).
16: *
17: * You should have received a copy of the GNU General Public License
18: * version 2 along with this work; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA
21: *
22: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
23: * Clara, CA 95054 or visit www.sun.com if you need additional
24: * information or have any questions.
25: */
26:
27: package com.sun.midp.crypto;
28:
29: /**
30: * Implements the base interface for keys used in symmetric algorithms.
31: */
32: public final class SecretKey implements Key {
33: /** Type of key, e.g. DES, RSA etc. */
34: String alg;
35:
36: /** Local secret. */
37: byte[] secret = null;
38:
39: /**
40: * Constructs a secret key from the given byte array, using the first len
41: * bytes of key, starting at offset inclusive.
42: * </p>
43: * The bytes that constitute the secret key are those between key[offset]
44: * and key[offset+len-1] inclusive.
45: * </p>
46: * This constructor does not check if the given bytes indeed specify a
47: * secret key of the specified algorithm. For example, if the algorithm
48: * is DES, this constructor does not check if key is 8 bytes long, and
49: * also does not check for weak or semi-weak keys. In order for those
50: * checks to be performed, an algorithm-specific key specification class
51: * must be used.
52: *
53: * @param key the key material of the secret key.
54: * @param offset the offset in key where the key material starts.
55: * @param len the length of the key material.
56: * @param algorithm the ID of the secret-key algorithm to be associated
57: * with the given key material.
58: */
59: public SecretKey(byte[] key, int offset, int len, String algorithm) {
60: alg = algorithm;
61: secret = Util.cloneSubarray(key, offset, len);
62: }
63:
64: /**
65: * Returns the name of the algorithm associated with this secret key.
66: *
67: * @return the secret key algorithm.
68: */
69: public String getAlgorithm() {
70: return alg;
71: }
72:
73: /**
74: * Returns the name of the encoding format for this secret key.
75: *
76: * @return the string "RAW".
77: */
78: public String getFormat() {
79: return "RAW";
80: }
81:
82: /**
83: * Returns the key material of this secret key.
84: *
85: * @return the key material
86: */
87: public byte[] getEncoded() {
88: return Util.cloneArray(secret);
89: }
90: }
|