HMAC { public static byte[] sign(String toSign, String secret) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, UnsupportedEncodingException { validateParameters(toSign, secret); String password = secret; PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray()); SecretKeyFactory kf = SecretKeyFactory .getInstance("PBEWithMD5AndDES"); SecretKey passwordKey = kf.generateSecret(keySpec); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(passwordKey); byte[] text = toSign.getBytes("UTF-8"); byte[] signatureBytes = mac.doFinal(text); return signatureBytes; }  static byte[] sign(String toSign, String secret);  }
@Test public void testSignNotNullNotEqual() { try { byte[] Signature = HMAC.sign("Some_String", "Password"); assertNotNull(Signature); assertTrue(!Signature.toString().equals("SOME_STRING")); } catch (Exception e) { assertTrue(e instanceof Exception); } }
@Test public void nullPassword() { String result=""; try { result = HMAC.sign("Some_String", null).toString(); } catch (Exception e) { assertTrue(e instanceof InvalidParameterException); } }
@Test public void nullInputString() { String result=""; try { result = HMAC.sign(null, "Password").toString(); } catch (Exception e) { e.printStackTrace(); assertTrue(e instanceof InvalidParameterException); } }