Source Code Cross Referenced for DSAKeyFactoryImpl.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » security » provider » crypto » 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 » Apache Harmony Java SE » org package » org.apache.harmony.security.provider.crypto 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package org.apache.harmony.security.provider.crypto;
019:
020:        import java.math.BigInteger;
021:        import java.security.InvalidKeyException;
022:        import java.security.Key;
023:        import java.security.KeyFactorySpi;
024:        import java.security.PrivateKey;
025:        import java.security.PublicKey;
026:        import java.security.interfaces.DSAParams;
027:        import java.security.interfaces.DSAPrivateKey;
028:        import java.security.interfaces.DSAPublicKey;
029:        import java.security.spec.DSAPrivateKeySpec;
030:        import java.security.spec.DSAPublicKeySpec;
031:        import java.security.spec.InvalidKeySpecException;
032:        import java.security.spec.KeySpec;
033:        import java.security.spec.PKCS8EncodedKeySpec;
034:        import java.security.spec.X509EncodedKeySpec;
035:
036:        import org.apache.harmony.security.internal.nls.Messages;
037:
038:        public class DSAKeyFactoryImpl extends KeyFactorySpi {
039:
040:            /**
041:             * This method generates a DSAPrivateKey object from the provided key specification. 
042:             *
043:             * @param
044:             *    keySpec - the specification (key material) for the DSAPrivateKey.
045:             *
046:             * @return
047:             *    a DSAPrivateKey object
048:             *
049:             * @throws InvalidKeySpecException
050:             *     if "keySpec" is neither DSAPrivateKeySpec nor PKCS8EncodedKeySpec
051:             */
052:            protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
053:                    throws InvalidKeySpecException {
054:
055:                if (keySpec != null) {
056:                    if (keySpec instanceof  DSAPrivateKeySpec) {
057:
058:                        return new DSAPrivateKeyImpl(
059:                                (DSAPrivateKeySpec) keySpec);
060:                    }
061:                    if (keySpec instanceof  PKCS8EncodedKeySpec) {
062:
063:                        return new DSAPrivateKeyImpl(
064:                                (PKCS8EncodedKeySpec) keySpec);
065:                    }
066:                }
067:                throw new InvalidKeySpecException(Messages
068:                        .getString("security.19C")); //$NON-NLS-1$
069:            }
070:
071:            /**
072:             * This method generates a DSAPublicKey object from the provided key specification. 
073:             *
074:             * @param
075:             *    keySpec - the specification (key material) for the DSAPublicKey.
076:             *
077:             * @return
078:             *    a DSAPublicKey object
079:             *
080:             * @throws InvalidKeySpecException
081:             *     if "keySpec" is neither DSAPublicKeySpec nor X509EncodedKeySpec
082:             */
083:            protected PublicKey engineGeneratePublic(KeySpec keySpec)
084:                    throws InvalidKeySpecException {
085:
086:                if (keySpec != null) {
087:                    if (keySpec instanceof  DSAPublicKeySpec) {
088:
089:                        return new DSAPublicKeyImpl((DSAPublicKeySpec) keySpec);
090:                    }
091:                    if (keySpec instanceof  X509EncodedKeySpec) {
092:
093:                        return new DSAPublicKeyImpl(
094:                                (X509EncodedKeySpec) keySpec);
095:                    }
096:                }
097:                throw new InvalidKeySpecException(Messages
098:                        .getString("security.19D")); //$NON-NLS-1$
099:            }
100:
101:            /**
102:             * This method returns a specification for the supplied key.
103:             * 
104:             * The specification will be returned in the form of an object of the type
105:             * specified by keySpec.
106:             * 
107:             * @param key -
108:             *            either DSAPrivateKey or DSAPublicKey
109:             * @param keySpec -
110:             *            either DSAPrivateKeySpec.class or DSAPublicKeySpec.class
111:             * 
112:             * @return either a DSAPrivateKeySpec or a DSAPublicKeySpec
113:             * 
114:             * @throws InvalidKeySpecException
115:             *             if "keySpec" is not a specification for DSAPublicKey or
116:             *             DSAPrivateKey
117:             */
118:            protected <T extends KeySpec> T engineGetKeySpec(Key key,
119:                    Class<T> keySpec) throws InvalidKeySpecException {
120:
121:                BigInteger p, q, g, x, y;
122:
123:                if (key != null) {
124:                    if (keySpec == null) {
125:                        throw new NullPointerException(Messages
126:                                .getString("security.19E")); //$NON-NLS-1$
127:                    }
128:                    if (key instanceof  DSAPrivateKey) {
129:                        DSAPrivateKey privateKey = (DSAPrivateKey) key;
130:
131:                        if (keySpec.equals(DSAPrivateKeySpec.class)) {
132:
133:                            x = privateKey.getX();
134:
135:                            DSAParams params = privateKey.getParams();
136:
137:                            p = params.getP();
138:                            q = params.getQ();
139:                            g = params.getG();
140:
141:                            return (T) (new DSAPrivateKeySpec(x, p, q, g));
142:                        }
143:
144:                        if (keySpec.equals(PKCS8EncodedKeySpec.class)) {
145:                            return (T) (new PKCS8EncodedKeySpec(key
146:                                    .getEncoded()));
147:                        }
148:
149:                        throw new InvalidKeySpecException(Messages
150:                                .getString("security.19C")); //$NON-NLS-1$
151:                    }
152:
153:                    if (key instanceof  DSAPublicKey) {
154:                        DSAPublicKey publicKey = (DSAPublicKey) key;
155:
156:                        if (keySpec.equals(DSAPublicKeySpec.class)) {
157:
158:                            y = publicKey.getY();
159:
160:                            DSAParams params = publicKey.getParams();
161:
162:                            p = params.getP();
163:                            q = params.getQ();
164:                            g = params.getG();
165:
166:                            return (T) (new DSAPublicKeySpec(y, p, q, g));
167:                        }
168:
169:                        if (keySpec.equals(X509EncodedKeySpec.class)) {
170:                            return (T) (new X509EncodedKeySpec(key.getEncoded()));
171:                        }
172:
173:                        throw new InvalidKeySpecException(Messages
174:                                .getString("security.19D")); //$NON-NLS-1$
175:                    }
176:                }
177:                throw new InvalidKeySpecException(Messages
178:                        .getString("security.19F")); //$NON-NLS-1$
179:            }
180:
181:            /**
182:             * The method generates a DSAPublicKey object from the provided key. 
183:             *
184:             * @param
185:             *    key - a DSAPublicKey object or DSAPrivateKey object.
186:             *
187:             * @return
188:             *    object of the same type as the "key" argument
189:             *
190:             * @throws InvalidKeyException
191:             *     if "key" is neither DSAPublicKey nor DSAPrivateKey
192:             */
193:            protected Key engineTranslateKey(Key key)
194:                    throws InvalidKeyException {
195:
196:                if (key != null) {
197:                    if (key instanceof  DSAPrivateKey) {
198:
199:                        DSAPrivateKey privateKey = (DSAPrivateKey) key;
200:                        DSAParams params = privateKey.getParams();
201:
202:                        try {
203:                            return engineGeneratePrivate(new DSAPrivateKeySpec(
204:                                    privateKey.getX(), params.getP(), params
205:                                            .getQ(), params.getG()));
206:                        } catch (InvalidKeySpecException e) {
207:                            // Actually this exception shouldn't be thrown
208:                            throw new InvalidKeyException(Messages.getString(
209:                                    "security.1A0", e)); //$NON-NLS-1$
210:                        }
211:                    }
212:
213:                    if (key instanceof  DSAPublicKey) {
214:
215:                        DSAPublicKey publicKey = (DSAPublicKey) key;
216:                        DSAParams params = publicKey.getParams();
217:
218:                        try {
219:                            return engineGeneratePublic(new DSAPublicKeySpec(
220:                                    publicKey.getY(), params.getP(), params
221:                                            .getQ(), params.getG()));
222:                        } catch (InvalidKeySpecException e) {
223:                            // Actually this exception shouldn't be thrown
224:                            throw new InvalidKeyException(Messages.getString(
225:                                    "security.1A1", e)); //$NON-NLS-1$
226:                        }
227:                    }
228:                }
229:                throw new InvalidKeyException(Messages
230:                        .getString("security.19F")); //$NON-NLS-1$
231:            }
232:
233:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.