Source Code Cross Referenced for DSA2Test.java in  » Security » Bouncy-Castle » org » bouncycastle » openpgp » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Security » Bouncy Castle » org.bouncycastle.openpgp.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.bouncycastle.openpgp.test;
002:
003:        import junit.framework.Test;
004:        import junit.framework.TestCase;
005:        import junit.framework.TestSuite;
006:        import org.bouncycastle.bcpg.BCPGOutputStream;
007:        import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
008:        import org.bouncycastle.openpgp.PGPCompressedData;
009:        import org.bouncycastle.openpgp.PGPLiteralData;
010:        import org.bouncycastle.openpgp.PGPLiteralDataGenerator;
011:        import org.bouncycastle.openpgp.PGPObjectFactory;
012:        import org.bouncycastle.openpgp.PGPOnePassSignature;
013:        import org.bouncycastle.openpgp.PGPOnePassSignatureList;
014:        import org.bouncycastle.openpgp.PGPPublicKeyRing;
015:        import org.bouncycastle.openpgp.PGPSecretKeyRing;
016:        import org.bouncycastle.openpgp.PGPSignature;
017:        import org.bouncycastle.openpgp.PGPSignatureGenerator;
018:        import org.bouncycastle.openpgp.PGPSignatureList;
019:        import org.bouncycastle.openpgp.PGPUtil;
020:        import org.bouncycastle.util.test.UncloseableOutputStream;
021:
022:        import java.io.ByteArrayInputStream;
023:        import java.io.ByteArrayOutputStream;
024:        import java.io.FileInputStream;
025:        import java.io.InputStream;
026:        import java.io.OutputStream;
027:        import java.security.Security;
028:        import java.util.Date;
029:
030:        /**
031:         * GPG compatability test vectors
032:         */
033:        public class DSA2Test extends TestCase {
034:            private static final String TEST_DATA_HOME = "bc.test.data.home";
035:
036:            public void setUp() {
037:                if (Security.getProvider("BC") == null) {
038:                    Security
039:                            .addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
040:                }
041:            }
042:
043:            public void testK1024H160() throws Exception {
044:                doSigVerifyTest("DSA-1024-160.pub", "dsa-1024-160-sign.gpg");
045:            }
046:
047:            public void testK1024H224() throws Exception {
048:                doSigVerifyTest("DSA-1024-160.pub", "dsa-1024-224-sign.gpg");
049:            }
050:
051:            public void testK1024H256() throws Exception {
052:                doSigVerifyTest("DSA-1024-160.pub", "dsa-1024-256-sign.gpg");
053:            }
054:
055:            public void testK1024H384() throws Exception {
056:                doSigVerifyTest("DSA-1024-160.pub", "dsa-1024-384-sign.gpg");
057:            }
058:
059:            public void testK1024H512() throws Exception {
060:                doSigVerifyTest("DSA-1024-160.pub", "dsa-1024-512-sign.gpg");
061:            }
062:
063:            public void testK2048H224() throws Exception {
064:                doSigVerifyTest("DSA-2048-224.pub", "dsa-2048-224-sign.gpg");
065:            }
066:
067:            public void testK3072H256() throws Exception {
068:                doSigVerifyTest("DSA-3072-256.pub", "dsa-3072-256-sign.gpg");
069:            }
070:
071:            public void testK7680H384() throws Exception {
072:                doSigVerifyTest("DSA-7680-384.pub", "dsa-7680-384-sign.gpg");
073:            }
074:
075:            public void testK15360H512() throws Exception {
076:                doSigVerifyTest("DSA-15360-512.pub", "dsa-15360-512-sign.gpg");
077:            }
078:
079:            public void testGenerateK1024H224() throws Exception {
080:                doSigGenerateTest("DSA-1024-160.sec", "DSA-1024-160.pub",
081:                        PGPUtil.SHA224);
082:            }
083:
084:            public void testGenerateK1024H256() throws Exception {
085:                doSigGenerateTest("DSA-1024-160.sec", "DSA-1024-160.pub",
086:                        PGPUtil.SHA256);
087:            }
088:
089:            public void testGenerateK1024H384() throws Exception {
090:                doSigGenerateTest("DSA-1024-160.sec", "DSA-1024-160.pub",
091:                        PGPUtil.SHA384);
092:            }
093:
094:            public void testGenerateK1024H512() throws Exception {
095:                doSigGenerateTest("DSA-1024-160.sec", "DSA-1024-160.pub",
096:                        PGPUtil.SHA512);
097:            }
098:
099:            public void testGenerateK2048H256() throws Exception {
100:                doSigGenerateTest("DSA-2048-224.sec", "DSA-2048-224.pub",
101:                        PGPUtil.SHA256);
102:            }
103:
104:            public void testGenerateK2048H512() throws Exception {
105:                doSigGenerateTest("DSA-2048-224.sec", "DSA-2048-224.pub",
106:                        PGPUtil.SHA512);
107:            }
108:
109:            private void doSigGenerateTest(String privateKeyFile,
110:                    String publicKeyFile, int digest) throws Exception {
111:                PGPSecretKeyRing secRing = loadSecretKey(privateKeyFile);
112:                PGPPublicKeyRing pubRing = loadPublicKey(publicKeyFile);
113:                String data = "hello world!";
114:                ByteArrayOutputStream bOut = new ByteArrayOutputStream();
115:                ByteArrayInputStream testIn = new ByteArrayInputStream(data
116:                        .getBytes());
117:                PGPSignatureGenerator sGen = new PGPSignatureGenerator(
118:                        PublicKeyAlgorithmTags.DSA, digest, "BC");
119:
120:                sGen.initSign(PGPSignature.BINARY_DOCUMENT, secRing
121:                        .getSecretKey().extractPrivateKey("test".toCharArray(),
122:                                "BC"));
123:
124:                BCPGOutputStream bcOut = new BCPGOutputStream(bOut);
125:
126:                sGen.generateOnePassVersion(false).encode(bcOut);
127:
128:                PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
129:
130:                Date testDate = new Date(
131:                        (System.currentTimeMillis() / 1000) * 1000);
132:                OutputStream lOut = lGen.open(
133:                        new UncloseableOutputStream(bcOut),
134:                        PGPLiteralData.BINARY, "_CONSOLE",
135:                        data.getBytes().length, testDate);
136:
137:                int ch;
138:                while ((ch = testIn.read()) >= 0) {
139:                    lOut.write(ch);
140:                    sGen.update((byte) ch);
141:                }
142:
143:                lGen.close();
144:
145:                sGen.generate().encode(bcOut);
146:
147:                PGPObjectFactory pgpFact = new PGPObjectFactory(bOut
148:                        .toByteArray());
149:                PGPOnePassSignatureList p1 = (PGPOnePassSignatureList) pgpFact
150:                        .nextObject();
151:                PGPOnePassSignature ops = p1.get(0);
152:
153:                assertEquals(digest, ops.getHashAlgorithm());
154:                assertEquals(PublicKeyAlgorithmTags.DSA, ops.getKeyAlgorithm());
155:
156:                PGPLiteralData p2 = (PGPLiteralData) pgpFact.nextObject();
157:                if (!p2.getModificationTime().equals(testDate)) {
158:                    fail("Modification time not preserved");
159:                }
160:
161:                InputStream dIn = p2.getInputStream();
162:
163:                ops.initVerify(pubRing.getPublicKey(), "BC");
164:
165:                while ((ch = dIn.read()) >= 0) {
166:                    ops.update((byte) ch);
167:                }
168:
169:                PGPSignatureList p3 = (PGPSignatureList) pgpFact.nextObject();
170:                PGPSignature sig = p3.get(0);
171:
172:                assertEquals(digest, sig.getHashAlgorithm());
173:                assertEquals(PublicKeyAlgorithmTags.DSA, sig.getKeyAlgorithm());
174:
175:                assertTrue(ops.verify(sig));
176:            }
177:
178:            private void doSigVerifyTest(String publicKeyFile, String sigFile)
179:                    throws Exception {
180:                PGPPublicKeyRing publicKey = loadPublicKey(publicKeyFile);
181:                PGPObjectFactory pgpFact = loadSig(sigFile);
182:
183:                PGPCompressedData c1 = (PGPCompressedData) pgpFact.nextObject();
184:
185:                pgpFact = new PGPObjectFactory(c1.getDataStream());
186:
187:                PGPOnePassSignatureList p1 = (PGPOnePassSignatureList) pgpFact
188:                        .nextObject();
189:                PGPOnePassSignature ops = p1.get(0);
190:
191:                PGPLiteralData p2 = (PGPLiteralData) pgpFact.nextObject();
192:
193:                InputStream dIn = p2.getInputStream();
194:
195:                ops.initVerify(publicKey.getPublicKey(), "BC");
196:
197:                int ch;
198:                while ((ch = dIn.read()) >= 0) {
199:                    ops.update((byte) ch);
200:                }
201:
202:                PGPSignatureList p3 = (PGPSignatureList) pgpFact.nextObject();
203:
204:                assertTrue(ops.verify(p3.get(0)));
205:            }
206:
207:            private PGPObjectFactory loadSig(String sigName) throws Exception {
208:                FileInputStream fIn = new FileInputStream(getDataHome()
209:                        + "/sigs/" + sigName);
210:
211:                return new PGPObjectFactory(fIn);
212:            }
213:
214:            private PGPPublicKeyRing loadPublicKey(String keyName)
215:                    throws Exception {
216:                FileInputStream fIn = new FileInputStream(getDataHome()
217:                        + "/keys/" + keyName);
218:
219:                return new PGPPublicKeyRing(fIn);
220:            }
221:
222:            private PGPSecretKeyRing loadSecretKey(String keyName)
223:                    throws Exception {
224:                FileInputStream fIn = new FileInputStream(getDataHome()
225:                        + "/keys/" + keyName);
226:
227:                return new PGPSecretKeyRing(fIn);
228:            }
229:
230:            private String getDataHome() {
231:                String dataHome = System.getProperty(TEST_DATA_HOME);
232:
233:                if (dataHome == null) {
234:                    throw new IllegalStateException(TEST_DATA_HOME
235:                            + " property not set");
236:                }
237:
238:                return dataHome + "/openpgp/dsa";
239:            }
240:
241:            public static void main(String[] args) throws Exception {
242:                junit.textui.TestRunner.run(suite());
243:            }
244:
245:            public static Test suite() throws Exception {
246:                TestSuite suite = new TestSuite("GPG DSA2 tests");
247:
248:                suite.addTestSuite(DSA2Test.class);
249:
250:                return suite;
251:            }
252:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.