sam.cipher
Class Key

java.lang.Object
  |
  +--sam.cipher.Key
Direct Known Subclasses:
Deal.Key, DES.Key, Frog.Key, Loki.Key, Mars.Key, RC6.Key, Rijndael.Key, Serpent.Key, Twofish.Key, WarFactory.Key

public class Key
extends java.lang.Object

General purpose key class.

This class's data is immutable, and so should be subclasses.

You may create a sub-class in the following way:


 class MyKey extends Cipher.Key {
   public MyKey(long[] longs) {
     super( makeKey(longs), 27 );  //
   }
   private static long[] makeKey(long[] longs) {
      long[] key = new long[39];
      ...  // Use 64 bits of 'longs[i]' to generate 27 bits in 'key[j]'
    return key;
   }
   public Key inverse() { return new MyKey(...); }
 }
 


Nested Class Summary
static class Key.Mode
          Parsing mode for converting characters to significant bits.
 
Field Summary
protected  boolean[] asBooleans
           
protected  byte[] asBytes
           
static Key.Mode ASCII
          One char issues 8 bits
protected  int[] asInts
           
protected  long[] asLongs
           
protected  short[] asShorts
           
static Key.Mode BINARY
          One char (0,1) issues 1 bit
protected  long[] bits
          Actual bits.
protected  int bitsPerLong
          Number of significant bits per long of bits.
 boolean direction
          Reports wether this is an encryption- or a decryption-key.
static sam.cipher.Key EMPTY
          One char issues no single bit
static Key.Mode HEXA
          One char (0,1,..9,A,..F) issues 4 bits
static Key.Mode IGNORE
          One char issues no bit at all
protected  int mask
          Mask for keeping only the significant bits of a long.
static Key.Mode OCTAL
          One char (0,1,..,7) issues 3 bits
static Key.Mode PLAIN
          One char issues 6 bits.
static Key.Mode UNICODE
          One char issues 16 bits
 
Constructor Summary
protected Key(boolean[] bits)
          Builds a new key.
  Key(long[] bits)
          Builds a new key.
protected Key(long[] bits, int bitsPerLong)
          Builds a new key.
protected Key(long[] bits, int bitsPerLong, boolean direction)
          Builds a new key.
  Key(java.lang.String password, Key.Mode mode, int bitsPerLong)
          Builds a new key.
 
Method Summary
 boolean equals(java.lang.Object object)
          General-purpose.
 byte[] expandToBytes()
           
 int[] expandToInts()
           
 long[] expandToLongs()
           
 short[] expandToShorts()
           
 boolean[] get1()
           
 short[] get16()
           
 int[] get32()
           
 long[] get64()
           
 byte[] get8()
           
 boolean[] getBits()
           
 boolean[] getBooleans()
           
 byte[] getBytes()
           
 int[] getInts()
           
 long[] getLongs()
           
 short[] getShorts()
           
 sam.cipher.Key inverse()
          Inverses this key.
 boolean isEmpty()
          Wether this key is empty
protected static long[] parse(java.lang.String password, Key.Mode mode, int bitsPerLong)
          Parses a String passphrase to a long[] array key material.
protected static byte[] toBytes(long[] longs)
          Converts an array of longs to an array of bytes, without any loss of data.
protected static long[] toLongs(boolean[] booleans)
          Converts an array of booleans to an array of longs, without any loss of data.
protected static long[] toLongs(byte[] bytes)
          Converts an array of bytes to an array of longs, without any loss of data.
protected static long[] toLongs(char[] chars)
          Converts an array of chars to an array of longs, without any loss of data.
protected static long[] toLongs(int[] ints)
          Converts an array of ints to an array of longs, without any loss of data.
protected static long[] toLongs(short[] shorts)
          Converts an array of shorts to an array of longs, without any loss of data.
 java.lang.String toPassword()
          Unparses a key to a passphrase.
 java.lang.String toPassword(Key.Mode mode)
          Unparses a key to a passphrase.
 java.lang.String toString()
          Gets a human-readable description of this key.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

EMPTY

public static final sam.cipher.Key EMPTY
One char issues no single bit


UNICODE

public static final Key.Mode UNICODE
One char issues 16 bits


ASCII

public static final Key.Mode ASCII
One char issues 8 bits


PLAIN

public static final Key.Mode PLAIN
One char issues 6 bits.

Default mode for keyboard-entered passworsd.


BINARY

public static final Key.Mode BINARY
One char (0,1) issues 1 bit


OCTAL

public static final Key.Mode OCTAL
One char (0,1,..,7) issues 3 bits


HEXA

public static final Key.Mode HEXA
One char (0,1,..9,A,..F) issues 4 bits


IGNORE

public static final Key.Mode IGNORE
One char issues no bit at all


direction

public final boolean direction
Reports wether this is an encryption- or a decryption-key.


bits

protected final long[] bits
Actual bits.


bitsPerLong

protected final int bitsPerLong
Number of significant bits per long of bits.


mask

protected final int mask
Mask for keeping only the significant bits of a long.


asBooleans

protected final boolean[] asBooleans

asBytes

protected final byte[] asBytes

asShorts

protected final short[] asShorts

asInts

protected final int[] asInts

asLongs

protected final long[] asLongs
Constructor Detail

Key

public Key(java.lang.String password,
           Key.Mode mode,
           int bitsPerLong)
Builds a new key.

Parameters:
password - the plain text passphrase
mode - parsing mode
bitsPerLong - desired number of bits to be stored per int (max 32).
See Also:
parse

Key

protected Key(boolean[] bits)
Builds a new key.

Parameters:
bits - the boolean[] array of bits composing the key material

Key

public Key(long[] bits)
Builds a new key.

Parameters:
bits - the long[] array of 64-bit groups composing the key material Note that this constructor must be public as sam.cipher.Key might be the default key used.

Key

protected Key(long[] bits,
              int bitsPerLong)
Builds a new key.

Parameters:
bits - the long[] array of bit groups composing the key material
bitsPerLong - the number of significant bits per 64-bits long to be used

Key

protected Key(long[] bits,
              int bitsPerLong,
              boolean direction)
Builds a new key.

Parameters:
bits - the long[] array of bit groups composing the key material
bitsPerLong - the number of significant bits per 64-bits long to be used
direction - either Cipher.ENCRYPT or Cipher.DECRYPT
Method Detail

isEmpty

public boolean isEmpty()
Wether this key is empty


inverse

public sam.cipher.Key inverse()
Inverses this key. This turns a ciphering key into a de-ciphering one, and vice-versa. To be overriden as required.

This implementation simply returns this.


toString

public java.lang.String toString()
Gets a human-readable description of this key. Warning: this methods returns the actual key material

Overrides:
toString in class java.lang.Object
Returns:
description String of the form "Key(8|0xff)[0011011000101]".
See Also:
toPassword(sam.cipher.Key.Mode)

toPassword

public java.lang.String toPassword(Key.Mode mode)
Unparses a key to a passphrase. Warning: this methods returns the actual key material

Parameters:
mode - the parsing mode.
Returns:
the password.

toPassword

public java.lang.String toPassword()
Unparses a key to a passphrase. The default unparsing mode is PLAIN Warning: this methods returns the actual key material

Returns:
the password.

get1

public boolean[] get1()
See Also:
getBooleans()

getBits

public boolean[] getBits()
See Also:
getBooleans()

getBooleans

public boolean[] getBooleans()
Returns:
all significant bits repacked in a one-bit boolean[] array.

get8

public byte[] get8()
See Also:
getBytes()

getBytes

public byte[] getBytes()
Returns:
all significant bits repacked in a 8-bits byte[] array.

expandToBytes

public byte[] expandToBytes()

get16

public short[] get16()
See Also:
getShorts()

getShorts

public short[] getShorts()
Returns:
all significant bits repacked in a 16 bits short[] array.

expandToShorts

public short[] expandToShorts()

get32

public int[] get32()
See Also:
getInts()

getInts

public int[] getInts()
Returns:
all significant bits repacked in a 32 bits int[] array.

expandToInts

public int[] expandToInts()

get64

public long[] get64()
See Also:
getLongs()

getLongs

public long[] getLongs()
Returns:
all significant bits repacked in a 64 bits long[] array.

expandToLongs

public long[] expandToLongs()
Returns:
all significant bits repacked in a 64 bits long[] array.

equals

public boolean equals(java.lang.Object object)
General-purpose.

Required to be overriden by subclasses only if specific instance data are declared.

Overrides:
equals in class java.lang.Object

parse

protected static long[] parse(java.lang.String password,
                              Key.Mode mode,
                              int bitsPerLong)
Parses a String passphrase to a long[] array key material.

Parameters:
password - the password to be parsed
mode - the parsing mode
bitsPerLong - the desired number of significant bits to be stored in each resulting long.

Note that the actual number of significant bits of the password is part of the mode parameter, the resulting array is thus sized accordingly to the bitsPerLong parameter.


toBytes

protected static byte[] toBytes(long[] longs)
Converts an array of longs to an array of bytes, without any loss of data.

Parameters:
longs - the source array of 64-bits full longs.
Returns:
an four times larger array of 8-bits full bytes.

toLongs

protected static long[] toLongs(boolean[] booleans)
Converts an array of booleans to an array of longs, without any loss of data.

Parameters:
booleans - the source array of 1-bit full booleans.
Returns:
an 64 times smaller array of 64-bits full longs.

toLongs

protected static long[] toLongs(byte[] bytes)
Converts an array of bytes to an array of longs, without any loss of data.

Parameters:
bytes - the source array of 8-bits full bytes.
Returns:
an 8 times smaller array of 64-bits full longs.

toLongs

protected static long[] toLongs(short[] shorts)
Converts an array of shorts to an array of longs, without any loss of data.

Parameters:
shorts - the source array of 16-bits full shorts.
Returns:
an 4 times smaller array of 64-bits full longs.

toLongs

protected static long[] toLongs(char[] chars)
Converts an array of chars to an array of longs, without any loss of data.

Returns:
an 4 times smaller array of 64-bits full longs.

toLongs

protected static long[] toLongs(int[] ints)
Converts an array of ints to an array of longs, without any loss of data.

Parameters:
ints - the source array of 32-bits full ints.
Returns:
an twice smaller array of 64-bits full longs.