echo off

set JAR_SIGNER_JDK=C:\Program Files\Java\jdk1.8.0_112
set KEY_TOOL_JRE=%JAR_SIGNER_JDK%\jre
set KEY_TOOL=%KEY_TOOL_JRE%\bin\keytool.exe
set JAR_SIGNER=%JAR_SIGNER_JDK%\bin\jarsigner.exe
set PROVIDER=JCSPRSA
set STORE_TYPE=HSMDB
set KEY_ALIAS=KL2017GP
set KEY_PASSWORD=123456
set JAR_FILE=test.jar
set SIGNED_JAR_FILE=test.signed.jar
set TRUST_STORE=trust_for_jar_signer
set PARAM=-J-Dkeytool.compat=true

echo "1. List containers:"
call "%KEY_TOOL%" %PARAM% -list -providername %PROVIDER% -storetype %STORE_TYPE% -keystore NONE -storepass 1
echo "Listing completed."

pause

echo "1*. Control deleting copy-container:"
call "%KEY_TOOL%" %PARAM% -delete -alias %KEY_ALIAS% -providername %PROVIDER% -storetype %STORE_TYPE% -keystore NONE -storepass 1
echo "Control deleting completed."

pause

echo "2. Copy container:"
call "%KEY_TOOL%" %PARAM% -importkeystore -srckeystore KL2017GP.keystore -srcstoretype JKS -srcstorepass %KEY_PASSWORD% -destkeystore NONE -deststoretype %STORE_TYPE% -deststorepass 1 -destprovidername %PROVIDER% -srcalias %KEY_ALIAS% -srckeypass %KEY_PASSWORD% -destalias %KEY_ALIAS%
echo "Copying completed."

echo "Please, put jar-file %JAR_FILE% before signing and install Java CSP into JDK %JAR_SIGNER_JDK%"
pause

echo "3. Signing jar:"
call "%JAR_SIGNER%" %PARAM% -keystore NONE -storetype %STORE_TYPE% -storepass 1 -signedjar %SIGNED_JAR_FILE% -digestalg CP_SHA256 -sigalg CP_SHA256withRSA -providerName %PROVIDER% -keypass %KEY_PASSWORD% %JAR_FILE% %KEY_ALIAS%
echo "Signing jar completed."

pause

echo "4. Verifying signed jar:"
call "%JAR_SIGNER%" %PARAM% -verify %SIGNED_JAR_FILE%
echo "Verifying completed."