Source Code Cross Referenced for DigestTest.java in  » Security » Bouncy-Castle » org » bouncycastle » crypto » 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.crypto.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.bouncycastle.crypto.test;
002:
003:        import org.bouncycastle.crypto.Digest;
004:        import org.bouncycastle.util.encoders.Hex;
005:        import org.bouncycastle.util.test.SimpleTest;
006:
007:        public abstract class DigestTest extends SimpleTest {
008:            private Digest digest;
009:            private String[] input;
010:            private String[] results;
011:
012:            DigestTest(Digest digest, String[] input, String[] results) {
013:                this .digest = digest;
014:                this .input = input;
015:                this .results = results;
016:            }
017:
018:            public String getName() {
019:                return digest.getAlgorithmName();
020:            }
021:
022:            public void performTest() {
023:                byte[] resBuf = new byte[digest.getDigestSize()];
024:
025:                for (int i = 0; i < input.length - 1; i++) {
026:                    byte[] m = toByteArray(input[i]);
027:
028:                    vectorTest(digest, i, resBuf, m, Hex.decode(results[i]));
029:                }
030:
031:                byte[] lastV = toByteArray(input[input.length - 1]);
032:                byte[] lastDigest = Hex.decode(results[input.length - 1]);
033:
034:                vectorTest(digest, input.length - 1, resBuf, lastV, Hex
035:                        .decode(results[input.length - 1]));
036:
037:                //
038:                // clone test
039:                //
040:                digest.update(lastV, 0, lastV.length / 2);
041:
042:                // clone the Digest
043:                Digest d = cloneDigest(digest);
044:
045:                digest.update(lastV, lastV.length / 2, lastV.length
046:                        - lastV.length / 2);
047:                digest.doFinal(resBuf, 0);
048:
049:                if (!areEqual(lastDigest, resBuf)) {
050:                    fail("failing clone vector test",
051:                            results[results.length - 1], new String(Hex
052:                                    .encode(resBuf)));
053:                }
054:
055:                d.update(lastV, lastV.length / 2, lastV.length - lastV.length
056:                        / 2);
057:                d.doFinal(resBuf, 0);
058:
059:                if (!areEqual(lastDigest, resBuf)) {
060:                    fail("failing second clone vector test",
061:                            results[results.length - 1], new String(Hex
062:                                    .encode(resBuf)));
063:                }
064:            }
065:
066:            private byte[] toByteArray(String input) {
067:                byte[] bytes = new byte[input.length()];
068:
069:                for (int i = 0; i != bytes.length; i++) {
070:                    bytes[i] = (byte) input.charAt(i);
071:                }
072:
073:                return bytes;
074:            }
075:
076:            private void vectorTest(Digest digest, int count, byte[] resBuf,
077:                    byte[] input, byte[] expected) {
078:                digest.update(input, 0, input.length);
079:                digest.doFinal(resBuf, 0);
080:
081:                if (!areEqual(resBuf, expected)) {
082:                    System.out.println(new String(Hex.decode(input)));
083:                    fail("Vector " + count + " failed got "
084:                            + new String(Hex.encode(resBuf)));
085:                }
086:            }
087:
088:            protected abstract Digest cloneDigest(Digest digest);
089:
090:            //
091:            // optional tests
092:            //
093:            protected void millionATest(String expected) {
094:                byte[] resBuf = new byte[digest.getDigestSize()];
095:
096:                for (int i = 0; i < 1000000; i++) {
097:                    digest.update((byte) 'a');
098:                }
099:
100:                digest.doFinal(resBuf, 0);
101:
102:                if (!areEqual(resBuf, Hex.decode(expected))) {
103:                    fail("Million a's failed");
104:                }
105:            }
106:
107:            protected void sixtyFourKTest(String expected) {
108:                byte[] resBuf = new byte[digest.getDigestSize()];
109:
110:                for (int i = 0; i < 65536; i++) {
111:                    digest.update((byte) (i & 0xff));
112:                }
113:
114:                digest.doFinal(resBuf, 0);
115:
116:                if (!areEqual(resBuf, Hex.decode(expected))) {
117:                    fail("64k test failed");
118:                }
119:            }
120:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.