public void encodeAndSign(X500Name subject,Signature signature) throws CertificateException, IOException, SignatureException {
  DerOutputStream out, scratch;
  byte[] certificateRequestInfo;
  byte[] sig;
  if (encoded != null)   throw new SignatureException("request is already signed");
  this.subject=subject;
  scratch=new DerOutputStream();
  scratch.putInteger(BigInteger.ZERO);
  subject.encode(scratch);
  scratch.write(subjectPublicKeyInfo.getEncoded());
  attributeSet.encode(scratch);
  out=new DerOutputStream();
  out.write(DerValue.tag_Sequence,scratch);
  certificateRequestInfo=out.toByteArray();
  scratch=out;
  signature.update(certificateRequestInfo,0,certificateRequestInfo.length);
  sig=signature.sign();
  AlgorithmId algId=null;
  try {
    algId=AlgorithmId.get(signature.getAlgorithm());
  }
 catch (  NoSuchAlgorithmException nsae) {
    throw new SignatureException(nsae);
  }
  algId.encode(scratch);
  scratch.putBitString(sig);
  out=new DerOutputStream();
  out.write(DerValue.tag_Sequence,scratch);
  encoded=out.toByteArray();
}

public void encodeAndSign ( X500Name subject , Signature signature ) throws CertificateException , IOException , SignatureException { DerOutputStream out , scratch ; byte [ ] certificateRequestInfo ; byte [ ] sig ; if ( encoded != null ) throw new SignatureException ( "request is already signed" ) ; this . subject = subject ; scratch = new DerOutputStream ( ) ; scratch . putInteger ( BigInteger . ZERO ) ; subject . encode ( scratch ) ; scratch . write ( subjectPublicKeyInfo . getEncoded ( ) ) ; attributeSet . encode ( scratch ) ; out = new DerOutputStream ( ) ; out . write ( DerValue . tag_Sequence , scratch ) ; certificateRequestInfo = out . toByteArray ( ) ; scratch = out ; signature . update ( certificateRequestInfo , 0 , certificateRequestInfo . length ) ; sig = signature . sign ( ) ; AlgorithmId algId = null ; try { algId = AlgorithmId . get ( signature . getAlgorithm ( ) ) ; } catch ( NoSuchAlgorithmException nsae ) { throw new SignatureException ( nsae ) ; } algId . encode ( scratch ) ; scratch . putBitString ( sig ) ; out = new DerOutputStream ( ) ; out . write ( DerValue . tag_Sequence , scratch ) ; encoded = out . toByteArray ( ) ; }
create the signed certificate request . this will later be retrieved in either string or binary format .
