Javacard:使用 ant-javacard 构建包时静态数组初始化错误

Javacard: error on static array initialization in package build with ant-javacard

我有以下带有静态数组的库包的玩具代码:

package testapplets.library;
import javacard.security.RandomData;

public class SomeLibrary {
    public static final short TRUE = (short) 0x5AA5;
    public static final short FALSE = (short) 0xA55A;

    public static final byte[] test = { 'a', 'b', 'c'};

    public static short booleantest(boolean b) {
            return b ? TRUE : FALSE;
    }

    public static RandomData getRandom() {
        return RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
    }
}

和以下构建库包的 ant 任务:

  <target name="libtest" depends="prepare">
    <javacard jckit="${converter.sdk}">
      <cap targetsdk="${target.sdk}" sources="src/library" package="testapplets.library" aid="11:22:33:44:55" export="lib" output="build/libtest.cap" version="1.0"/>
    </javacard>
  </target>

但我收到以下错误消息:

  [convert] [ INFO: ] Converter [v3.1.0]
  [convert] [ INFO: ]     Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
  [convert]     
  [convert]     
  [convert] [ INFO: ] conversion completed with 1 errors and 0 warnings.
  [convert] error:  Static array initialization in class testapplets/library/SomeLibrary in library package not allowed.

为什么库中的静态数组有问题?如果我删除静态数组

构建良好

如果我没记错的话,在 Java 卡中禁止在字段初始化中使用 new。您要么必须从静态 install 方法(或从 install 调用的方法)中执行 new,要么可以标记字段 private,在这种情况下,数组存储在常量池中。我强烈推荐后者。基本上,不允许 static 代码执行。

使用 TRUEFALSE 作为 public 静态字段也不是一个好主意,因为它们将是引用查找,这很容易受到时序预言的影响。以这种方式定义 TRUEFALSE 的整个想法是为了防止此类预言机和故障注入;我建议制作它们 private.