sam.cipher
Class Factory

java.lang.Object
  |
  +--sam.cipher.Factory
Direct Known Subclasses:
ReflexiveFactory, WarFactory

public abstract class Factory
extends java.lang.Object

General-purpose Cipher factory. This is an abstraction for instantiating various ciphering algorithm and parsing keys.


Field Summary
 java.lang.String description
          A human-readable description of this cipher.
 java.lang.String name
          The code name of this cipher
 
Constructor Summary
protected Factory(java.lang.String name, java.lang.String description)
          Builds a new Cipher factory
 
Method Summary
protected  int getPreferredKeySize(int suggestedKeySize)
          Specifies preferred key size, in bits.
protected static int highestIn(int suggested, int[] authorized)
          Gets the highest integer less than or equal to suggested from exhaustive list of valid values, or minimum of those if such a number does not exist.
protected static int highestMultiple(int number, int root)
          Gets the highest integer less than or equal to number that is a multiple of root, and that is higher than root.
protected abstract  sam.cipher.Cipher make(long[] key, boolean mode)
          Instantiates a new Cipher implementation.
protected static int ranged(int suggested, int min, int max)
          Ensures a size fits in specified range.
protected static boolean[] resizeKey(boolean[] key, int keySize)
          Resizes key to specified length.
protected  boolean[] resizeKeyToPreferredSize(boolean[] key)
          Resizes key to its desired length.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

name

public final java.lang.String name
The code name of this cipher


description

public final java.lang.String description
A human-readable description of this cipher. This should include any copyright notice.

Constructor Detail

Factory

protected Factory(java.lang.String name,
                  java.lang.String description)
Builds a new Cipher factory

Parameters:
name - the code name of this cipher
description - a human-readable description of this cipher.
Method Detail

make

protected abstract sam.cipher.Cipher make(long[] key,
                                          boolean mode)
                                   throws java.lang.Error
Instantiates a new Cipher implementation.

Parameters:
key - the key as full longs, its length having been retrofitted by the getPreferredKeySize method.
mode - true if encrypting, false otherwise (decrypting).
Throws:
java.lang.Error - in case of impossible instantiation
See Also:
Cipher.ENCRYPT, Cipher.DECRYPT

getPreferredKeySize

protected int getPreferredKeySize(int suggestedKeySize)
Specifies preferred key size, in bits. This shall be overridden to match legal key sizes of specific algorithms.

Parameters:
suggestedKeySize - the suggested key size as computed from the password length
Returns:
a key size suited to this cipher implementation
See Also:
Key, highestMultiple(int, int)

highestMultiple

protected static int highestMultiple(int number,
                                     int root)
Gets the highest integer less than or equal to number that is a multiple of root, and that is higher than root. Simply root if no such integer.


highestIn

protected static int highestIn(int suggested,
                               int[] authorized)
                        throws java.lang.Error
Gets the highest integer less than or equal to suggested from exhaustive list of valid values, or minimum of those if such a number does not exist.

java.lang.Error

ranged

protected static int ranged(int suggested,
                            int min,
                            int max)
                     throws java.lang.Error
Ensures a size fits in specified range.

Returns:
suggested if it fits in specified interval, or bound otherwise.
Throws:
java.lang.Error - if max is less than min.

resizeKeyToPreferredSize

protected boolean[] resizeKeyToPreferredSize(boolean[] key)
                                      throws java.lang.Error
Resizes key to its desired length.

java.lang.Error
See Also:
getPreferredKeySize(int)

resizeKey

protected static boolean[] resizeKey(boolean[] key,
                                     int keySize)
                              throws java.lang.Error
Resizes key to specified length. If the current size is larger, the key is truncated. If it is smaller, the key is concatenated to itself until it is large enough.

Parameters:
keySize - the desired new key size
Throws:
java.lang.Error - if this key is empty and expected size is not