Update valid.rb

Remove use of $1 global.
Add support for sha2 family.
Add reasonable error message in case algorithm could not be understood.
Add return to prevent unnecessary exception.