1 package net.sourceforge.lightcrypto.test; 2 3 import junit.framework.TestCase; 4 import junit.framework.Assert; 5 6 import java.io.*; 7 8 import net.sourceforge.lightcrypto.Key; 9 import net.sourceforge.lightcrypto.SafeObject; 10 import net.sourceforge.lightcrypto.Macs; 11 12 22 23 public class MacTest extends TestCase { 24 private StringBuffer text1; 25 private StringBuffer text2; 26 private StringBuffer text3; 27 private StringBuffer text4; 28 private StringBuffer text5; 29 private StringBuffer text6; 30 private StringBuffer text7; 31 private StringBuffer text8; 32 private StringBuffer text9; 33 private StringBuffer text10; 34 35 StringBuffer mac = null; 36 StringBuffer plaintext = null; 37 38 43 protected void setUp() throws IOException { 44 text1 = new StringBuffer ("The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms"); 45 text2 = new StringBuffer ("This software is distributed under a license based on the MIT X Consortium license"); 46 text3 = new StringBuffer ("found in $JAVA_HOME/jre/lib/security/java.security, where $JAVA_HOME is the location of your JDK/JRE distribution"); 47 text4 = new StringBuffer ("Mit Project 2002 zum erfolgreichen Projektmanagement Damit Sie in Zukunft Ihre Projekte präzise und komfortabel steuern können"); 48 text5 = new StringBuffer ("En av de största nyheterna är att det finns en .NET Enterprise Server-lösning för stora företagsomspännade projekt"); 49 text6 = new StringBuffer ("Lees de productinformatie en ontdek alles over de krachtige tools binnen Visual Studio .NET"); 50 text7 = new StringBuffer ("Vergeet even die oude tovenaars met puntige hoeden en rondborstige jonkvrouwen in nood... oké, vergeet in ieder geval even die tovenaars, want Lionheart komt met een ambitieuze rollenspelvariant"); 51 text8 = new StringBuffer ("An implementation of ECIES (stream mode) as described in IEEE P 1363a."); 52 text9 = new StringBuffer ("This makes the entire keystore resistant to tampering and inspection, and forces verification"); 53 text10 = new StringBuffer ("application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature"); 54 55 FileOutputStream outStr = new FileOutputStream(RunTest.TEMPFOLDER + "readable.txt"); 57 DataOutputStream dataStr = new DataOutputStream(outStr); 58 59 dataStr.writeBytes("This is a readable string inside a file"); 60 61 dataStr.flush(); 62 dataStr.close(); 63 64 outStr.close(); 65 } 66 67 72 public void testMac() throws Exception { 73 Key.generatekey(RunTest.TEMPFOLDER + "tempkey.key", new StringBuffer ("password")); 75 76 SafeObject k = new SafeObject(); 77 k = Key.loadkey(RunTest.TEMPFOLDER + "tempkey.key", new StringBuffer ("password")); 78 79 mac = Macs.mac(text1, k); 80 Assert.assertTrue(Macs.macEquals(text1, mac, k)); 81 82 mac = Macs.mac(text2, k); 83 Assert.assertTrue(Macs.macEquals(text2, mac, k)); 84 85 mac = Macs.mac(text3, k); 86 Assert.assertTrue(Macs.macEquals(text3, mac, k)); 87 88 mac = Macs.mac(text4, k); 89 Assert.assertTrue(Macs.macEquals(text4, mac, k)); 90 91 mac = Macs.mac(text5, k); 92 Assert.assertTrue(Macs.macEquals(text5, mac, k)); 93 94 mac = Macs.mac(text6, k); 95 Assert.assertTrue(Macs.macEquals(text6, mac, k)); 96 97 mac = Macs.mac(text7, k); 98 Assert.assertTrue(Macs.macEquals(text7, mac, k)); 99 100 mac = Macs.mac(text8, k); 101 Assert.assertTrue(Macs.macEquals(text8, mac, k)); 102 103 mac = Macs.mac(text9, k); 104 Assert.assertTrue(Macs.macEquals(text9, mac, k)); 105 106 mac = Macs.mac(text10, k); 107 Assert.assertTrue(Macs.macEquals(text10, mac, k)); 108 109 mac = Macs.mac(text10, k); 110 Assert.assertFalse(Macs.macEquals(text9, mac, k)); 111 } 112 113 118 public void testFileEncryption() throws Exception { 119 SafeObject k = new SafeObject(); 120 k = Key.loadkey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer ("password")); 121 122 mac = Macs.macFromFile(RunTest.TEMPFOLDER + "readable.txt",k); 123 Assert.assertTrue(Macs.macEqualsFile(RunTest.TEMPFOLDER + "readable.txt",mac,k)); 124 125 } 126 } 127 | Popular Tags |