1

在搜索了一段时间后,我搜索了一种在 Node.js 中使用等效于以下 PHP 函数的方法,但我发现在我的情况下没有任何效果:

gmp_init gmp_import gmp_powm gmp_export

这个想法是用js重写这个php代码:

function CalculateSRP6Verifier($username, $password, $salt)
    {
        // algorithm constants
        $g = gmp_init(7);
        $N = gmp_init('894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7', 16);
        
        // calculate first hash
        $h1 = sha1(strtoupper($username . ':' . $password), TRUE);
        
        // calculate second hash
        $h2 = sha1($salt.$h1, TRUE);
        
        // convert to integer (little-endian)
        $h2 = gmp_import($h2, 1, GMP_LSW_FIRST);
        
        // g^h2 mod N
        $verifier = gmp_powm($g, $h2, $N);
        
        // convert back to a byte array (little-endian)
        $verifier = gmp_export($verifier, 1, GMP_LSW_FIRST);
        
        // pad to 32 bytes, remember that zeros go on the end in little-endian!
        $verifier = str_pad($verifier, 32, chr(0), STR_PAD_RIGHT);
        
        // done!
        return $verifier;
    }
4

1 回答 1

0

不久前我找到了我的问题的答案。这可以在 Node.js 中使用 Buffer 和以下 libs 来完成bigint-bufferbig-integer就像我在下面所做的那样。

const bigintBuffer = require(`bigint-buffer`)
const BigInteger = require(`big-integer`)
const crypto = require(`crypto`)

/**
 *
 * @param {Buffer} salt
 * @param {string} identity
 * @param {string} password
 * @return {Buffer}
 */
function computeVerifier (salt, identity, password) {
    const hashIP = crypto.createHash(`sha1`)
        .update(identity + `:` + password)
        .digest()
    const hashX = crypto.createHash(`sha1`)
        .update(salt)
        .update(hashIP)
        .digest()
    const x = bigintBuffer.toBigIntLE(hashX)
    const g = BigInt(`0x7`)
    const N = BigInt(`0x894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7`)
    const verifier = BigInteger(g).modPow(x, N)
    const lEVerifier = verifier.value.toString(16).match(/.{2}/g).reverse().join(``)
    return Buffer.from(lEVerifier, `hex`)
}

// Test
crypto.randomBytes(32, (err, buf) => {
    if (err) throw err;
    computeVerifier(buf, `foo`, `bar`);
});

如果您想直接使用我创建的适用于 TrinityCore 和 AzerothCore 的库:https ://www.npmjs.com/package/trinitycore-srp6

于 2022-01-22T00:10:35.323 回答