@Test public void nullInputString() { String result=""; try { result = HMAC.sign(null, "Password").toString(); } catch (Exception e) { e.printStackTrace(); assertTrue(e instanceof InvalidParameterException); } }
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; }
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; } }
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; }  }
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); }
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);  }