I found the answer to my question a while ago. This can be done in Node.js using the Buffer and the following libsbigint-buffer
,big-integer
as I did it below.
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`);
});
If you want to use directly a library I created one which works for TrinityCore and AzerothCore: https://www.npmjs.com/package/trinitycore-srp6
import crypto from "crypto";
import { toBigIntLE, toBufferLE } from "bigint-buffer";
import sha1 from "sha1";
import { modPow } from 'bigint-crypto-utils';
// 常量
const _g: bigint = BigInt(7);
const _N: bigint = BigInt('0x894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7');
/**
* 计算SRP6验证器
* @param username string 用户名
* @param password string 密码
* @param salt Buffer 盐
* @returns Buffer 验证器
*/
function calculateSRP6Verifier(username: string, password: string, salt: Buffer): Buffer {
const h1: Buffer = Buffer.from(sha1((username + ':' + password).toUpperCase()), 'hex') ;
const h2: bigint = toBigIntLE(Buffer.from(sha1(Buffer.concat([salt, h1])), 'hex'))
let verifier: Buffer = toBufferLE(modPow(_g, h2, _N), 32)
verifier = Buffer.concat([verifier, Buffer.alloc(32 - verifier.length, '\0')]);
return verifier;
}
/**
* 获取注册数据
* @param username string 用户名
* @param password string 密码
* @returns [Buffer, Buffer] 注册数据
*/
function getRegistrationData(username:string, password:string): [Buffer, Buffer] {
const salt: Buffer = crypto.randomBytes(32);
const verifier: Buffer = calculateSRP6Verifier(username, password, salt);
return [salt, verifier];
}
/**
* 验证SRP6
* @param user string 用户名
* @param pass string 密码
* @param salt Buffer 盐
* @param verifier Buffer 验证器
* @returns boolean 是否验证成功
*/
function verifySRP6(user: string, pass: string, salt: Buffer, verifier: Buffer): boolean {
const paddedVerifier: Buffer = calculateSRP6Verifier(user, pass, salt)
return verifier.equals(paddedVerifier);
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Node.js is an open source server-side framework built on top of the Google Chrome JavaScript Engine. The number of sites using NodeJS has increased by 84,000. It is one of the busiest cross-platform JavaScript runtimes. Node.js is an asynchronous, single-threaded, non-blocking I / O model that makes it lightweight and efficient. The Node.js package ecosystem, npm, is also the world's largest open source library ecosystem.
https://nodejs.org/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.