From c2396fba423be31a4de8db3dbe09d42a04db32dc Mon Sep 17 00:00:00 2001 From: "Raif S. Naffah" Date: Tue, 20 Jun 2006 11:24:41 +0000 Subject: 2006-06-20 Raif S. Naffah * gnu/java/security/key/dss/DSSKey.java: Source formatting. * gnu/java/security/key/dss/DSSKeyPairGenerator.java: Likewise. * gnu/java/security/key/dss/DSSKeyPairPKCS8Codec.java: Likewise. * gnu/java/security/key/dss/DSSKeyPairRawCodec.java: Likewise. * gnu/java/security/key/dss/DSSKeyPairX509Codec.java: Likewise. * gnu/java/security/key/dss/DSSPrivateKey.java: Likewise. * gnu/java/security/key/dss/DSSPublicKey.java: Likewise. * gnu/java/security/key/dss/FIPS186.java: Likewise. * gnu/java/security/key/rsa/GnuRSAKey.java: Likewise. * gnu/java/security/key/rsa/GnuRSAPrivateKey.java: Likewise. * gnu/java/security/key/rsa/GnuRSAPublicKey.java: Likewise. * gnu/java/security/key/rsa/RSAKeyPairGenerator.java: Likewise. * gnu/java/security/key/rsa/RSAKeyPairPKCS8Codec.java: Likewise. * gnu/java/security/key/rsa/RSAKeyPairRawCodec.java: Likewise. * gnu/java/security/key/rsa/RSAKeyPairX509Codec.java: Likewise. * gnu/java/security/key/IKeyPairCodec.java: Likewise. * gnu/java/security/key/IKeyPairGenerator.java: Likewise. * gnu/java/security/key/KeyPairCodecFactory.java: Likewise. * gnu/java/security/key/KeyPairGeneratorFactory.java: Likewise. --- gnu/java/security/key/dss/DSSKeyPairRawCodec.java | 250 ++++++++++------------ 1 file changed, 108 insertions(+), 142 deletions(-) (limited to 'gnu/java/security/key/dss/DSSKeyPairRawCodec.java') diff --git a/gnu/java/security/key/dss/DSSKeyPairRawCodec.java b/gnu/java/security/key/dss/DSSKeyPairRawCodec.java index 7c5491d6f..b1135b75f 100644 --- a/gnu/java/security/key/dss/DSSKeyPairRawCodec.java +++ b/gnu/java/security/key/dss/DSSKeyPairRawCodec.java @@ -47,91 +47,72 @@ import java.security.PrivateKey; import java.security.PublicKey; /** - *

An object that implements the {@link IKeyPairCodec} operations for the - * Raw format to use with DSS keypairs.

+ * An object that implements the {@link IKeyPairCodec} operations for the + * Raw format to use with DSS keypairs. */ -public class DSSKeyPairRawCodec implements IKeyPairCodec +public class DSSKeyPairRawCodec + implements IKeyPairCodec { - - // Constants and variables - // ------------------------------------------------------------------------- - - // Constructor(s) - // ------------------------------------------------------------------------- - // implicit 0-arguments constructor - // Class methods - // ------------------------------------------------------------------------- - - // Instance methods - // ------------------------------------------------------------------------- - - // gnu.crypto.keys.IKeyPairCodec interface implementation ------------------ - public int getFormatID() { return RAW_FORMAT; } /** - *

Returns the encoded form of the designated DSS (Digital Signature - * Standard) public key according to the Raw format supported by - * this library.

- * - *

The Raw format for a DSA public key, in this implementation, is - * a byte sequence consisting of the following:

+ * Returns the encoded form of the designated DSS (Digital Signature Standard) + * public key according to the Raw format supported by this library. + *

+ * The Raw format for a DSA public key, in this implementation, is a + * byte sequence consisting of the following: *

    - *
  1. 4-byte magic consisting of the value of the literal - * {@link Registry#MAGIC_RAW_DSS_PUBLIC_KEY},
  2. - *
  3. 1-byte version consisting of the constant: 0x01,
  4. - *
  5. 4-byte count of following bytes representing the DSA parameter - * p in internet order,
  6. - *
  7. n-bytes representation of a {@link BigInteger} obtained by invoking - * the toByteArray() method on the DSA parameter - * p,
  8. - *
  9. 4-byte count of following bytes representing the DSA parameter - * q,
  10. - *
  11. n-bytes representation of a {@link BigInteger} obtained by invoking - * the toByteArray() method on the DSA parameter - * q,
  12. - *
  13. 4-byte count of following bytes representing the DSA parameter - * g,
  14. - *
  15. n-bytes representation of a {@link BigInteger} obtained by invoking - * the toByteArray() method on the DSA parameter - * g,
  16. - *
  17. 4-byte count of following bytes representing the DSA parameter - * y,
  18. - *
  19. n-bytes representation of a {@link BigInteger} obtained by invoking - * the toByteArray() method on the DSA parameter - * y,
  20. + *
  21. 4-byte magic consisting of the value of the literal + * {@link Registry#MAGIC_RAW_DSS_PUBLIC_KEY}, + *
  22. + *
  23. 1-byte version consisting of the constant: 0x01,
  24. + *
  25. 4-byte count of following bytes representing the DSA parameter + * p in internet order,
  26. + *
  27. n-bytes representation of a {@link BigInteger} obtained by invoking + * the toByteArray() method on the DSA parameter p, + *
  28. + *
  29. 4-byte count of following bytes representing the DSA parameter + * q,
  30. + *
  31. n-bytes representation of a {@link BigInteger} obtained by invoking + * the toByteArray() method on the DSA parameter q, + *
  32. + *
  33. 4-byte count of following bytes representing the DSA parameter + * g,
  34. + *
  35. n-bytes representation of a {@link BigInteger} obtained by invoking + * the toByteArray() method on the DSA parameter g, + *
  36. + *
  37. 4-byte count of following bytes representing the DSA parameter + * y,
  38. + *
  39. n-bytes representation of a {@link BigInteger} obtained by invoking + * the toByteArray() method on the DSA parameter y, + *
  40. *
- * + * * @param key the key to encode. * @return the Raw format encoding of the designated key. * @throws IllegalArgumentException if the designated key is not a DSS - * (Digital Signature Standard) one. + * (Digital Signature Standard) one. * @see Registry#MAGIC_RAW_DSS_PUBLIC_KEY */ public byte[] encodePublicKey(PublicKey key) { - if (!(key instanceof DSSPublicKey)) - { - throw new IllegalArgumentException("key"); - } + if (! (key instanceof DSSPublicKey)) + throw new IllegalArgumentException("key"); DSSPublicKey dssKey = (DSSPublicKey) key; ByteArrayOutputStream baos = new ByteArrayOutputStream(); - // magic baos.write(Registry.MAGIC_RAW_DSS_PUBLIC_KEY[0]); baos.write(Registry.MAGIC_RAW_DSS_PUBLIC_KEY[1]); baos.write(Registry.MAGIC_RAW_DSS_PUBLIC_KEY[2]); baos.write(Registry.MAGIC_RAW_DSS_PUBLIC_KEY[3]); - // version baos.write(0x01); - // p byte[] buffer = dssKey.getParams().getP().toByteArray(); int length = buffer.length; @@ -140,7 +121,6 @@ public class DSSKeyPairRawCodec implements IKeyPairCodec baos.write((length >>> 8) & 0xFF); baos.write(length & 0xFF); baos.write(buffer, 0, length); - // q buffer = dssKey.getParams().getQ().toByteArray(); length = buffer.length; @@ -149,7 +129,6 @@ public class DSSKeyPairRawCodec implements IKeyPairCodec baos.write((length >>> 8) & 0xFF); baos.write(length & 0xFF); baos.write(buffer, 0, length); - // g buffer = dssKey.getParams().getG().toByteArray(); length = buffer.length; @@ -158,7 +137,6 @@ public class DSSKeyPairRawCodec implements IKeyPairCodec baos.write((length >>> 8) & 0xFF); baos.write(length & 0xFF); baos.write(buffer, 0, length); - // y buffer = dssKey.getY().toByteArray(); length = buffer.length; @@ -167,7 +145,6 @@ public class DSSKeyPairRawCodec implements IKeyPairCodec baos.write((length >>> 8) & 0xFF); baos.write(length & 0xFF); baos.write(buffer, 0, length); - return baos.toByteArray(); } @@ -178,112 +155,106 @@ public class DSSKeyPairRawCodec implements IKeyPairCodec || k[1] != Registry.MAGIC_RAW_DSS_PUBLIC_KEY[1] || k[2] != Registry.MAGIC_RAW_DSS_PUBLIC_KEY[2] || k[3] != Registry.MAGIC_RAW_DSS_PUBLIC_KEY[3]) - { - throw new IllegalArgumentException("magic"); - } + throw new IllegalArgumentException("magic"); // version if (k[4] != 0x01) - { - throw new IllegalArgumentException("version"); - } - int i = 5; + throw new IllegalArgumentException("version"); + int i = 5; int l; byte[] buffer; - // p - l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8 - | (k[i++] & 0xFF); + l = k[i++] << 24 + | (k[i++] & 0xFF) << 16 + | (k[i++] & 0xFF) << 8 + | (k[i++] & 0xFF); buffer = new byte[l]; System.arraycopy(k, i, buffer, 0, l); i += l; BigInteger p = new BigInteger(1, buffer); - // q - l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8 - | (k[i++] & 0xFF); + l = k[i++] << 24 + | (k[i++] & 0xFF) << 16 + | (k[i++] & 0xFF) << 8 + | (k[i++] & 0xFF); buffer = new byte[l]; System.arraycopy(k, i, buffer, 0, l); i += l; BigInteger q = new BigInteger(1, buffer); - // g - l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8 - | (k[i++] & 0xFF); + l = k[i++] << 24 + | (k[i++] & 0xFF) << 16 + | (k[i++] & 0xFF) << 8 + | (k[i++] & 0xFF); buffer = new byte[l]; System.arraycopy(k, i, buffer, 0, l); i += l; BigInteger g = new BigInteger(1, buffer); - // y - l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8 - | (k[i++] & 0xFF); + l = k[i++] << 24 + | (k[i++] & 0xFF) << 16 + | (k[i++] & 0xFF) << 8 + | (k[i++] & 0xFF); buffer = new byte[l]; System.arraycopy(k, i, buffer, 0, l); i += l; BigInteger y = new BigInteger(1, buffer); - return new DSSPublicKey(p, q, g, y); } /** - *

Returns the encoded form of the designated DSS (Digital Signature - * Standard) private key according to the Raw format supported by - * this library.

- * - *

The Raw format for a DSA private key, in this implementation, is - * a byte sequence consisting of the following:

+ * Returns the encoded form of the designated DSS (Digital Signature Standard) + * private key according to the Raw format supported by this library. + *

+ * The Raw format for a DSA private key, in this implementation, is a + * byte sequence consisting of the following: *

    - *
  1. 4-byte magic consisting of the value of the literal - * {@link Registry#MAGIC_RAW_DSS_PRIVATE_KEY},
  2. - *
  3. 1-byte version consisting of the constant: 0x01,
  4. - *
  5. 4-byte count of following bytes representing the DSA parameter - * p in internet order,
  6. - *
  7. n-bytes representation of a {@link BigInteger} obtained by invoking - * the toByteArray() method on the DSA parameter - * p,
  8. - *
  9. 4-byte count of following bytes representing the DSA parameter - * q,
  10. - *
  11. n-bytes representation of a {@link BigInteger} obtained by invoking - * the toByteArray() method on the DSA parameter - * q,
  12. - *
  13. 4-byte count of following bytes representing the DSA parameter - * g,
  14. - *
  15. n-bytes representation of a {@link BigInteger} obtained by invoking - * the toByteArray() method on the DSA parameter - * g,
  16. - *
  17. 4-byte count of following bytes representing the DSA parameter - * x,
  18. - *
  19. n-bytes representation of a {@link BigInteger} obtained by invoking - * the toByteArray() method on the DSA parameter - * x,
  20. + *
  21. 4-byte magic consisting of the value of the literal + * {@link Registry#MAGIC_RAW_DSS_PRIVATE_KEY}, + *
  22. + *
  23. 1-byte version consisting of the constant: 0x01,
  24. + *
  25. 4-byte count of following bytes representing the DSA parameter + * p in internet order,
  26. + *
  27. n-bytes representation of a {@link BigInteger} obtained by invoking + * the toByteArray() method on the DSA parameter p, + *
  28. + *
  29. 4-byte count of following bytes representing the DSA parameter + * q,
  30. + *
  31. n-bytes representation of a {@link BigInteger} obtained by invoking + * the toByteArray() method on the DSA parameter q, + *
  32. + *
  33. 4-byte count of following bytes representing the DSA parameter + * g,
  34. + *
  35. n-bytes representation of a {@link BigInteger} obtained by invoking + * the toByteArray() method on the DSA parameter g, + *
  36. + *
  37. 4-byte count of following bytes representing the DSA parameter + * x,
  38. + *
  39. n-bytes representation of a {@link BigInteger} obtained by invoking + * the toByteArray() method on the DSA parameter x, + *
  40. *
- * + * * @param key the key to encode. * @return the Raw format encoding of the designated key. * @throws IllegalArgumentException if the designated key is not a DSS - * (Digital Signature Standard) one. + * (Digital Signature Standard) one. */ public byte[] encodePrivateKey(PrivateKey key) { - if (!(key instanceof DSSPrivateKey)) - { - throw new IllegalArgumentException("key"); - } + if (! (key instanceof DSSPrivateKey)) + throw new IllegalArgumentException("key"); DSSPrivateKey dssKey = (DSSPrivateKey) key; ByteArrayOutputStream baos = new ByteArrayOutputStream(); - // magic baos.write(Registry.MAGIC_RAW_DSS_PRIVATE_KEY[0]); baos.write(Registry.MAGIC_RAW_DSS_PRIVATE_KEY[1]); baos.write(Registry.MAGIC_RAW_DSS_PRIVATE_KEY[2]); baos.write(Registry.MAGIC_RAW_DSS_PRIVATE_KEY[3]); - // version baos.write(0x01); - // p byte[] buffer = dssKey.getParams().getP().toByteArray(); int length = buffer.length; @@ -292,7 +263,6 @@ public class DSSKeyPairRawCodec implements IKeyPairCodec baos.write((length >>> 8) & 0xFF); baos.write(length & 0xFF); baos.write(buffer, 0, length); - // q buffer = dssKey.getParams().getQ().toByteArray(); length = buffer.length; @@ -301,7 +271,6 @@ public class DSSKeyPairRawCodec implements IKeyPairCodec baos.write((length >>> 8) & 0xFF); baos.write(length & 0xFF); baos.write(buffer, 0, length); - // g buffer = dssKey.getParams().getG().toByteArray(); length = buffer.length; @@ -310,7 +279,6 @@ public class DSSKeyPairRawCodec implements IKeyPairCodec baos.write((length >>> 8) & 0xFF); baos.write(length & 0xFF); baos.write(buffer, 0, length); - // x buffer = dssKey.getX().toByteArray(); length = buffer.length; @@ -319,7 +287,6 @@ public class DSSKeyPairRawCodec implements IKeyPairCodec baos.write((length >>> 8) & 0xFF); baos.write(length & 0xFF); baos.write(buffer, 0, length); - return baos.toByteArray(); } @@ -330,52 +297,51 @@ public class DSSKeyPairRawCodec implements IKeyPairCodec || k[1] != Registry.MAGIC_RAW_DSS_PRIVATE_KEY[1] || k[2] != Registry.MAGIC_RAW_DSS_PRIVATE_KEY[2] || k[3] != Registry.MAGIC_RAW_DSS_PRIVATE_KEY[3]) - { - throw new IllegalArgumentException("magic"); - } + throw new IllegalArgumentException("magic"); // version if (k[4] != 0x01) - { - throw new IllegalArgumentException("version"); - } - int i = 5; + throw new IllegalArgumentException("version"); + int i = 5; int l; byte[] buffer; - // p - l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8 - | (k[i++] & 0xFF); + l = k[i++] << 24 + | (k[i++] & 0xFF) << 16 + | (k[i++] & 0xFF) << 8 + | (k[i++] & 0xFF); buffer = new byte[l]; System.arraycopy(k, i, buffer, 0, l); i += l; BigInteger p = new BigInteger(1, buffer); - // q - l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8 - | (k[i++] & 0xFF); + l = k[i++] << 24 + | (k[i++] & 0xFF) << 16 + | (k[i++] & 0xFF) << 8 + | (k[i++] & 0xFF); buffer = new byte[l]; System.arraycopy(k, i, buffer, 0, l); i += l; BigInteger q = new BigInteger(1, buffer); - // g - l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8 - | (k[i++] & 0xFF); + l = k[i++] << 24 + | (k[i++] & 0xFF) << 16 + | (k[i++] & 0xFF) << 8 + | (k[i++] & 0xFF); buffer = new byte[l]; System.arraycopy(k, i, buffer, 0, l); i += l; BigInteger g = new BigInteger(1, buffer); - // x - l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8 - | (k[i++] & 0xFF); + l = k[i++] << 24 + | (k[i++] & 0xFF) << 16 + | (k[i++] & 0xFF) << 8 + | (k[i++] & 0xFF); buffer = new byte[l]; System.arraycopy(k, i, buffer, 0, l); i += l; BigInteger x = new BigInteger(1, buffer); - return new DSSPrivateKey(p, q, g, x); } } -- cgit v1.2.1