001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Alexander Y. Kleymenov
020: * @version $Revision$
021: */package javax.crypto.spec;
022:
023: import java.security.spec.KeySpec;
024: import java.util.Arrays;
025:
026: import org.apache.harmony.crypto.internal.nls.Messages;
027:
028: /**
029: * @com.intel.drl.spec_ref
030: */
031: public class PBEKeySpec implements KeySpec {
032:
033: private char[] password;
034: private final byte[] salt;
035: private final int iterationCount;
036: private final int keyLength;
037:
038: /**
039: * @com.intel.drl.spec_ref
040: */
041: public PBEKeySpec(char[] password) {
042: if (password == null) {
043: this .password = new char[0];
044: } else {
045: this .password = new char[password.length];
046: System.arraycopy(password, 0, this .password, 0,
047: password.length);
048: }
049: salt = null;
050: iterationCount = 0;
051: keyLength = 0;
052: }
053:
054: /**
055: * @com.intel.drl.spec_ref
056: */
057: public PBEKeySpec(char[] password, byte[] salt, int iterationCount,
058: int keyLength) {
059: if (salt == null) {
060: throw new NullPointerException(Messages
061: .getString("crypto.3B")); //$NON-NLS-1$
062: }
063: if (salt.length == 0) {
064: throw new IllegalArgumentException(Messages
065: .getString("crypto.3C")); //$NON-NLS-1$
066: }
067: if (iterationCount <= 0) {
068: throw new IllegalArgumentException(Messages
069: .getString("crypto.3D")); //$NON-NLS-1$
070: }
071: if (keyLength <= 0) {
072: throw new IllegalArgumentException(Messages
073: .getString("crypto.3E")); //$NON-NLS-1$
074: }
075:
076: if (password == null) {
077: this .password = new char[0];
078: } else {
079: this .password = new char[password.length];
080: System.arraycopy(password, 0, this .password, 0,
081: password.length);
082: }
083: this .salt = new byte[salt.length];
084: System.arraycopy(salt, 0, this .salt, 0, salt.length);
085: this .iterationCount = iterationCount;
086: this .keyLength = keyLength;
087: }
088:
089: /**
090: * @com.intel.drl.spec_ref
091: */
092: public PBEKeySpec(char[] password, byte[] salt, int iterationCount) {
093: if (salt == null) {
094: throw new NullPointerException(Messages
095: .getString("crypto.3B")); //$NON-NLS-1$
096: }
097: if (salt.length == 0) {
098: throw new IllegalArgumentException(Messages
099: .getString("crypto.3C")); //$NON-NLS-1$
100: }
101: if (iterationCount <= 0) {
102: throw new IllegalArgumentException(Messages
103: .getString("crypto.3D")); //$NON-NLS-1$
104: }
105:
106: if (password == null) {
107: this .password = new char[0];
108: } else {
109: this .password = new char[password.length];
110: System.arraycopy(password, 0, this .password, 0,
111: password.length);
112: }
113: this .salt = new byte[salt.length];
114: System.arraycopy(salt, 0, this .salt, 0, salt.length);
115: this .iterationCount = iterationCount;
116: this .keyLength = 0;
117: }
118:
119: /**
120: * @com.intel.drl.spec_ref
121: */
122: public final void clearPassword() {
123: Arrays.fill(password, '?');
124: password = null;
125: }
126:
127: /**
128: * @com.intel.drl.spec_ref
129: */
130: public final char[] getPassword() {
131: if (password == null) {
132: throw new IllegalStateException(Messages
133: .getString("crypto.3F")); //$NON-NLS-1$
134: }
135: char[] result = new char[password.length];
136: System.arraycopy(password, 0, result, 0, password.length);
137: return result;
138: }
139:
140: /**
141: * @com.intel.drl.spec_ref
142: */
143: public final byte[] getSalt() {
144: if (salt == null) {
145: return null;
146: }
147: byte[] result = new byte[salt.length];
148: System.arraycopy(salt, 0, result, 0, salt.length);
149: return result;
150: }
151:
152: /**
153: * @com.intel.drl.spec_ref
154: */
155: public final int getIterationCount() {
156: return iterationCount;
157: }
158:
159: /**
160: * @com.intel.drl.spec_ref
161: */
162: public final int getKeyLength() {
163: return keyLength;
164: }
165: }
|