Source for gnu.java.security.key.rsa.GnuRSAKey

   1: /* GnuRSAKey.java -- 
   2:    Copyright 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2 of the License, or (at
   9: your option) any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; if not, write to the Free Software
  18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  19: USA
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version.  */
  37: 
  38: 
  39: package gnu.java.security.key.rsa;
  40: 
  41: import gnu.java.security.Registry;
  42: import gnu.java.security.action.GetPropertyAction;
  43: import gnu.java.security.util.FormatUtil;
  44: 
  45: import java.math.BigInteger;
  46: import java.security.AccessController;
  47: import java.security.Key;
  48: import java.security.interfaces.RSAKey;
  49: 
  50: /**
  51:  * A base asbtract class for both public and private RSA keys.
  52:  */
  53: public abstract class GnuRSAKey
  54:     implements Key, RSAKey
  55: {
  56:   /** The public modulus of an RSA key pair. */
  57:   private final BigInteger n;
  58: 
  59:   /** The public exponent of an RSA key pair. */
  60:   private final BigInteger e;
  61: 
  62:   /**
  63:    * Identifier of the default encoding format to use when externalizing the key
  64:    * material.
  65:    */
  66:   protected final int defaultFormat;
  67: 
  68:   /** String representation of this key. Cached for speed. */
  69:   private transient String str;
  70: 
  71:   /**
  72:    * Trivial protected constructor.
  73:    * 
  74:    * @param defaultFormat the identifier of the encoding format to use by
  75:    *          default when externalizing the key.
  76:    * @param n the public modulus <code>n</code>.
  77:    * @param e the public exponent <code>e</code>.
  78:    */
  79:   protected GnuRSAKey(int defaultFormat, BigInteger n, BigInteger e)
  80:   {
  81:     super();
  82: 
  83:     this.defaultFormat = defaultFormat <= 0 ? Registry.RAW_ENCODING_ID
  84:                                             : defaultFormat;
  85:     this.n = n;
  86:     this.e = e;
  87:   }
  88: 
  89:   public BigInteger getModulus()
  90:   {
  91:     return getN();
  92:   }
  93: 
  94:   public String getAlgorithm()
  95:   {
  96:     return Registry.RSA_KPG;
  97:   }
  98: 
  99:   /** @deprecated see getEncoded(int). */
 100:   public byte[] getEncoded()
 101:   {
 102:     return getEncoded(defaultFormat);
 103:   }
 104: 
 105:   public String getFormat()
 106:   {
 107:     return FormatUtil.getEncodingShortName(defaultFormat);
 108:   }
 109: 
 110:   /**
 111:    * Returns the modulus <code>n</code>.
 112:    * 
 113:    * @return the modulus <code>n</code>.
 114:    */
 115:   public BigInteger getN()
 116:   {
 117:     return n;
 118:   }
 119: 
 120:   /**
 121:    * Returns the public exponent <code>e</code>.
 122:    * 
 123:    * @return the public exponent <code>e</code>.
 124:    */
 125:   public BigInteger getPublicExponent()
 126:   {
 127:     return getE();
 128:   }
 129: 
 130:   /**
 131:    * Same as {@link #getPublicExponent()}.
 132:    * 
 133:    * @return the public exponent <code>e</code>.
 134:    */
 135:   public BigInteger getE()
 136:   {
 137:     return e;
 138:   }
 139: 
 140:   /**
 141:    * Returns <code>true</code> if the designated object is an instance of
 142:    * {@link RSAKey} and has the same RSA parameter values as this one.
 143:    * 
 144:    * @param obj the other non-null RSA key to compare to.
 145:    * @return <code>true</code> if the designated object is of the same type
 146:    *         and value as this one.
 147:    */
 148:   public boolean equals(final Object obj)
 149:   {
 150:     if (obj == null)
 151:       return false;
 152: 
 153:     if (! (obj instanceof RSAKey))
 154:       return false;
 155: 
 156:     final RSAKey that = (RSAKey) obj;
 157:     return n.equals(that.getModulus());
 158:   }
 159: 
 160:   public String toString()
 161:   {
 162:     if (str == null)
 163:       {
 164:         String ls = (String) AccessController.doPrivileged
 165:             (new GetPropertyAction("line.separator"));
 166:         str = new StringBuilder(ls)
 167:             .append("defaultFormat=").append(defaultFormat).append(",").append(ls)
 168:             .append("n=0x").append(n.toString(16)).append(",").append(ls)
 169:             .append("e=0x").append(e.toString(16))
 170:             .toString();
 171:       }
 172:     return str;
 173:   }
 174: 
 175:   public abstract byte[] getEncoded(int format);
 176: }