[Bedework-commit] rpiutil r194 - trunk/src/edu/rpi/cmt/security

svnadmin at bedework.org svnadmin at bedework.org
Tue Oct 26 15:30:50 EDT 2010


Author: douglm
Date: 2010-10-26 15:30:49 -0400 (Tue, 26 Oct 2010)
New Revision: 194

Added:
   trunk/src/edu/rpi/cmt/security/PwEncryptionDefault.java
   trunk/src/edu/rpi/cmt/security/PwEncryptionIntf.java
Log:
Move en/decryption classes into rpiutil. 

Change configs to match

Added: trunk/src/edu/rpi/cmt/security/PwEncryptionDefault.java
===================================================================
--- trunk/src/edu/rpi/cmt/security/PwEncryptionDefault.java	                        (rev 0)
+++ trunk/src/edu/rpi/cmt/security/PwEncryptionDefault.java	2010-10-26 19:30:49 UTC (rev 194)
@@ -0,0 +1,116 @@
+/* **********************************************************************
+    Copyright 2008 Rensselaer Polytechnic Institute. All worldwide rights reserved.
+
+    Redistribution and use of this distribution in source and binary forms,
+    with or without modification, are permitted provided that:
+       The above copyright notice and this permission notice appear in all
+        copies and supporting documentation;
+
+        The name, identifiers, and trademarks of Rensselaer Polytechnic
+        Institute are not used in advertising or publicity without the
+        express prior written permission of Rensselaer Polytechnic Institute;
+
+    DISCLAIMER: The software is distributed" AS IS" without any express or
+    implied warranty, including but not limited to, any implied warranties
+    of merchantability or fitness for a particular purpose or any warrant)'
+    of non-infringement of any current or pending patent rights. The authors
+    of the software make no representations about the suitability of this
+    software for any particular purpose. The entire risk as to the quality
+    and performance of the software is with the user. Should the software
+    prove defective, the user assumes the cost of all necessary servicing,
+    repair or correction. In particular, neither Rensselaer Polytechnic
+    Institute, nor the authors of the software are liable for any indirect,
+    special, consequential, or incidental damages related to the software,
+    to the maximum extent the law permits.
+*/
+package edu.rpi.cmt.security;
+
+import edu.rpi.cmt.security.pki.PKITools;
+
+import org.apache.log4j.Logger;
+
+/**
+ * @author Mike Douglass
+ */
+public class PwEncryptionDefault implements PwEncryptionIntf {
+  private boolean debug;
+
+  private String privKeys;
+
+  private String pubKeys;
+
+  private PKITools pki;
+
+  private transient Logger log;
+
+  /**
+   * @throws Throwable
+   */
+  public PwEncryptionDefault() throws Throwable {
+    debug = getLog().isDebugEnabled();
+    pki = new PKITools(false /*verbose*/, debug);
+  }
+
+  /**
+   * @param privKeys
+   * @param pubKeys
+   * @throws Throwable
+   */
+  public void init (final String privKeys,
+                    final String pubKeys) throws Throwable {
+    this.privKeys = privKeys;
+    this.pubKeys = pubKeys;
+  }
+
+  public String encrypt(final String val) throws Throwable {
+    int numKeys = pki.countKeys(privKeys);
+
+    if (debug) {
+      debugMsg("Number of keys: " + numKeys);
+    }
+
+    int keyNum = numKeys - 1;
+
+    String etext = pki.encryptWithKeyFile(pubKeys,
+                                          val, keyNum);
+
+    StringBuilder sb = new StringBuilder();
+
+    sb.append(keyNum);
+    sb.append("{");
+    sb.append(etext);
+    sb.append("}");
+
+    return sb.toString();
+  }
+
+  public boolean match(final String plain,
+                       final String encrypted) throws Throwable {
+    return encrypt(plain).equals(encrypted);
+  }
+
+  public String decrypt(final String encrypted) throws Throwable {
+    int pos = encrypted.indexOf("{");
+
+    if ((pos < 0) || (encrypted.lastIndexOf("}") != encrypted.length() - 1)) {
+      throw new Exception(badPwFormat);
+    }
+
+    int keyNum = Integer.valueOf(encrypted.substring(0, pos));
+    return pki.decryptWithKeyFile(privKeys,
+                                  encrypted.substring(pos + 1, encrypted.length() - 1),
+                                  keyNum);
+  }
+
+  private Logger getLog() {
+    if (log == null) {
+      log = Logger.getLogger(this.getClass());
+    }
+
+    return log;
+  }
+
+  private void debugMsg(final String msg) {
+    getLog().debug(msg);
+  }
+}

Added: trunk/src/edu/rpi/cmt/security/PwEncryptionIntf.java
===================================================================
--- trunk/src/edu/rpi/cmt/security/PwEncryptionIntf.java	                        (rev 0)
+++ trunk/src/edu/rpi/cmt/security/PwEncryptionIntf.java	2010-10-26 19:30:49 UTC (rev 194)
@@ -0,0 +1,74 @@
+/* **********************************************************************
+    Copyright 2008 Rensselaer Polytechnic Institute. All worldwide rights reserved.
+
+    Redistribution and use of this distribution in source and binary forms,
+    with or without modification, are permitted provided that:
+       The above copyright notice and this permission notice appear in all
+        copies and supporting documentation;
+
+        The name, identifiers, and trademarks of Rensselaer Polytechnic
+        Institute are not used in advertising or publicity without the
+        express prior written permission of Rensselaer Polytechnic Institute;
+
+    DISCLAIMER: The software is distributed" AS IS" without any express or
+    implied warranty, including but not limited to, any implied warranties
+    of merchantability or fitness for a particular purpose or any warrant)'
+    of non-infringement of any current or pending patent rights. The authors
+    of the software make no representations about the suitability of this
+    software for any particular purpose. The entire risk as to the quality
+    and performance of the software is with the user. Should the software
+    prove defective, the user assumes the cost of all necessary servicing,
+    repair or correction. In particular, neither Rensselaer Polytechnic
+    Institute, nor the authors of the software are liable for any indirect,
+    special, consequential, or incidental damages related to the software,
+    to the maximum extent the law permits.
+*/
+package edu.rpi.cmt.security;
+
+import java.io.Serializable;
+
+/** Interface for classes which encrypt and decrypt passwords
+ *
+ * @author Mike Douglass
+ */
+public interface PwEncryptionIntf extends Serializable {
+  /** Bad stored password format */
+  public static final String badPwFormat =
+      "org.bedework.exception.security.badpwformat";
+
+  /** Call before any other method.
+   *
+   * @param privKeys
+   * @param pubKeys
+   * @throws Throwable
+   */
+  public void init (final String privKeys,
+                    final String pubKeys) throws Throwable;
+
+  /** Encrypt the password and return the result.
+   *
+   * @param val
+   * @return String
+   * @throws Throwable
+   */
+  public String encrypt(String val) throws Throwable;
+
+  /** Match the encrypted password - that is, encrypt the plain text and
+   * compare.
+   *
+   * @param plain
+   * @param encrypted
+   * @return boolean true for a match
+   * @throws Throwable
+   */
+  public boolean match(String plain,
+                       String encrypted) throws Throwable;
+
+  /** Decrypt the value
+   *
+   * @param encrypted
+   * @return String plain text.
+   * @throws Throwable
+   */
+  public String decrypt(String encrypted) throws Throwable;
+}



More information about the Bedework-commit mailing list