HmacEncoder extends SystemComponent { public String encodeToHex(String text) { try { Mac mac = Mac.getInstance(algorithm); mac.init(secretKeySpec); byte[] macBytes = mac.doFinal(text.getBytes()); char[] macChars = new char[macBytes.length * 2]; for (int i = 0; i < macBytes.length; i++) { macChars[i * 2 ] = HEX_CHARACTERS[(macBytes[i] >> 4) & 0xF]; macChars[i * 2 + 1] = HEX_CHARACTERS[macBytes[i] & 0xF]; } return new String(macChars); } catch (NoSuchAlgorithmException ex) { throw new MisconfigurationException("core.NO_SUCH_ALGORITHM", algorithm, "`HmacMD5`, `HmacSHA1`, and `HmacSHA256`",ex); } catch (InvalidKeyException ex) { throw new MisconfigurationException("core.INVALID_KEY", secretKeySpec, ex); } } HmacEncoder(); String encodeToHex(String text); void setAlgorithm(String algorithm); void setSecret(String secret); @Override String toString();  }
@Test public void encode() { EnkanSystem system = EnkanSystem.of("hmac", new HmacEncoder()); system.start(); try { HmacEncoder encoder = system.getComponent("hmac"); assertThat(encoder.encodeToHex("12345")) .isEqualTo("E20F9FB4EDE5059B4F0FC18CA4B0BB804060422DE2AA764119DE11BE8EE5A586"); } finally { system.stop(); } }