abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r474614 - in /incubator/abdera/java/trunk/examples/src/main: java/org/apache/abdera/examples/security/ resources/
Date Tue, 14 Nov 2006 00:55:20 GMT
Author: jmsnell
Date: Mon Nov 13 16:55:19 2006
New Revision: 474614

URL: http://svn.apache.org/viewvc?view=rev&rev=474614
Log:
Add simple security examples

Added:
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/security/
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/security/DSig.java
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/security/Enc.java
    incubator/abdera/java/trunk/examples/src/main/resources/key.jks   (with props)
    incubator/abdera/java/trunk/examples/src/main/resources/log4j.properties

Added: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/security/DSig.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/security/DSig.java?view=auto&rev=474614
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/security/DSig.java
(added)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/security/DSig.java
Mon Nov 13 16:55:19 2006
@@ -0,0 +1,95 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  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.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.examples.security;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.security.KeyStore;
+import java.security.PrivateKey;
+import java.security.cert.X509Certificate;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.factory.Factory;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.security.AbderaSecurity;
+import org.apache.abdera.security.Signature;
+import org.apache.abdera.security.SignatureOptions;
+
+public class DSig {
+
+  private static final String keystoreFile = "/key.jks";
+  private static final String keystoreType = "JKS";
+  private static final String keystorePass = "testing";
+  private static final String privateKeyAlias = "James";
+  private static final String privateKeyPass = "testing";
+  private static final String certificateAlias = "James";
+  
+  public static void main(String[] args) throws Exception {
+    
+    // Initialize the keystore
+    KeyStore ks = KeyStore.getInstance(keystoreType);
+    InputStream in = DSig.class.getResourceAsStream(keystoreFile);    
+    ks.load(in, keystorePass.toCharArray());
+    PrivateKey signingKey = 
+      (PrivateKey) ks.getKey(
+        privateKeyAlias,
+        privateKeyPass.toCharArray());
+    X509Certificate cert = 
+      (X509Certificate) ks.getCertificate(
+        certificateAlias);
+    
+    // Create the entry to sign
+    Abdera abdera = new Abdera();
+    AbderaSecurity absec = new AbderaSecurity(abdera);
+    Factory factory = abdera.getFactory();
+    
+    Entry entry = factory.newEntry();
+    entry.setId("http://example.org/foo/entry");  
+    entry.setUpdated(new java.util.Date());
+    entry.setTitle("This is an entry");
+    entry.setContentAsXhtml("This <b>is</b> <i>markup</i>");
+    entry.addAuthor("James");
+    entry.addLink("http://www.example.org");
+    
+    // Prepare the digital signature options
+    Signature sig = absec.getSignature();
+    SignatureOptions options = sig.getDefaultSignatureOptions();    
+    options.setCertificate(cert);
+    options.setSigningKey(signingKey);  
+
+    // Sign the entry
+    entry = sig.sign(entry, options);      
+    
+    // Check the round trip
+    ByteArrayOutputStream out = new ByteArrayOutputStream();
+    entry.writeTo(out); // do not use the pretty writer, it will break the signature
+    ByteArrayInputStream bais = new ByteArrayInputStream(out.toByteArray());
+    Document<Entry> entry_doc = abdera.getParser().parse(bais);
+    entry = entry_doc.getRoot();
+    
+    System.out.println("Valid? " + sig.verify(entry,null));
+    
+    entry.setTitle("Change the title");
+    
+    System.out.println("Valid after changing the title? " + sig.verify(entry,null));
+    
+  }
+  
+}

Added: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/security/Enc.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/security/Enc.java?view=auto&rev=474614
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/security/Enc.java
(added)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/security/Enc.java
Mon Nov 13 16:55:19 2006
@@ -0,0 +1,86 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  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.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.examples.security;
+
+import java.security.Security;
+
+import javax.crypto.KeyGenerator;
+import javax.crypto.SecretKey;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.factory.Factory;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.security.AbderaSecurity;
+import org.apache.abdera.security.Encryption;
+import org.apache.abdera.security.EncryptionOptions;
+import org.apache.abdera.test.security.EncryptionTest;
+
+public class Enc {
+
+  @SuppressWarnings("unchecked")
+  public static void main(String[] args) throws Exception {
+    
+    // Prepare the crypto provider
+    try {
+      Class.forName("org.bouncycastle.LICENSE");
+      Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
+    } catch (Exception e) {
+      EncryptionTest.fail("The Bouncy Castle JCE Provider is not available");
+    }
+    
+    // Generate Encryption Key
+    String jceAlgorithmName = "AES";
+    KeyGenerator keyGenerator =
+        KeyGenerator.getInstance(jceAlgorithmName);
+    keyGenerator.init(128);
+    SecretKey key = keyGenerator.generateKey();
+
+    // Create the entry to encrypt
+    Abdera abdera = new Abdera();
+    AbderaSecurity absec = new AbderaSecurity(abdera);
+    Factory factory = abdera.getFactory();
+    
+    Entry entry = factory.newEntry();
+    entry.setId("http://example.org/foo/entry");
+    entry.setUpdated(new java.util.Date());
+    entry.setTitle("This is an entry");
+    entry.setContentAsXhtml("This <b>is</b> <i>markup</i>");
+    entry.addAuthor("James");
+    entry.addLink("http://www.example.org");
+
+    // Prepare the encryption options
+    Encryption enc = absec.getEncryption();
+    EncryptionOptions options = enc.getDefaultEncryptionOptions();
+    options.setDataEncryptionKey(key);
+    
+    // Encrypt the document using the generated key
+    Document enc_doc = enc.encrypt(entry.getDocument(), options);
+
+    enc_doc.writeTo(System.out);
+    
+    System.out.println("\n\n");
+    
+    // Decrypt the document using the generated key
+    Document<Entry> entry_doc = enc.decrypt(enc_doc, options);
+
+    entry_doc.writeTo(System.out);
+    
+  }
+  
+}

Added: incubator/abdera/java/trunk/examples/src/main/resources/key.jks
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/resources/key.jks?view=auto&rev=474614
==============================================================================
Binary file - no diff available.

Propchange: incubator/abdera/java/trunk/examples/src/main/resources/key.jks
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/abdera/java/trunk/examples/src/main/resources/log4j.properties
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/resources/log4j.properties?view=auto&rev=474614
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/resources/log4j.properties (added)
+++ incubator/abdera/java/trunk/examples/src/main/resources/log4j.properties Mon Nov 13 16:55:19
2006
@@ -0,0 +1,5 @@
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.Target=System.err
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%c{1}.%M: %m%n
+log4j.rootLogger=warn, stdout
\ No newline at end of file



Mime
View raw message