"I thought I might need it later" does not constitute a legal defense if your employer comes after you for IP theft.
IANAL, but I can't see what foundation your strategy rests on. That the code is generic does not change the fact that it was written by you to solve a problem that your employer had. This pretty unambiguously places it within the range of what your employer can claim as theirs, no matter how applicable it might be to other use cases.
It'd be one thing if you had code that you'd written a few months back that turned out to be useful solving today's problem. But what you're describing is that your employer asks you to do something and then you go and open-source part or all of the solution without consulting them.
If you are as indispensable as you say you are, the actually bulletproof approach to getting this code open sourced legally would be to use your influence to persuade your employer to officially open-source that code.