StaticTicketCrypto { public static byte[] generateHMAC(MacAlgorithm macAlgorithm, byte[] plaintext, byte[] key) throws CryptoException { byte[] result = new byte[0]; try { Mac mac = Mac.getInstance(macAlgorithm.getJavaName()); SecretKeySpec macKey = new SecretKeySpec(key, macAlgorithm.getJavaName()); mac.init(macKey); result = mac.doFinal(plaintext); } catch (InvalidKeyException | NoSuchAlgorithmException ex) { LOGGER.warn("Encountered exception while generating the HMAC " + macAlgorithm.name() + " of an encryptedState."); LOGGER.debug(ex); throw new CryptoException("Error while HMAC generation. See Debug-Log for more Information."); } return result; } private  StaticTicketCrypto(); static byte[] encrypt(CipherAlgorithm cipherAlgorithm, byte[] plaintextUnpadded, byte[] key, byte[] iv); static byte[] decrypt(CipherAlgorithm cipherAlgorithm, byte[] ciphertext, byte[] key, byte[] iv); static byte[] generateHMAC(MacAlgorithm macAlgorithm, byte[] plaintext, byte[] key); static boolean verifyHMAC(MacAlgorithm macalgo, byte[] mac, byte[] plaintext, byte[] key);  }
@Test public void testGenerateHMAC_SHA256() throws CryptoException { LOGGER.info("Generate HMAC SHA256"); byte[] plaintext = ArrayConverter.hexStringToByteArray("4869205468657265"); byte[] key = ArrayConverter.hexStringToByteArray("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"); byte[] expResult = ArrayConverter .hexStringToByteArray("b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7"); byte[] result = StaticTicketCrypto.generateHMAC(MacAlgorithm.HMAC_SHA256, plaintext, key); assertArrayEquals(expResult, result); }