Class PKCS8Key
- java.lang.Object
-
- org.mozilla.jss.netscape.security.pkcs.PKCS8Key
-
- All Implemented Interfaces:
java.io.Serializable
,java.security.Key
,java.security.PrivateKey
,javax.security.auth.Destroyable
- Direct Known Subclasses:
DSAPrivateKey
public class PKCS8Key extends java.lang.Object implements java.security.PrivateKey
Holds a PKCS#8 key, for example a private key- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected AlgorithmId
algid
protected byte[]
encodedKey
protected byte[]
key
static java.math.BigInteger
VERSION
-
Constructor Summary
Constructors Constructor Description PKCS8Key()
Default constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static PKCS8Key
buildPKCS8Key(AlgorithmId algid, byte[] key)
void
decode(byte[] encodedKey)
void
decode(java.io.InputStream in)
Initialize an PKCS8Key object from an input stream.byte[]
encode()
Returns the DER-encoded form of the key as a byte array.void
encode(DerOutputStream out)
PKCS#8 sequence on the DER output stream.boolean
equals(java.lang.Object object)
Compares two private keys.java.lang.String
getAlgorithm()
Returns the algorithm to be used with this key.AlgorithmId
getAlgorithmId()
Returns the algorithm ID to be used with this key.byte[]
getEncoded()
Returns the DER-encoded form of the key as a byte array.java.lang.String
getFormat()
Returns the format for this key: "PKCS#8"int
hashCode()
Calculates a hash code value for this object.static PKCS8Key
parse(DerValue in)
Construct PKCS#8 subject public key from a DER value.protected void
parseKeyBits()
Parse the key bits.java.lang.String
toString()
-
-
-
Field Detail
-
algid
protected AlgorithmId algid
-
key
protected byte[] key
-
encodedKey
protected byte[] encodedKey
-
VERSION
public static final java.math.BigInteger VERSION
-
-
Method Detail
-
parse
public static PKCS8Key parse(DerValue in) throws java.io.IOException
Construct PKCS#8 subject public key from a DER value. If the runtime environment is configured with a specific class for this kind of key, a subclass is returned. Otherwise, a generic PKCS8Key object is returned.This mechanism gurantees that keys (and algorithms) may be freely manipulated and transferred, without risk of losing information. Also, when a key (or algorithm) needs some special handling, that specific need can be accomodated.
- Parameters:
in
- the DER-encoded SubjectPublicKeyInfo value- Throws:
java.io.IOException
- on data format errors
-
parseKeyBits
protected void parseKeyBits() throws java.io.IOException, java.security.InvalidKeyException
Parse the key bits. This may be redefined by subclasses to take advantage of structure within the key. For example, RSA public keys encapsulate two unsigned integers (modulus and exponent) as DER values within thekey
bits; Diffie-Hellman and DSS/DSA keys encapsulate a single unsigned integer.This function is called when creating PKCS#8 SubjectPublicKeyInfo values using the PKCS8Key member functions, such as
parse
anddecode
.- Throws:
java.io.IOException
- if a parsing error occurs.java.security.InvalidKeyException
- if the key encoding is invalid.
-
buildPKCS8Key
public static PKCS8Key buildPKCS8Key(AlgorithmId algid, byte[] key) throws java.io.IOException, java.security.InvalidKeyException
- Throws:
java.io.IOException
java.security.InvalidKeyException
-
getAlgorithm
public java.lang.String getAlgorithm()
Returns the algorithm to be used with this key.- Specified by:
getAlgorithm
in interfacejava.security.Key
-
getAlgorithmId
public AlgorithmId getAlgorithmId()
Returns the algorithm ID to be used with this key.
-
encode
public final void encode(DerOutputStream out) throws java.io.IOException
PKCS#8 sequence on the DER output stream.- Throws:
java.io.IOException
-
getEncoded
public byte[] getEncoded()
Returns the DER-encoded form of the key as a byte array.- Specified by:
getEncoded
in interfacejava.security.Key
-
getFormat
public java.lang.String getFormat()
Returns the format for this key: "PKCS#8"- Specified by:
getFormat
in interfacejava.security.Key
-
encode
public byte[] encode() throws java.security.InvalidKeyException
Returns the DER-encoded form of the key as a byte array.- Throws:
java.security.InvalidKeyException
- if an encoding error occurs.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
decode
public void decode(java.io.InputStream in) throws java.security.InvalidKeyException
Initialize an PKCS8Key object from an input stream. The data on that input stream must be encoded using DER, obeying the PKCS#8 format: a sequence consisting of a version, an algorithm ID and a bit string which holds the key. (That bit string is often used to encapsulate another DER encoded sequence.)Subclasses should not normally redefine this method; they should instead provide a
parseKeyBits
method to parse any fields inside thekey
member.- Parameters:
in
- an input stream with a DER-encoded PKCS#8 SubjectPublicKeyInfo value- Throws:
java.security.InvalidKeyException
- if a parsing error occurs.
-
decode
public void decode(byte[] encodedKey) throws java.security.InvalidKeyException
- Throws:
java.security.InvalidKeyException
-
equals
public boolean equals(java.lang.Object object)
Compares two private keys. This returns false if the object with which to compare is not of typeKey
. Otherwise, the encoding of this key object is compared with the encoding of the given key object.- Overrides:
equals
in classjava.lang.Object
- Parameters:
object
- the object with which to compare- Returns:
true
if this key has the same encoding as the object argument;false
otherwise.
-
hashCode
public int hashCode()
Calculates a hash code value for this object. Objects which are equal will also have the same hashcode.- Overrides:
hashCode
in classjava.lang.Object
-
-