sam.cipher
Class ReflexiveFactory

java.lang.Object
  |
  +--sam.cipher.Factory
        |
        +--sam.cipher.ReflexiveFactory

public class ReflexiveFactory
extends Factory

Implementation of a Cipher factory based on reflection. Registers a new Cipher class. Any specialized Key class associated with the cipher must be declared innerly with a long[] constructor.

To be called typically in a static initializer of a subclass, as in:


            public class MyCipher extends Cipher {
                static {
                          Cipher.register( MyCipher.class, "My cipher", "This is my private key cipher" );  // May throw a Cipher.Error
            }
 
           ...
 
                   public class Key extends Cipher.Key {
                       public Key(long[] bits) {
                           super(bits);
                    ...
                    }
            }
            }
        
or alternatively

            public class MyCipher extends Cipher {
                static {
                          Cipher.register( new ReflexiveFactory("Mycipher", "This is my own cipher class") {
                              public int getPreferredKeySize(int suggestedSize) {
                                      return highestMultiple(suggestedSize, 8);  // My cipher requires key lengths be multiple of 8 bits
                                  }
                          } );  // May throw a Cipher.Error
            }
           ...
            }
        


Field Summary
 
Fields inherited from class sam.cipher.Factory
description, name
 
Constructor Summary
ReflexiveFactory(java.lang.Class cipher, java.lang.String name, java.lang.String description)
          Makes a new registration entry for a cipher.
ReflexiveFactory(java.lang.String description)
          Builds a new reflexive Cipher factory based on the supposedly existing outer class.
ReflexiveFactory(java.lang.String name, java.lang.String description)
          Builds a new reflexive Cipher factory based on the supposedly existing outer class.
 
Method Summary
protected static java.lang.String getBestDescription(java.lang.String description, java.lang.Class clazz)
          Gets the best name for specified cipher class.
protected static java.lang.String getBestName(java.lang.String name, java.lang.Class clazz)
          Gets the best name for specified cipher class.
protected  sam.cipher.Cipher make(long[] keyBits, boolean mode)
          Builds a new cipher instance through constructors found by introspection.
 
Methods inherited from class sam.cipher.Factory
getPreferredKeySize, highestIn, highestMultiple, ranged, resizeKey, resizeKeyToPreferredSize
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflexiveFactory

public ReflexiveFactory(java.lang.String description)
                 throws Cipher.Error
Builds a new reflexive Cipher factory based on the supposedly existing outer class. Typical use would be to declare a static inner subclass of ReflexiveFactory inside your Cipher subclass.

Parameters:
description - a human-readable description of this cipher implementation Note that the registration name is automatically inferred from the ciphering class name. Warning using automatic naming based on class name can cause registration name collision ! Warning: this is not a ReflexiveFactory(String className) constructor !

ReflexiveFactory

public ReflexiveFactory(java.lang.String name,
                        java.lang.String description)
                 throws Cipher.Error
Builds a new reflexive Cipher factory based on the supposedly existing outer class. Typical use would be to declare a static inner subclass of ReflexiveFactory inside your Cipher subclass.

Parameters:
name - the registration name for this cipher implementation.
description - a human-readable description of this cipher implementation

ReflexiveFactory

public ReflexiveFactory(java.lang.Class cipher,
                        java.lang.String name,
                        java.lang.String description)
                 throws Cipher.Error
Makes a new registration entry for a cipher.

The main cipher constructor public Cipher(Cipher.Key) is obtained by reflection.

The public Key(long[]) constructor is obtained by reflection, either from a declared inner-class of the cipher class, or from the default Cipher.Key class.

Parameters:
cipher - the cipher class, or null if to be retrieved automatically through reflection.
name - the registration name of the cipher, or null if default name.
description - the description to be associated with the cipher, or null if none.
Throws:
Cipher.Error - in case of security or instantiation exceptions
See Also:
SecurityManager
Method Detail

make

protected sam.cipher.Cipher make(long[] keyBits,
                                 boolean mode)
                          throws Cipher.Error
Builds a new cipher instance through constructors found by introspection.

Specified by:
make in class Factory
Parameters:
keyBits - the key as full longs, its length having been retrofitted by the getPreferredKeySize method.
mode - true if encrypting, false otherwise (decrypting).
Cipher.Error
See Also:
Cipher.ENCRYPT, Cipher.DECRYPT

getBestName

protected static java.lang.String getBestName(java.lang.String name,
                                              java.lang.Class clazz)
Gets the best name for specified cipher class. This is the supplied name if it is neither null nor equal to ""; otherwise the last part of the class name is used, which includes dot-separated outer-classes but excludes package specification. Warning using automatic naming based on class name can cause registration name collision !


getBestDescription

protected static java.lang.String getBestDescription(java.lang.String description,
                                                     java.lang.Class clazz)
Gets the best name for specified cipher class. This is the supplied name if it is neither null nor equal to ""; otherwise a default string is used stating that ... no information is available.