Crypto { public static String sign(String message, byte[] key) { if (key.length == 0) { return message; } try { Mac mac = Mac.getInstance("HmacSHA1"); SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1"); mac.init(signingKey); byte[] messageBytes = message.getBytes(Charsets.UTF_8); byte[] result = mac.doFinal(messageBytes); return BaseEncoding.base64().encode(result); } catch (Exception ex) { throw new RuntimeException(ex); } }  static String sign(String message, byte[] key);  }
@Test public void testSign() throws Exception { String signature = Crypto.sign("My message to sign", "my grain of salt".getBytes(Charsets.UTF_8.name())); assertThat(signature) .isNotNull() .isEqualTo("yIDXrtZ71qCfHnUNvlYSS .isEqualTo(Crypto.sign("My message to sign", "my grain of salt".getBytes(Charsets.UTF_8.name()))); }