And, no, I am not being facetious by saying nobody knows how to do that. I am being quite literal. Have you ever done that? Do you know how? Do you even know what you would google to figure out how?
I'm yet to see my favorite library of course's documentation on a HSM. How do you do that in e.g. PHP with MySQL? MVC with MS SQL? Java with Oracle?
Edit: There's even a project to literally do this directly from PHP[3]
[1] http://stackoverflow.com/questions/10796485/interfacing-with...
[2] http://en.wikipedia.org/wiki/PKCS_11
[3] http://stackoverflow.com/questions/3231293/how-to-interface-...
I have not used either myself, but I would imagine the documentation is quite good.
You don't get your own HSM but it's MUCH cheaper ($1/key/month) and more scalable and available than an HSM.