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: * @author Alexander Y. Kleymenov
019: * @version $Revision$
020: */package javax.crypto.spec;
021:
022: import java.security.InvalidKeyException;
023: import java.security.spec.KeySpec;
024:
025: import org.apache.harmony.crypto.internal.nls.Messages;
026:
027: /**
028: * @com.intel.drl.spec_ref
029: */
030: public class DESedeKeySpec implements KeySpec {
031:
032: /**
033: * @com.intel.drl.spec_ref
034: */
035: public static final int DES_EDE_KEY_LEN = 24;
036:
037: private final byte[] key;
038:
039: /**
040: * @com.intel.drl.spec_ref
041: */
042: public DESedeKeySpec(byte[] key) throws InvalidKeyException {
043: if (key == null) {
044: throw new NullPointerException(Messages
045: .getString("crypto.2F")); //$NON-NLS-1$
046: }
047: if (key.length < DES_EDE_KEY_LEN) {
048: throw new InvalidKeyException(Messages
049: .getString("crypto.30")); //$NON-NLS-1$
050: }
051: this .key = new byte[DES_EDE_KEY_LEN];
052: System.arraycopy(key, 0, this .key, 0, DES_EDE_KEY_LEN);
053: }
054:
055: /**
056: * @com.intel.drl.spec_ref
057: */
058: public DESedeKeySpec(byte[] key, int offset)
059: throws InvalidKeyException {
060: if (key == null) {
061: throw new NullPointerException(Messages
062: .getString("crypto.2F")); //$NON-NLS-1$
063: }
064: if (key.length - offset < DES_EDE_KEY_LEN) {
065: throw new InvalidKeyException(Messages
066: .getString("crypto.30")); //$NON-NLS-1$
067: }
068: this .key = new byte[DES_EDE_KEY_LEN];
069: System.arraycopy(key, offset, this .key, 0, DES_EDE_KEY_LEN);
070: }
071:
072: /**
073: * @com.intel.drl.spec_ref
074: */
075: public byte[] getKey() {
076: byte[] result = new byte[DES_EDE_KEY_LEN];
077: System.arraycopy(this .key, 0, result, 0, DES_EDE_KEY_LEN);
078: return result;
079: }
080:
081: /**
082: * @com.intel.drl.spec_ref
083: */
084: public static boolean isParityAdjusted(byte[] key, int offset)
085: throws InvalidKeyException {
086: if (key.length - offset < DES_EDE_KEY_LEN) {
087: throw new InvalidKeyException(Messages
088: .getString("crypto.30")); //$NON-NLS-1$
089: }
090: for (int i = offset; i < DES_EDE_KEY_LEN + offset; i++) {
091: int b = key[i];
092: if ((((b & 1) + ((b & 2) >> 1) + ((b & 4) >> 2)
093: + ((b & 8) >> 3) + ((b & 16) >> 4)
094: + ((b & 32) >> 5) + ((b & 64) >> 6)) & 1) == ((b & 128) >> 7)) {
095: return false;
096: }
097: }
098: return true;
099: }
100: }
|