/** * Calculates MD5 hash for string. assume string is UTF-8 encoded * @param input string which is going to be encoded into MD5 format * @return MD5 representation of the input string * @throws NoSuchAlgorithmException if the MD5 algorithm is not available. */ public static String calculateMd5(String input) throws NoSuchAlgorithmException { try { return calculateMd5(input, "UTF-8"); } catch (UnsupportedEncodingException e) { return null; // -- impossible -- } }
md5 = MD5Util.calculateMd5(str); } catch (Exception e) { log.error("error...", e);
md5 = MD5Util.calculateMd5(str); } catch (Exception e) { log.error("error...", e);
@Test public void testApiKeyWithSignatureSecret() throws NexmoUnacceptableAuthException, NoSuchAlgorithmException { NexmoClient nexmoClient = new NexmoClient.Builder().apiKey("api-key").signatureSecret("api-secret").build(); AuthCollection authCollection = nexmoClient.getHttpWrapper().getAuthCollection(); RequestBuilder requestBuilder = RequestBuilder.get(); authCollection.getAuth(SignatureAuthMethod.class).apply(requestBuilder); List<NameValuePair> parameters = requestBuilder.getParameters(); // This is messy but trying to generate a signature auth method and then comparing with what's on the request // could have a race condition depending on the returned timestamp. // So, we're going to generate the signature after trying to determine what the timestamp is. String timestamp = parameters .stream() .filter(pair -> "timestamp".equals(pair.getName())) .findFirst() .orElse(new BasicNameValuePair("", "")) .getValue(); String sig = MD5Util.calculateMd5("&api_key=api-key×tamp=" + timestamp + "api-secret"); assertContainsParam(parameters, "sig", sig); }