java - Opentimestamps - 尝试反序列化 OTS 文件时出现 Java NullPointerException
问题描述
当我尝试使用 Java 库验证时间戳时,我遇到了 Java NullPointerException。
我正在关注https://github.com/opentimestamps/java-opentimestamps的自述文件中的示例
这是我的代码:
byte[] originalFile = Files.readAllBytes(fileData.get("absoluteFilePath"));
byte[] originalBytes = new String(originalFile).getBytes("UTF-8");
String originalHex = DatatypeConverter.printHexBinary(originalBytes);
byte[] otsFile = Files.readAllBytes(fileData.get("otsFilePath"));
byte[] otsBytes = new String(otsFile).getBytes("UTF-8");
String otsHex = DatatypeConverter.printHexBinary(otsBytes);
// below is from the examples...
DetachedTimestampFile detached = DetachedTimestampFile.from( new OpSHA256(), originalFile );
DetachedTimestampFile detachedOts = DetachedTimestampFile.deserialize(ots);
HashMap<Chains, VerifyResult> result = OpenTimestamps.verify(detachedOts,detached);
这是一个例外:
java.lang.NullPointerException: null
at com.eternitywall.ots.Timestamp.doTagOrAttestation(Timestamp.java:101) ~[java-opentimestamps-1.16.jar:na]
at com.eternitywall.ots.Timestamp.deserialize(Timestamp.java:89) ~[java-opentimestamps-1.16.jar:na]
at com.eternitywall.ots.DetachedTimestampFile.deserialize(DetachedTimestampFile.java:107) ~[java-opentimestamps-1.16.jar:na]
at com.eternitywall.ots.DetachedTimestampFile.deserialize(DetachedTimestampFile.java:120) ~[java-opentimestamps-1.16.jar:na]
at com.fmr.ots.FileTimestampController.verifyDocument(FileTimestampController.java:110) ~[classes/:na]
有谁知道如何正确读取原始文件和 ots 文件以正确验证文件?
解决方案
ots文件可以读取如下
Path pathOts = Paths.get(otsfilename);
byte[] byteOts = Files.readAllBytes(pathOts);
DetachedTimestampFile detachedOts = DetachedTimestampFile.deserialize(byteOts);
可以读取普通文件并将其转换为 DetachedTimeStamp 为,
File file = new File(originalfilename);
DetachedTimestampFile detached = DetachedTimestampFile.from(new OpSHA256(), file);
最后,可以使用 detached 和 detachedOts 文件调用 verify 方法
HashMap<Chains, VerifyResult> result = OpenTimestamps.verify(detachedOts,detached);
推荐阅读
- kubernetes - 运行“minikube 插件启用入口”时未创建入口控制器
- reactjs - 我无法写入引导输入
- wordpress - 禁止除主页以外的 amp 页面并禁止子域以防止出现在域搜索控制台中
- firebase - Google Play 新用户与 Firebase first_open / 新用户比较
- accessibility - 使用 aria-expanded 宣布状态
- extjs - 如何按宽度折叠面板在 ext.js 中不显示任何内容
- r - 如何制作一个对线性模型进行 K 折交叉验证的函数?
- python - 在 Python 中绘制多个图形的有效方法
- praat - 感知实验的 PRAAT 脚本
- flutter - Flutter:使用 pushNamedAndRemoveUntil 查找已停用小部件的祖先是不安全的