diff --git a/src/main/java/org/bdware/sc/SM2Helper.java b/src/main/java/org/bdware/sc/SM2Helper.java new file mode 100644 index 0000000..10cdfb5 --- /dev/null +++ b/src/main/java/org/bdware/sc/SM2Helper.java @@ -0,0 +1,46 @@ +package org.bdware.sc; + +import org.bdware.sc.util.FileUtil; +import org.zz.gmhelper.SM2KeyPair; +import org.zz.gmhelper.SM2Util; + +import java.io.File; +import java.io.FileOutputStream; +import java.nio.charset.StandardCharsets; +import java.security.SecureRandom; + +public class SM2Helper { + public static void main(String[] args) { + + SM2KeyPair pair = SM2Util.generateSM2KeyPair(new SecureRandom()); + try { + if (args.length > 0 && args[0].equals("generateKeyToFile")) { + backupFile(new File("./manager.keypair")); + backupFile(new File("./manager.key")); + System.out.println("Generate file: manger.key, manager.keypair"); + FileOutputStream fout = new FileOutputStream("./manager.keypair"); + fout.write(pair.toJson().getBytes(StandardCharsets.UTF_8)); + fout.close(); + fout = new FileOutputStream("./manager.key"); + fout.write(pair.getPublicKeyStr().getBytes(StandardCharsets.UTF_8)); + fout.close(); + } else + System.out.println(pair.toJson()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static void backupFile(File file) { + if (!file.exists()) return; + File backup = null; + for (int i = 0; i <= 100; i++) { + if (i == 100) throw new IllegalArgumentException("failed to backup:" + file.getAbsolutePath()); + backup = new File(file.getParent(), file.getName() + "." + i); + if (!backup.exists()) + break; + } + FileUtil.copyFile(file, backup); + System.out.println("Backup: " + file.getAbsolutePath() + " --> " + backup.getAbsolutePath()); + } +} diff --git a/src/main/java/org/bdware/sc/boundry/JavaScriptEntry.java b/src/main/java/org/bdware/sc/boundry/JavaScriptEntry.java index efdbddc..77ef3d0 100644 --- a/src/main/java/org/bdware/sc/boundry/JavaScriptEntry.java +++ b/src/main/java/org/bdware/sc/boundry/JavaScriptEntry.java @@ -18,6 +18,7 @@ import org.bdware.sc.util.HashUtil; import org.bdware.sc.util.JsonUtil; import org.zz.gmhelper.SM2KeyPair; import wrp.jdk.nashorn.api.scripting.NashornScriptEngine; +import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror; import wrp.jdk.nashorn.internal.objects.Global; import wrp.jdk.nashorn.internal.runtime.PropertyMap; import wrp.jdk.nashorn.internal.runtime.ScriptFunction; diff --git a/src/main/java/org/bdware/sc/boundry/utils/RocksDBUtil.java b/src/main/java/org/bdware/sc/boundry/utils/RocksDBUtil.java index f9c0739..c0ae2be 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/RocksDBUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/RocksDBUtil.java @@ -75,7 +75,6 @@ public class RocksDBUtil { try { return new String(rocksDB.get(key.getBytes())); } catch (Exception e) { - e.printStackTrace(); } return null; } diff --git a/src/main/java/org/bdware/sc/engine/JSONTool.java b/src/main/java/org/bdware/sc/engine/JSONTool.java index 4e16a9a..9c65aff 100644 --- a/src/main/java/org/bdware/sc/engine/JSONTool.java +++ b/src/main/java/org/bdware/sc/engine/JSONTool.java @@ -2,6 +2,7 @@ package org.bdware.sc.engine; import com.google.gson.*; import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror; +import wrp.jdk.nashorn.internal.objects.NativeArray; import wrp.jdk.nashorn.internal.runtime.PropertyMap; import wrp.jdk.nashorn.internal.scripts.JO; @@ -31,12 +32,11 @@ public class JSONTool { } return jo; } else if (jsonElement.isJsonArray()) { - JO jo = new JO(PropertyMap.newMap()); - jo.setIsArray(); + NativeArray narray = new NativeArray(); JsonArray jsonArray = jsonElement.getAsJsonArray(); for (int i = 0; i < jsonArray.size(); i++) - jo.put(i, convertJsonElementToMirror(jsonArray.get(i)), false); - return jo; + NativeArray.push(narray, convertJsonElementToMirror(jsonArray.get(i))); + return narray; } return null; }