If you can get away with using Guava it is by far the simplest way to do it, and you don't have to reinvent the wheel:
final HashCode hashCode = Hashing.sha1().hashString(yourValue, Charset.defaultCharset());
You can then take the hashed value and get it as a byte[]
, as an int
, or as a long
.
No wrapping in a try catch, no shenanigans. And if you decide you want to use something other than SHA-1, Guava also supports sha256, sha 512, and a few I had never even heard about like adler32 and murmur3.