Path: blob/master/test/jdk/com/sun/crypto/provider/CICO/PBEFunc/AbstractPBEWrapper.java
41161 views
/*1* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22import java.security.GeneralSecurityException;23import javax.crypto.Cipher;2425/**26* PBEWrapper is an abstract class for all concrete PBE Cipher wrappers.27*/28public abstract class AbstractPBEWrapper {29/**30* Iteration count.31*/32public static final int DEFAULT_ITERATION = 1000;3334public static final String PBKDF2 = "PBKDF2";35public static final String AES = "AES";36public static final String DEFAULT = "default";3738/**39* transformation the name of the transformation, e.g.,40* DES/CBC/PKCS5Padding41*/42protected final String transformation;4344/**45* the standard name of the requested secret-key algorithm.46*/47protected final String baseAlgo;4849/**50* The contents of salt are copied to protect against subsequent51* modification.52*/53protected final byte[] salt;5455/**56* Password.57*/58protected final String password;5960/**61* PBEWrapper creator.62*63* @param algo PBE algorithm to test64* @param passwd a password phrase65* @return PBEWrapper in accordance to requested algo.66* @throws GeneralSecurityException all exceptions are thrown.67*/68public static AbstractPBEWrapper createWrapper(PBEAlgorithm algo, String passwd)69throws GeneralSecurityException {70switch (algo.type) {71case PBKDF2:72return new PBKDF2Wrapper(algo, passwd);73case AES:74return new AESPBEWrapper(algo, passwd);75default:76return new DefaultPBEWrapper(algo, passwd);77}78}7980/**81* PBEWrapper constructor.82*83* @param algo algorithm to wrap84* @param password password phrase85* @param saltSize salt size (defined in subclasses)86*/87protected AbstractPBEWrapper(PBEAlgorithm algo, String password, int saltSize) {88this.transformation = algo.getTransformation();89this.baseAlgo = algo.baseAlgo;90this.salt = TestUtilities.generateBytes(saltSize);91this.password = password;92}9394/**95* Initialize Cipher object for the operation requested in the mode parameter.96*97* @param mode encryption or decryption98* @return a cipher initialize by mode.99* @throws GeneralSecurityException all security exceptions are thrown.100*/101protected abstract Cipher initCipher(int mode) throws GeneralSecurityException;102}103104105