Hello, I need to convert a json string to the standard format(normalize Json) requested by the server according to the instructions he said to communicate with a server.
I don't know Java, but I have its Java code. I don't know how to convert to b4x or use inline
Please help friends who know
Thankful
Finally, the function should return a string similar to this string
============================================
step 1 : normalize json that return string
step 2 : sign string returned from step1 with privatekey (RSA-PKCS1-v1.5 - 2048 - SHA256)
===========================================
I don't know Java, but I have its Java code. I don't know how to convert to b4x or use inline
Please help friends who know
Thankful
Finally, the function should return a string similar to this string
B4X:
0#2400000#1#0#0#0###1#0#1#1#0000011300#65989672956A1113A0E1CA154229C2E03#2616000#0#19117484910002#19117484910001#1#0#2400000#216000#216000#252544#125036#6037991785693265#1665989670660#19117484910002#2356566#252545
============================================
step 1 : normalize json that return string
step 2 : sign string returned from step1 with privatekey (RSA-PKCS1-v1.5 - 2048 - SHA256)
===========================================
json String:
Dim PacketMap As Map
PacketMap.Initialize
PacketMap.Put("uid", Null)
PacketMap.Put("packetType", "GET_TOKEN")
PacketMap.Put("retry", False)
PacketMap.Put("data", CreateMap("username":"test-tsp-id-1"))
PacketMap.Put("encryptionKeyId", "")
PacketMap.Put("symmetricKey", "")
PacketMap.Put("iv", "")
PacketMap.Put("fiscalId", "")
PacketMap.Put("dataSignature","")
Dim HeaderMap As Map
HeaderMap.Initialize
HeaderMap.Put("requestTraceId",DateTime.Now)
HeaderMap.Put("timestamp",DateTime.Now)
HeaderMap.Put("packet",PacketMap)
Dim json As JSONGenerator
json.Initialize(HeaderMap)
Log(json.ToPrettyString(1))
Java Code_normalize json:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.text.Collator;
import java.util.*;
public class CryptoUtils {
private final static ObjectMapper mapper = new ObjectMapper();
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte)((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
}
return data;
}
public static String normalJson(Object object, Map < String, Object > header) {
if (object == null && header == null) return null;
Map < String, Object > map = null;
if (object != null) {
if (object instanceof String) {
try {
object = mapper.readValue((String) object, Object.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e.getMessage());
}
}
if (object instanceof Collection) {
PacketsWrapper packetsWrapper = new PacketsWrapper((Collection) object);
map = mapper.convertValue(packetsWrapper, Map.class);
} else {
map = mapper.convertValue(object, Map.class);
}
}
if (map == null && header != null) {
map = header;
}
if (map != null && header != null) {
for (Map.Entry < String, Object > entry: header.entrySet()) {
map.put(entry.getKey(), entry.getValue());
}
}
Map < String, Object > result = new HashMap < > ();
flatMap(result, null, map);
StringBuilder sb = new StringBuilder();
List < String > keys = new ArrayList < > (result.keySet());
Collections.sort(keys, Collator.getInstance(Locale.ENGLISH));
for (String key: keys) {
String textValue;
Object value = result.get(key);
if (value != null) {
textValue = value.toString();
if (textValue == null || textValue.equals("")) {
textValue = "#";
} else {
textValue = textValue.replaceAll("#", "##");
}
} else {
textValue = "#";
}
sb.append(textValue).append('#');
}
return sb.deleteCharAt(sb.length() - 1).toString();
}
private static String getKey(String rootKey, String myKey) {
if (rootKey != null) {
return rootKey + "." + myKey;
} else {
return myKey;
}
}
private static void flatMap(Map < String, Object > result, String rootKey, Object input) {
if (input instanceof Collection) {
Collection list = (Collection) input;
int i = 0;
for (Object e: list) {
String key = getKey(rootKey, "E" + i++);
flatMap(result, key, e);
}
} else if (input instanceof Map) {
Map < String, Object > map = (Map) input;
for (Map.Entry < String, Object > entry: map.entrySet()) {
flatMap(result, getKey(rootKey, entry.getKey()), entry.getValue());
}
} else {
result.put(rootKey, input);
}
}
private static class PacketsWrapper {
private Collection packets;
public PacketsWrapper() {}
public PacketsWrapper(Collection packets) {
this.packets = packets;
}
public Collection getPackets() {
return packets;
}
public void setPackets(Collection packets) {
this.packets = packets;
}
}
}
Java_Code_SignString:
public static String getSignedText(String text, String algorithm, PrivateKey privateKey) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
byte[] data = text.getBytes("UTF8");
Signature sig = Signature.getInstance(algorithm == null ? " SHA256WITHRSA" : algorithm);
sig.initSign(privateKey);
sig.update(data);
byte[] signatureBytes = sig.sign();
return Base64.getEncoder().encodeToString(signatureBytes);
}
Attachments
Last edited: