Home | History | Annotate | Download | only in security
      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.InvalidKeyException;
     20 import java.security.KeyPair;
     21 import java.security.NoSuchAlgorithmException;
     22 import java.security.PrivateKey;
     23 import java.security.PublicKey;
     24 import java.security.Signature;
     25 import java.security.SignatureException;
     26 import junit.framework.Assert;
     27 
     28 public class SignatureHelper extends TestHelper<KeyPair> {
     29 
     30     private final String algorithmName;
     31     private final String plainData = "some data do sign and verify";
     32 
     33     public SignatureHelper(String algorithmName) {
     34         this.algorithmName = algorithmName;
     35     }
     36 
     37     @Override
     38     public void test(KeyPair keyPair) {
     39         test(keyPair.getPrivate(), keyPair.getPublic());
     40     }
     41 
     42     public void test(PrivateKey encryptKey, PublicKey decryptKey) {
     43 
     44         Signature signature = null;
     45         try {
     46             signature = Signature.getInstance(algorithmName);
     47         } catch (NoSuchAlgorithmException e) {
     48             Assert.fail(e.getMessage());
     49         }
     50 
     51         try {
     52             signature.initSign(encryptKey);
     53         } catch (InvalidKeyException e) {
     54             Assert.fail(e.getMessage());
     55         }
     56 
     57         try {
     58             signature.update(plainData.getBytes());
     59         } catch (SignatureException e) {
     60             Assert.fail(e.getMessage());
     61         }
     62 
     63         byte[] signed = null;
     64         try {
     65             signed = signature.sign();
     66         } catch (SignatureException e) {
     67             Assert.fail(e.getMessage());
     68         }
     69 
     70         try {
     71             signature.initVerify(decryptKey);
     72         } catch (InvalidKeyException e) {
     73             Assert.fail(e.getMessage());
     74         }
     75 
     76         try {
     77             signature.update(plainData.getBytes());
     78         } catch (SignatureException e) {
     79             Assert.fail(e.getMessage());
     80         }
     81 
     82         try {
     83             Assert.assertTrue("signature could not be verified", signature
     84                     .verify(signed));
     85         } catch (SignatureException e) {
     86             Assert.fail(e.getMessage());
     87         }
     88     }
     89 }
     90