package com.ack.security;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.SignatureException;
import com.ack.security.ByteUtils;
/**
* utility class for administering signatures
*/
public class SignatureUtil {
public static byte[] signData( java.io.InputStream stream, java.security.PrivateKey key )
throws java.security.NoSuchProviderException, java.security.NoSuchAlgorithmException,
java.security.InvalidKeyException, java.io.IOException, java.security.SignatureException {
return signData( "SHA1withDSA", "SUN", stream, key );
}
public static byte[] signData( java.security.Signature signature,
java.io.InputStream stream,
java.security.PrivateKey privateKey )
throws java.security.NoSuchProviderException, java.security.NoSuchAlgorithmException,
java.security.InvalidKeyException, java.io.IOException, java.security.SignatureException {
// set the private to perform the sign
signature.initSign( privateKey );
// load data into the signature
byte[] buffer = com.ack.security.ByteUtils.loadBytesFromStream( stream );
signature.update( buffer );
// rsign that file and return the signed bytes
return signature.sign();
}
public static byte[] signData( String signatureAlgorithm,
String signatureProvider,
java.io.InputStream stream,
java.security.PrivateKey privateKey )
throws java.security.NoSuchProviderException, java.security.NoSuchAlgorithmException,
java.security.InvalidKeyException, java.io.IOException, java.security.SignatureException {
return signData( java.security.Signature.getInstance( signatureAlgorithm, signatureProvider ),
stream,
privateKey );
}
}