import java.io.*;
import java.security.*;
import java.security.spec.*;
import java.util.logging.*;

/**
 * Utility for creating an RSA SHA1 signature based on a supplied PEM formatted private key
 */
public class MessageSigner {
    private static Logger logger = Logger.getLogger(MessageSigner.class.getName());
    private Signature sig;
    
    /**
     * Standard Java usage example, safe to remove
     */
    public static void main(String args[]) throws Exception {
        String data = args[0];
        MessageSigner ms = new MessageSigner("private-key.pem");
        String signature = ms.sign(data);
        
        logger.log(Level.INFO, "Request: {0}", data);
        logger.log(Level.INFO, "Response: {0}", signature);
    }
    
    /**
     * Servlet usage example, safe to remove
     */
    protected void doProcessRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Get request from URL
        String data = request.getParameter("request");

        String signature = new MessageSigner("private-key.pem").sign(data);
        
        // Send signed message back
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
        out.write(signature);		
        out.flush();
        out.close();
    }
    
    /**
     * Constructs an RSA SHA1 signature object for signing
     * @param keyPath
     * @throws Exception 
     */
    public MessageSigner(String keyPath) throws Exception {
        byte[] keyData = cleanseKeyData(readData(keyPath));
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyData);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PrivateKey key = kf.generatePrivate(keySpec);
        sig = Signature.getInstance("SHA1withRSA");
        sig.initSign(key);
    }
    
    /**
     * Signs the specified data with the provided private key, returning the
     * RSA SHA1 signature
     * @param data
     * @return
     * @throws Exception 
     */
    private String sign(String data) throws Exception {
        sig.update(data.getBytes());
        return Base64.encodeBytes(sig.sign());
    }

    /**
     * Reads the raw byte[] data from a file resource
     * @param resourcePath
     * @return the raw byte data from a resource file
     * @throws IOException 
     */
    public static byte[] readData(String resourcePath) throws IOException {
        InputStream is = MessageSigner.class.getResourceAsStream(resourcePath);
        if (is == null) {
            throw new IOException(String.format("Can't open resource \"%s\"",  resourcePath));
        }
        DataInputStream dis = new DataInputStream(is);
        byte[] data = new byte[dis.available()];
        dis.readFully(data);
        dis.close();
        return data;
    }

    /**
     * Parses an X509 PEM formatted base64 encoded private key, returns the decoded
     * private key byte data
     * @param keyData PEM file contents, a X509 base64 encoded private key
     * @return Private key data
     * @throws IOException 
     */
    private static byte[] cleanseKeyData(byte[] keyData) throws IOException {
        StringBuilder sb = new StringBuilder();
        String[] lines = new String(keyData).split("\n");
        String[] skips = new String[]{"-----BEGIN", "-----END", ": "};
        for (String line : lines) {
            boolean skipLine = false;
            for (String skip : skips) {
                if (line.contains(skip)) {
                    skipLine = true;
                }
            }
            if (!skipLine) {
                sb.append(line.trim());
            }
        }
        return Base64.decode(sb.toString());
    }

}

@@ -1,20 +1,1190 @@
+/*%0A * Java signing example%0A * Echoes the signed message and exits%0A */%0A%0A// #########################################################%0A// #             WARNING   WARNING   WARNING               #%0A// #########################################################%0A// #                                                       #%0A// # This file is intended for demonstration purposes      #%0A// # only.                                                 #%0A// #                                                       #%0A// # It is the SOLE responsibility of YOU, the programmer  #%0A// # to prevent against unauthorized access to any signing #%0A// # functions.                                            #%0A// #                                                       #%0A// # Organizations that do not protect against un-         #%0A// # authorized signing will be black-listed to prevent    #%0A// # software piracy.                                      #%0A// #                                                       #%0A// # -QZ Industries, LLC                                   #%0A// #                                                       #%0A// #########################################################%0A%0A
 import java.io.*;%0Aim

Add disclaimer to Java signing sample