You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by pl...@apache.org on 2015/11/04 09:25:54 UTC

[28/48] directory-kerby git commit: DIRKRB-433 Load the private key and public key from file.

DIRKRB-433 Load the private key and public key from file.


Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/91f6e716
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/91f6e716
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/91f6e716

Branch: refs/heads/pkinit-support
Commit: 91f6e716c43f4df1c8cafe637ae5340ace47bb8c
Parents: a180614
Author: plusplusjiajia <ji...@intel.com>
Authored: Tue Oct 13 16:23:40 2015 +0800
Committer: plusplusjiajia <ji...@intel.com>
Committed: Tue Oct 13 16:23:40 2015 +0800

----------------------------------------------------------------------
 .../kerberos/kerb/common/PrivateKeyReader.java  | 73 +++++++++++++++++++
 .../kerberos/kerb/common/PublicKeyReader.java   | 74 ++++++++++++++++++++
 2 files changed, 147 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/91f6e716/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/PrivateKeyReader.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/PrivateKeyReader.java b/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/PrivateKeyReader.java
new file mode 100644
index 0000000..98d1f9d
--- /dev/null
+++ b/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/PrivateKeyReader.java
@@ -0,0 +1,73 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.common;
+
+import org.apache.kerby.util.Base64;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.security.KeyFactory;
+import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.PKCS8EncodedKeySpec;
+
+public class PrivateKeyReader {
+
+    public static PrivateKey loadPrivateKey(InputStream in) throws Exception {
+        try {
+            BufferedReader br = new BufferedReader(new InputStreamReader(in));
+            String readLine = null;
+            StringBuilder sb = new StringBuilder();
+            while ((readLine = br.readLine()) != null) {
+                if (readLine.charAt(0) == '-') {
+                    continue;
+                } else {
+                    sb.append(readLine);
+                    sb.append('\r');
+                }
+            }
+            return loadPrivateKey(sb.toString());
+        } catch (IOException e) {
+            throw e;
+        } catch (NullPointerException e) {
+            throw e;
+        }
+    }
+
+    public static PrivateKey loadPrivateKey(String privateKeyStr) throws Exception {
+        try {
+            Base64 base64 = new Base64();
+            byte[] buffer = base64.decode(privateKeyStr);
+            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
+            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
+            return keyFactory.generatePrivate(keySpec);
+        } catch (NoSuchAlgorithmException e) {
+            throw e;
+        } catch (InvalidKeySpecException e) {
+            throw e;
+        } catch (NullPointerException e) {
+            throw e;
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/91f6e716/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/PublicKeyReader.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/PublicKeyReader.java b/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/PublicKeyReader.java
new file mode 100644
index 0000000..ed54746
--- /dev/null
+++ b/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/PublicKeyReader.java
@@ -0,0 +1,74 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.common;
+
+import org.apache.kerby.util.Base64;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.security.KeyFactory;
+import java.security.NoSuchAlgorithmException;
+import java.security.PublicKey;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.X509EncodedKeySpec;
+
+public class PublicKeyReader {
+
+    public static PublicKey loadPublicKey(InputStream in) throws Exception {
+        try {
+            BufferedReader br = new BufferedReader(new InputStreamReader(in));
+            String readLine = null;
+            StringBuilder sb = new StringBuilder();
+            while ((readLine = br.readLine()) != null) {
+                if (readLine.charAt(0) == '-') {
+                    continue;
+                } else {
+                    sb.append(readLine);
+                    sb.append('\r');
+                }
+            }
+            return loadPublicKey(sb.toString());
+        } catch (IOException e) {
+            throw e;
+        } catch (NullPointerException e) {
+            throw e;
+        }
+    }
+
+
+    public static PublicKey loadPublicKey(String publicKeyStr) throws Exception {
+        try {
+            Base64 base64 = new Base64();
+            byte[] buffer = base64.decode(publicKeyStr);
+            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
+            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
+            return keyFactory.generatePublic(keySpec);
+        } catch (NoSuchAlgorithmException e) {
+            throw e;
+        } catch (InvalidKeySpecException e) {
+            throw e;
+        } catch (NullPointerException e) {
+            throw e;
+        }
+    }
+
+}