@Test public void signingShouldThrowIfUnsupportAlgorithm() throws Exception { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Unsupported algorithm: hmac-sha1"); HmacKey key = new HmacKey("key1", "abcd"); key.sign(Algorithm.HMAC_SHA1, "hello".getBytes(UTF_8)); }
public byte[] sign(Algorithm algorithm, byte[] contentBytes) { try{ SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes(), mapAlgorithm(algorithm)); Mac mac = Mac.getInstance(mapAlgorithm(algorithm)); mac.init(secretKeySpec); return mac.doFinal(contentBytes); } catch (NoSuchAlgorithmException e) { LOGGER.error("[sign] failed to sign content.", e); } catch (InvalidKeyException e) { LOGGER.error("[sign] failed to sign content.", e); } return EMPTY_BYTES; }
HmacKey implements Key { public byte[] sign(Algorithm algorithm, byte[] contentBytes) { try{ SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes(), mapAlgorithm(algorithm)); Mac mac = Mac.getInstance(mapAlgorithm(algorithm)); mac.init(secretKeySpec); return mac.doFinal(contentBytes); } catch (NoSuchAlgorithmException e) { LOGGER.error("[sign] failed to sign content.", e); } catch (InvalidKeyException e) { LOGGER.error("[sign] failed to sign content.", e); } return EMPTY_BYTES; } }
HmacKey implements Key { public byte[] sign(Algorithm algorithm, byte[] contentBytes) { try{ SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes(), mapAlgorithm(algorithm)); Mac mac = Mac.getInstance(mapAlgorithm(algorithm)); mac.init(secretKeySpec); return mac.doFinal(contentBytes); } catch (NoSuchAlgorithmException e) { LOGGER.error("[sign] failed to sign content.", e); } catch (InvalidKeyException e) { LOGGER.error("[sign] failed to sign content.", e); } return EMPTY_BYTES; } HmacKey(String keyId, String secret); }
HmacKey implements Key { public byte[] sign(Algorithm algorithm, byte[] contentBytes) { try{ SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes(), mapAlgorithm(algorithm)); Mac mac = Mac.getInstance(mapAlgorithm(algorithm)); mac.init(secretKeySpec); return mac.doFinal(contentBytes); } catch (NoSuchAlgorithmException e) { LOGGER.error("[sign] failed to sign content.", e); } catch (InvalidKeyException e) { LOGGER.error("[sign] failed to sign content.", e); } return EMPTY_BYTES; } HmacKey(String keyId, String secret); String getId(); Set<Algorithm> getAlgorithms(); boolean canVerify(); boolean verify(Algorithm algorithm, byte[] contentBytes, byte[] signatureBytes); boolean canSign(); byte[] sign(Algorithm algorithm, byte[] contentBytes); }
HmacKey implements Key { public byte[] sign(Algorithm algorithm, byte[] contentBytes) { try{ SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes(), mapAlgorithm(algorithm)); Mac mac = Mac.getInstance(mapAlgorithm(algorithm)); mac.init(secretKeySpec); return mac.doFinal(contentBytes); } catch (NoSuchAlgorithmException e) { LOGGER.error("[sign] failed to sign content.", e); } catch (InvalidKeyException e) { LOGGER.error("[sign] failed to sign content.", e); } return EMPTY_BYTES; } HmacKey(String keyId, String secret); String getId(); Set<Algorithm> getAlgorithms(); boolean canVerify(); boolean verify(Algorithm algorithm, byte[] contentBytes, byte[] signatureBytes); boolean canSign(); byte[] sign(Algorithm algorithm, byte[] contentBytes);  }