Invariance and first integrals of canonical Hamiltonian equations