RSASigpublic final class RSASig extends Object Implements RSA Signatures. |
Fields Summary |
---|
String | algCurrent algorithm. | MessageDigest | mdCurrent message digest. | Cipher | cCurrent cipher. | RSAKey | kCurrent key. | byte[] | prefixSignature prefix. |
Constructors Summary |
---|
RSASig(byte[] sigPrefix, MessageDigest messageDigest)Constructs an RSA signature object that uses the specified
signature algorithm.
prefix = sigPrefix;
md = messageDigest;
try {
c = Cipher.getInstance("RSA");
} catch (NoSuchPaddingException e) {
// we used the default mode and padding this should not happen
throw new NoSuchAlgorithmException();
}
|
Methods Summary |
---|
public int | getLength()Gets the byte-length of the signature.
if (k == null)
return (short)0;
else // return the modulus length in bytes
return (short)k.getModulusLen();
| public void | initSign(PrivateKey theKey)Initializes the RSASig object with the appropriate
Key for signature creation.
if (!(theKey instanceof RSAPrivateKey)) {
throw new InvalidKeyException();
}
c.init(Cipher.ENCRYPT_MODE, theKey);
k = (RSAKey)theKey;
| public void | initVerify(PublicKey theKey)Initializes the RSASig object with the appropriate
Key for signature verification.
if (!(theKey instanceof RSAPublicKey)) {
throw new InvalidKeyException();
}
c.init(Cipher.DECRYPT_MODE, theKey);
k = (RSAKey)theKey;
| public int | sign(byte[] sigBuf, int sigOff, int sigLen)Generates the signature of all/last input data. A call to this
method also resets this signature object to the state it was in
when previously initialized via a call to init(). That is, the
object is reset and available to sign another message.
if (k == null || !(k instanceof RSAPrivateKey)) {
throw new SignatureException("Illegal State");
}
if (sigLen < k.getModulusLen()) {
throw new SignatureException("Buffer too short");
}
byte[] data = new byte[prefix.length + md.getDigestLength()];
// Include the OID of signing algorithm in padding
System.arraycopy(prefix, 0, data, 0, prefix.length);
try {
md.digest(data, prefix.length, md.getDigestLength());
/*
* we can cast to a short because a private key encryption is
* is less than the key length, which is a short.
*/
return c.doFinal(data, 0, data.length, sigBuf, sigOff);
} catch (GeneralSecurityException ce) {
throw new SignatureException(ce.getMessage());
}
| public void | update(byte[] inBuf, int inOff, int inLen)Accumulates a signature of the input data. When this method is used,
temporary storage of intermediate results is required. This method
should only be used if all the input data required for the signature
is not available in one byte array. The sign() or verify() method is
recommended whenever possible.
if (k == null) {
throw new SignatureException("Illegal State");
}
md.update(inBuf, inOff, inLen);
| public boolean | verify(byte[] sigBuf, int sigOff, int sigLen)Verifies the signature of all/last input data against the passed
in signature. A call to this method also resets this signature
object to the state it was in when previously initialized via a
call to init(). That is, the object is reset and available to
verify another message.
if (k == null || !(k instanceof RSAPublicKey)) {
throw new SignatureException("Illegal State");
}
byte[] res = null;
int val;
byte[] digest = new byte[md.getDigestLength()];
try {
md.digest(digest, 0, digest.length);
res = new byte[k.getModulusLen()];
val = c.doFinal(sigBuf, sigOff, sigLen, res, 0);
} catch (IllegalArgumentException iae) {
throw new SignatureException(iae.getMessage());
} catch (GeneralSecurityException e) {
return false;
}
int size = prefix.length + md.getDigestLength();
if (val != size) {
return false;
}
// Match the prefix corresponding to the signature algorithm
for (int i = 0; i < prefix.length; i++) {
if (res[i] != prefix[i]) {
return false;
}
}
for (int i = prefix.length; i < size; i++) {
if (res[i] != digest[i - prefix.length]) {
return false;
}
}
return true;
|
|