public String digest ( String message ) { try { Mac mac = Mac . getInstance ( algorithm ) ; SecretKeySpec macKey = new SecretKeySpec ( key , algorithm ) ; mac . init ( macKey ) ; byte [ ] digest = mac . doFinal ( ByteArray . fromString ( message ) ) ; return ByteArray . toHex ( digest ) ; } catch ( NoSuchAlgorithmException e ) { throw new IllegalStateException ( "Algorithm unavailable: " + algorithm , e ) ; } catch ( InvalidKeyException e ) { throw new IllegalArgumentException ( "Unable to construct key for " + algorithm + ". Please check the value passed in when this class was initialised." , e ) ; } }
Computes an HMAC for the given message using the key passed to the constructor .