在 ReactNative 中使用 JavaScript BigInteger 类型

YUS*_*MLE 5 javascript biginteger react-native

我有一个旧的 JavaScript 库,它在 web 客户端和 ReactJS 框架中运行良好。在这个库中有一个用于生成 BigInteger 类型的 JavaScript 文件,为此使用 JavaApplet BigInteger 类型。但是我想在我的 ReactNative 项目中使用这个库。我不能在 ReactNative 中使用 JavaApplets(我不确定!),我用现有的 JavaScript BigInteger 实现替换了它,但它们没有我需要的功能,如BigInteger.bitLength().

我的问题是:有没有办法在 ReactNative 中使用 Java BigInteger 类?!

下面是我的 JavaScript 文件:

/** BigInt.js */

var BigInteger = require("big-integer");

var USE_SJCL = true;

// let's make this much cleaner
if (USE_SJCL) {
    var BigInt = BigInteger;
    BigInt.TWO = new BigInt("2",10);

    BigInt.setup = function(callback, fail_callback) {
    callback();
    }

    BigInt.prototype.toJSONObject = function() {
    return this.toString();
    };

} else {
    BigInt = Class.extend({
        init: function(value, radix) {
        if (value == null) {
            throw "null value!";
        }

        if (USE_SJCL) {
            this._java_bigint = new BigInteger(value, radix);
        } else if (BigInt.use_applet) {
            this._java_bigint = BigInt.APPLET.newBigInteger(value, radix);
        } else {
            try {
            this._java_bigint = new java.math.BigInteger(value, radix);
            } catch (e) {
            // alert("oy " + e.toString() + " value=" + value + " , radix=" + radix);
            throw TypeError
            }
        }
        },

        toString: function() {
        return this._java_bigint.toString() + "";
        },

        toJSONObject: function() {
        return this.toString();
        },

        add: function(other) {
        return BigInt._from_java_object(this._java_bigint.add(other._java_bigint));
        },

        bitLength: function() {
        return this._java_bigint.bitLength();
        },

        mod: function(modulus) {
        return BigInt._from_java_object(this._java_bigint.mod(modulus._java_bigint));
        },

        equals: function(other) {
        return this._java_bigint.equals(other._java_bigint);
        },

        modPow: function(exp, modulus) {
        return BigInt._from_java_object(this._java_bigint.modPow(exp._java_bigint, modulus._java_bigint));
        },

        negate: function() {
        return BigInt._from_java_object(this._java_bigint.negate());
        },

        multiply: function(other) {
        return BigInt._from_java_object(this._java_bigint.multiply(other._java_bigint));
        },

        modInverse: function(modulus) {
        return BigInt._from_java_object(this._java_bigint.modInverse(modulus._java_bigint));
        }

    });

    BigInt.ready_p = false;

    BigInt._from_java_object = function(jo) {
    var obj = new BigInt("0",10);
    obj._java_bigint = jo;
    return obj;
    };

    function check_applet() {
    var is_ns4 = (navigator.appName == "Netscape" && navigator.appVersion < "5");

    var str_workaround = (navigator.appName == "Opera");

    BigInt.is_ie = (navigator.appName == "Microsoft Internet Explorer");

    var use_applet = BigInt.is_ie || (!is_ns4 && navigator.platform.substr(0, 5) == "Linux") || str_workaround || typeof(java) == 'undefined';

    // add the applet
    if (use_applet) {
        var applet_base = JSCRYPTO_HOME;

        var applet_html = '<applet codebase="' + applet_base + '" mayscript name="bigint" code="bigint.class" width=1 height=1 id="bigint_applet"></applet>';
        // var applet_html = '<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" name="bigint" width="1" height="1" codebase="http://java.sun.com/products/plugin/autodl/jinstall-1_5_0-windows-i586.cab#Version=1,5,0,0"> <param name="code" value="bigint.class"> <param name="codebase" value="' + applet_base + '"> <param name="archive" value="myapplet.jar"> <param name="type" value="application/x-java-applet;version=1.5.0"> <param name="scriptable" value="true"> <param name="mayscript" value="false"> <comment> <embed code="bigint.class" name="bigint" java_codebase="' + applet_base + '" width="1" height="1" scriptable="true" mayscript="false" type="application/x-java-applet;version=1.5.0" pluginspage="http://java.sun.com/j2se/1.5.0/download.html"> <noembed>No Java Support.</noembed> </embed> </comment> </object>';
        $("#applet_div").html(applet_html);
    }

    return use_applet;
    };

    BigInt._setup = function() {
    if (BigInt.use_applet) {
        BigInt.APPLET = document.applets["bigint"];
    }

    try {
        BigInt.ZERO = new BigInt("0",10);
        BigInt.ONE = new BigInt("1",10);
        BigInt.TWO = new BigInt("2",10);
        BigInt.FORTY_TWO = new BigInt("42",10);

        BigInt.ready_p = true;
    } catch (e) {
        if (this.num_invocations == null)
        this.num_invocations = 0;

        this.num_invocations += 1;

        if (this.num_invocations > 5) {
        if (!USE_SJCL) {
            USE_SJCL = true;
            this.num_invocations = 1;
            BigInt.use_applet = false;
        } else {

            if (BigInt.setup_interval)
            window.clearInterval(BigInt.setup_interval);

            if (BigInt.setup_fail) {
            BigInt.setup_fail();
            } else {
            alert('bigint failed!');
            }
        }
        }
        return;
    }

    if (BigInt.setup_interval)
        window.clearInterval(BigInt.setup_interval);

    if (BigInt.setup_callback)
        BigInt.setup_callback();
    };

    BigInt.setup = function(callback, fail_callback) {
    if (callback)
        BigInt.setup_callback = callback;

    if (fail_callback)
        BigInt.setup_fail = fail_callback;

    BigInt.setup_interval = window.setInterval("BigInt._setup()", 1000);
    }
}

BigInt.fromJSONObject = function(s) {
    return new BigInt(s, 10);
};

BigInt.fromInt = function(i) {
    return BigInt.fromJSONObject("" + i);
};

BigInt.use_applet = false;
Run Code Online (Sandbox Code Playgroud)

和 Java Applet 代码:

public class bigint extends java.applet.Applet {
    public java.security.SecureRandom newSecureRandom() {
        return new java.security.SecureRandom();
    }

    public java.math.BigInteger newBigInteger(String value, int radix) {
        return new java.math.BigInteger(value, radix);
    }

    public java.math.BigInteger randomBigInteger(int bitlen, java.util.Random rng) {
        return new java.math.BigInteger(bitlen, rng);
    }

    public java.math.BigInteger randomPrimeBigInteger(int bitlen, int certainty, java.util.Random rng) {
        return new java.math.BigInteger(bitlen, certainty, rng);
    }
}
Run Code Online (Sandbox Code Playgroud)