@Test public void calculateRFC2104HMACTest() throws SignatureException { String date = "Tue, 13 May 2014 20:10:38 +0200"; String resource = "/questionnaires/58"; String method = "GET"; String stringToSign = new StringBuilder().append(method).append(" ").append(resource).append("\n").append(date) .toString(); String secret = "L0LTMJ1XAMEY13FT27MWZMQX1LWK8I1L"; String signature = HMACSignature.calculateRFC2104HMAC(stringToSign, secret); assertThat(signature).isEqualTo("oVMRHIMFzmt0NrczGQG8iAu9prc="); }
public static String calculateRFC2104HMAC(String data, String key) throws java.security.SignatureException { String result = null; try { Charset utf8ChartSet = Charset.forName("UTF-8"); SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(utf8ChartSet), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); byte[] rawHmac = mac.doFinal(data.getBytes(utf8ChartSet)); result = Base64.encodeToString(rawHmac); } catch (NoSuchAlgorithmException | InvalidKeyException e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } return result; }
HMACSignature { public static String calculateRFC2104HMAC(String data, String key) throws java.security.SignatureException { String result = null; try { Charset utf8ChartSet = Charset.forName("UTF-8"); SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(utf8ChartSet), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); byte[] rawHmac = mac.doFinal(data.getBytes(utf8ChartSet)); result = Base64.encodeToString(rawHmac); } catch (NoSuchAlgorithmException | InvalidKeyException e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } return result; } }
HMACSignature { public static String calculateRFC2104HMAC(String data, String key) throws java.security.SignatureException { String result = null; try { Charset utf8ChartSet = Charset.forName("UTF-8"); SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(utf8ChartSet), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); byte[] rawHmac = mac.doFinal(data.getBytes(utf8ChartSet)); result = Base64.encodeToString(rawHmac); } catch (NoSuchAlgorithmException | InvalidKeyException e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } return result; }  }
HMACSignature { public static String calculateRFC2104HMAC(String data, String key) throws java.security.SignatureException { String result = null; try { Charset utf8ChartSet = Charset.forName("UTF-8"); SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(utf8ChartSet), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); byte[] rawHmac = mac.doFinal(data.getBytes(utf8ChartSet)); result = Base64.encodeToString(rawHmac); } catch (NoSuchAlgorithmException | InvalidKeyException e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } return result; }  static String calculateRFC2104HMAC(String data, String key); }
HMACSignature { public static String calculateRFC2104HMAC(String data, String key) throws java.security.SignatureException { String result = null; try { Charset utf8ChartSet = Charset.forName("UTF-8"); SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(utf8ChartSet), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); byte[] rawHmac = mac.doFinal(data.getBytes(utf8ChartSet)); result = Base64.encodeToString(rawHmac); } catch (NoSuchAlgorithmException | InvalidKeyException e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } return result; }  static String calculateRFC2104HMAC(String data, String key);  }