Improved CSRF security functions

Improved generating and checking functions for preventing CSRF