ReactNative DH秘钥交换测试

ReactNative使用DH秘钥交换,JS端使用了bignumber库,结果iPhone5s移动端计算时间达到了7秒,使用了react-native-crypto库替代了node.js下的crypto(不支持RN)库后,移动端平均时间减少到了500ms,PC端减少 到了几十毫秒

test1() {
    console.log("----------------test1")

    // https://nodejs.org/api/crypto.html#crypto_class_diffiehellman
    const dh1 = crypto.getDiffieHellman('modp14')
    const dh2 = crypto.getDiffieHellman('modp14')

    if (dh1.getPrime().toString('hex') == dh2.getPrime().toString('hex')) {
        console.log("equal primes")        // 打印
    }

    if (dh1.getGenerator().toString('hex') == dh2.getGenerator().toString('hex')) {
        console.log("equal getGenerator")  // 打印
    }

    dh1.generateKeys()
    dh2.generateKeys()

    const dh1_secret = dh1.computeSecret(dh2.getPublicKey())
    const dh2_secret = dh2.computeSecret(dh1.getPublicKey())


    if (dh1_secret == dh2_secret) {
        console.log("is equal")
    } else {
        console.log("is not equal")         // 打印
    }

    if (dh1_secret.toString('hex') == dh2_secret.toString('hex')) {
        console.log("secret tostring is equal")         // 打印
    } else {
        console.log("secret tostring is not equal")
    }
}

test2() {
    console.log("----------------test2")

    // https://nodejs.org/api/crypto.html#crypto_class_diffiehellman
    var server = crypto.createDiffieHellman(32, '3');
    var prime = server.getPrime();
    console.log("prime:"+prime.toString('hex'))

    const dh1 = crypto.createDiffieHellman(prime)
    const dh2 = crypto.createDiffieHellman(prime)

    if (dh1.getPrime().toString('hex') == dh2.getPrime().toString('hex')) {
        console.log("equal primes")        // 打印
    }

    if (dh1.getGenerator().toString('hex') == dh2.getGenerator().toString('hex')) {
        console.log("equal getGenerator")  // 打印
    }

    dh1.generateKeys()
    dh2.generateKeys()

    const dh1_secret = dh1.computeSecret(dh2.getPublicKey())
    const dh2_secret = dh2.computeSecret(dh1.getPublicKey())

    if (dh1_secret == dh2_secret) {
        console.log("is equal")
    } else {
        console.log("is not equal")         // 打印
    }

    if (dh1_secret.toString('hex') == dh2_secret.toString('hex')) {
        console.log("secret tostring is equal")         // 打印
    } else {
        console.log("secret tostring is not equal")
    }
}

test3() {
    // console.log("----------------test3")

    // https://nodejs.org/api/crypto.html#crypto_class_diffiehellman
    // var prime = server.getPrime('hex');
    // console.log("prime:"+prime.toString('hex'))

    const dh1 = crypto.createDiffieHellman('eab2115f', 'hex')
    const dh2 = crypto.createDiffieHellman('eab2115f', 'hex')

    // if (dh1.getPrime().toString('hex') == dh2.getPrime().toString('hex')) {
    //     console.log("equal primes")        // 打印
    // }
    //
    // if (dh1.getGenerator().toString('hex') == dh2.getGenerator().toString('hex')) {
    //     console.log("equal getGenerator")  // 打印
    // }

    dh1.generateKeys()
    dh2.generateKeys()

    const dh1_secret = dh1.computeSecret(dh2.getPublicKey())
    const dh2_secret = dh2.computeSecret(dh1.getPublicKey())

    // if (dh1_secret == dh2_secret) {
    //     console.log("is equal")
    // } else {
    //     console.log("is not equal")         // 打印
    // }
    //
    // if (dh1_secret.toString('hex') == dh2_secret.toString('hex')) {
    //     console.log("secret tostring is equal")         // 打印
    // } else {
    //     console.log("secret tostring is not equal")
    // }
}

test4() {
    let st = new Date()
    for (let i = 0; i < 10; ++i) {
        this.test2()
    }
    let et = new Date()

    this.setState({
        time: (et - st)/10
    })
}

测试例子2:

var assert = require("assert");
var crypto = require("crypto");

// the prime is shared by everyone
var server = crypto.createDiffieHellman(512);
var prime = server.getPrime();


// sharing secret key on a pair
var alice = crypto.createDiffieHellman(prime);
alice.generateKeys();
var alicePub = alice.getPublicKey();

var bob = crypto.createDiffieHellman(prime);
bob.generateKeys();
var bobPub = bob.getPublicKey();

var bobAliceSecret = bob.computeSecret(alicePub);
var aliceBobSecret = alice.computeSecret(bobPub);

// public keys are different, but secret is common.
assert.notEqual(bobPub, alicePub);
assert.equal(bobAliceSecret, aliceBobSecret);
// use common secret as shared encryption key...

// shared secret with 3rd person
var carol = crypto.createDiffieHellman(prime);
carol.generateKeys();
var carolPub = carol.getPublicKey();

var carolAliceSecret = carol.computeSecret(alicePub);
var aliceCarolSecret = alice.computeSecret(carolPub);

assert.notEqual(carolPub, alicePub);
assert.equal(carolAliceSecret, aliceCarolSecret);

// secret depends on pairs
assert.notEqual(aliceBobSecret, aliceCarolSecret);
var carolBobSecret = carol.computeSecret(bobPub);
assert.notEqual(carolAliceSecret, carolBobSecret);

实际使用RN下的node_modules/diffie-hellman文档和node.js下的dh文档参数有出入,填了不少坑:

// 创建 3为底数、7fffffc3为mod的dh
const dh = crypto.createDiffieHellman('7fffffc3', 'hex', new Buffer([3]))

// 计算秘钥
let svr_pub_key = 123456789      // number int64
const serect = dh.computeSecret(svr_pub_key.toString())

node.js下的DH参考链接:
https://nodejs.org/api/crypto.html#crypto_diffiehellman_getprime_encoding

大素数参考链接:
https://www.rfc-editor.org/rfc/rfc3526.txt

你可能感兴趣的:(ReactNative DH秘钥交换测试)