PowerAuthHttpBody { public static String getSignatureBaseString(String httpMethod, String requestUri, byte[] nonce, byte[] data) { String requestUriHash = ""; if (requestUri != null) { byte[] bytes = requestUri.getBytes(StandardCharsets.UTF_8); requestUriHash = BaseEncoding.base64().encode(bytes); } String dataBase64 = ""; if (data != null) { dataBase64 = BaseEncoding.base64().encode(data); } String nonceBase64 = ""; if (nonce != null) { nonceBase64 = BaseEncoding.base64().encode(nonce); } return (httpMethod != null ? httpMethod.toUpperCase() : "GET") + "&" + requestUriHash + "&" + nonceBase64 + "&" + dataBase64; }  static String getSignatureBaseString(String httpMethod, String requestUri, byte[] nonce, byte[] data);  }
@Test public void testSignatureBaseString() { String method; String resourceId; byte[] nonce; byte[] body; String signatureBaseString; method = "POST"; resourceId = "/pa/login"; nonce = BaseEncoding.base64().decode("vkueT796IGqdXlfVIJrB9A=="); body = "{\"Platform\": \"A\",\"LanguageCode\": \"ENG\"}".getBytes(StandardCharsets.UTF_8); signatureBaseString = PowerAuthHttpBody.getSignatureBaseString(method, resourceId, nonce, body); assertEquals(signatureBaseString, "POST&L3BhL2xvZ2lu&vkueT796IGqdXlfVIJrB9A==&eyJQbGF0Zm9ybSI6ICJBIiwiTGFuZ3VhZ2VDb2RlIjogIkVORyJ9"); method = "GET"; resourceId = "/pa/login"; nonce = BaseEncoding.base64().decode("t5W/nUcGPKAVUjA11ydJeQ=="); body = "{\"Platform\": \"A\",\"LanguageCode\": \"ENG\"}".getBytes(StandardCharsets.UTF_8); signatureBaseString = PowerAuthHttpBody.getSignatureBaseString(method, resourceId, nonce, body); assertEquals(signatureBaseString, "GET&L3BhL2xvZ2lu&t5W/nUcGPKAVUjA11ydJeQ==&eyJQbGF0Zm9ybSI6ICJBIiwiTGFuZ3VhZ2VDb2RlIjogIkVORyJ9"); method = "POST"; nonce = BaseEncoding.base64().decode("oXcwuuRCCEHiw/pFiIg4bA=="); body = "{\"Platform\": \"A\",\"LanguageCode\": \"ENG\"}".getBytes(StandardCharsets.UTF_8); signatureBaseString = PowerAuthHttpBody.getSignatureBaseString(method, null, nonce, body); assertEquals(signatureBaseString, "POST&&oXcwuuRCCEHiw/pFiIg4bA==&eyJQbGF0Zm9ybSI6ICJBIiwiTGFuZ3VhZ2VDb2RlIjogIkVORyJ9"); method = "POST"; resourceId = "/pa/login"; nonce = BaseEncoding.base64().decode("U2EjaQ2N7KMlrghn7KL+3A=="); body = "{\"Platform\": \"A\",\"LanguageCode\": \"ENG\"}".getBytes(StandardCharsets.UTF_8); signatureBaseString = PowerAuthHttpBody.getSignatureBaseString(method, resourceId, nonce, body); assertEquals(signatureBaseString, "POST&L3BhL2xvZ2lu&U2EjaQ2N7KMlrghn7KL+3A==&eyJQbGF0Zm9ybSI6ICJBIiwiTGFuZ3VhZ2VDb2RlIjogIkVORyJ9"); method = "POST"; resourceId = "/pa/login"; nonce = BaseEncoding.base64().decode("UWtQ9nMNGtJQbZ9zx/J3FQ=="); body = "{\"Platform\": \"A\",\"LanguageCode\": \"ENG\"}".getBytes(StandardCharsets.UTF_8); signatureBaseString = PowerAuthHttpBody.getSignatureBaseString(method, resourceId, nonce, body); assertEquals(signatureBaseString, "POST&L3BhL2xvZ2lu&UWtQ9nMNGtJQbZ9zx/J3FQ==&eyJQbGF0Zm9ybSI6ICJBIiwiTGFuZ3VhZ2VDb2RlIjogIkVORyJ9"); resourceId = "/pa/login"; nonce = BaseEncoding.base64().decode("UWtQ9nMNGtJQbZ9zx/J3FQ=="); body = "{\"Platform\": \"A\",\"LanguageCode\": \"ENG\"}".getBytes(StandardCharsets.UTF_8); signatureBaseString = PowerAuthHttpBody.getSignatureBaseString(null, resourceId, nonce, body); assertEquals(signatureBaseString, "GET&L3BhL2xvZ2lu&UWtQ9nMNGtJQbZ9zx/J3FQ==&eyJQbGF0Zm9ybSI6ICJBIiwiTGFuZ3VhZ2VDb2RlIjogIkVORyJ9"); method = "POST"; resourceId = "/pa/login"; body = "{\"Platform\": \"A\",\"LanguageCode\": \"ENG\"}".getBytes(StandardCharsets.UTF_8); signatureBaseString = PowerAuthHttpBody.getSignatureBaseString(method, resourceId, null, body); assertEquals(signatureBaseString, "POST&L3BhL2xvZ2lu&&eyJQbGF0Zm9ybSI6ICJBIiwiTGFuZ3VhZ2VDb2RlIjogIkVORyJ9"); method = "POST"; resourceId = "/pa/login"; nonce = new byte[16]; body = "{\"Platform\": \"A\",\"LanguageCode\": \"ENG\"}".getBytes(StandardCharsets.UTF_8); signatureBaseString = PowerAuthHttpBody.getSignatureBaseString(method, resourceId, nonce, body); assertEquals(signatureBaseString, "POST&L3BhL2xvZ2lu&AAAAAAAAAAAAAAAAAAAAAA==&eyJQbGF0Zm9ybSI6ICJBIiwiTGFuZ3VhZ2VDb2RlIjogIkVORyJ9"); method = "POST"; resourceId = "/pa/login"; nonce = new byte[16]; signatureBaseString = PowerAuthHttpBody.getSignatureBaseString(method, resourceId, nonce, null); assertEquals(signatureBaseString, "POST&L3BhL2xvZ2lu&AAAAAAAAAAAAAAAAAAAAAA==&"); }