1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package tests.security; 18 19 import java.security.AlgorithmParameters; 20 import java.security.InvalidAlgorithmParameterException; 21 import java.security.InvalidKeyException; 22 import java.security.KeyPair; 23 import java.security.KeyPairGenerator; 24 import java.security.NoSuchAlgorithmException; 25 import java.security.Signature; 26 import java.security.SignatureException; 27 import java.security.spec.AlgorithmParameterSpec; 28 import java.security.spec.InvalidParameterSpecException; 29 import junit.framework.Assert; 30 31 public class AlgorithmParameterSignatureHelper<T extends AlgorithmParameterSpec> 32 extends TestHelper<AlgorithmParameters> { 33 34 private final String algorithmName; 35 private final String plainData = "some data do sign and verify"; 36 private final Class<T> parameterSpecClass; 37 38 public AlgorithmParameterSignatureHelper(String algorithmName, Class<T> parameterSpecCla1ss) { 39 this.algorithmName = algorithmName; 40 this.parameterSpecClass = parameterSpecCla1ss; 41 } 42 43 @Override 44 public void test(AlgorithmParameters parameters) throws Exception { 45 Signature signature = Signature.getInstance(algorithmName); 46 T parameterSpec = parameters.getParameterSpec(parameterSpecClass); 47 KeyPairGenerator generator = KeyPairGenerator.getInstance(algorithmName); 48 49 generator.initialize(parameterSpec); 50 KeyPair keyPair = generator.genKeyPair(); 51 52 signature.initSign(keyPair.getPrivate()); 53 signature.update(plainData.getBytes()); 54 byte[] signed = signature.sign(); 55 56 signature.initVerify(keyPair.getPublic()); 57 signature.update(plainData.getBytes()); 58 Assert.assertTrue("signature should verify", signature.verify(signed)); 59 } 60 } 61