1f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson/* 2f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * Copyright (C) 2009 The Android Open Source Project 3f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * 4f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License"); 5f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * you may not use this file except in compliance with the License. 6f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * You may obtain a copy of the License at 7f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * 8f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 9f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * 10f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * Unless required by applicable law or agreed to in writing, software 11f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 12f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * See the License for the specific language governing permissions and 14f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * limitations under the License. 15f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson */ 16f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson 17f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonpackage tests.security; 18f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson 19f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.InvalidKeyException; 20f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.KeyPair; 21f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.NoSuchAlgorithmException; 22f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.PrivateKey; 23f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.PublicKey; 24f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.Signature; 25f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.SignatureException; 26f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport junit.framework.Assert; 27f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson 28f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonpublic class SignatureHelper extends TestHelper<KeyPair> { 29f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson 30f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson private final String algorithmName; 31f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson private final String plainData = "some data do sign and verify"; 32f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson 33f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson public SignatureHelper(String algorithmName) { 34f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson this.algorithmName = algorithmName; 35f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson } 36f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson 37f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson @Override 38420ea38aecdef0f5895c5e82751ebabe26bc0bd5Kenny Root public void test(KeyPair keyPair) throws Exception { 39f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson test(keyPair.getPrivate(), keyPair.getPublic()); 40f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson } 41f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson 42420ea38aecdef0f5895c5e82751ebabe26bc0bd5Kenny Root public void test(PrivateKey encryptKey, PublicKey decryptKey) throws Exception { 43420ea38aecdef0f5895c5e82751ebabe26bc0bd5Kenny Root Signature signature = Signature.getInstance(algorithmName); 44420ea38aecdef0f5895c5e82751ebabe26bc0bd5Kenny Root signature.initSign(encryptKey); 45420ea38aecdef0f5895c5e82751ebabe26bc0bd5Kenny Root signature.update(plainData.getBytes()); 46420ea38aecdef0f5895c5e82751ebabe26bc0bd5Kenny Root byte[] signed = signature.sign(); 47f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson 48420ea38aecdef0f5895c5e82751ebabe26bc0bd5Kenny Root signature.initVerify(decryptKey); 49420ea38aecdef0f5895c5e82751ebabe26bc0bd5Kenny Root signature.update(plainData.getBytes()); 50420ea38aecdef0f5895c5e82751ebabe26bc0bd5Kenny Root Assert.assertTrue("signature could not be verified", signature.verify(signed)); 51f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson } 52f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson} 53