/* * Copyright (C) 2005-2008 Alfresco Software Limited. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * As a special exception to the terms and conditions of version 2.0 of * the GPL, you may redistribute this Program in connection with Free/Libre * and Open Source Software ("FLOSS") applications as described in Alfresco's * FLOSS exception. You should have received a copy of the text describing * the FLOSS exception, and it is also available here: * http://www.alfresco.com/legal/licensing" */ package org.alfresco.util.security; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.SecureRandom; import java.util.Arrays; import junit.framework.TestCase; /** * Tests that the EncryptingOutputStream and EncryptingInputStream classes work correctly. */ public class EncryptingOutputStreamTest extends TestCase { /** * Tests encryption / decryption by attempting to encrypt and decrypt the bytes forming this class definition and * comparing it with the unencrypted bytes. * * @throws Exception * an exception */ public void testEncrypt() throws Exception { final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); final SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); final byte[] seed = getClass().getName().getBytes("UTF-8"); random.setSeed(seed); keyGen.initialize(1024, random); final KeyPair pair = keyGen.generateKeyPair(); final ByteArrayOutputStream buff = new ByteArrayOutputStream(2048); final OutputStream encrypting = new EncryptingOutputStream(buff, pair.getPublic(), random); final ByteArrayOutputStream plainText1 = new ByteArrayOutputStream(2048); final InputStream in = getClass().getResourceAsStream(getClass().getSimpleName() + ".class"); final byte[] inbuff = new byte[17]; int bytesRead; while ((bytesRead = in.read(inbuff)) != -1) { encrypting.write(inbuff, 0, bytesRead); plainText1.write(inbuff, 0, bytesRead); } in.close(); encrypting.close(); plainText1.close(); final InputStream decrypting = new DecryptingInputStream(new ByteArrayInputStream(buff.toByteArray()), pair .getPrivate()); final ByteArrayOutputStream plainText2 = new ByteArrayOutputStream(2048); while ((bytesRead = decrypting.read(inbuff)) != -1) { plainText2.write(inbuff, 0, bytesRead); } assertTrue(Arrays.equals(plainText1.toByteArray(), plainText2.toByteArray())); } }