From a3cb913c51a28cb12e450b5a06a8a2a9995989aa Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Fri, 18 Dec 2020 15:04:12 +0100 Subject: [PATCH] v0.1.0 --- .gitignore | 3 ++- index.ts | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 5 +++++ yarn.lock | 10 ++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 index.ts diff --git a/.gitignore b/.gitignore index 04c01ba..6c7cbbe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ -dist/ \ No newline at end of file +dist/ +/yarn-error.log diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..f7a8f18 --- /dev/null +++ b/index.ts @@ -0,0 +1,53 @@ +import { randomBytes, createHash } from 'crypto' +import bigInt from 'big-integer' + +export const hex2bin = (s: String) => { + const ret = [] + for (let i = 0, l = s.length; i < l; i += 2) { + const c = parseInt(s.substr(i, 1), 16) + const k = parseInt(s.substr(i + 1, 1), 16) + if (isNaN(c) || isNaN(k)) return false + ret.push((c << 4) | k) + } + return String.fromCharCode.apply(String, ret) +} + +export const bin2hex = (s: String) => { + let result = '' + for (let i = 0, l = s.length; i < l; i++) { + const n = s.charCodeAt(i).toString(16) + result += n.length < 2 ? '0' + n : n + } + return result +} + +export const reverseHex = (string: String) => { + const bytes = [] + for (let i = 0, length = string.length; i < length; i += 2) { + bytes.push(string.substr(i, 2)) + } + return bytes.reverse().join('') +} + +export const register = ( + username: String, + password: String, + gHex = '07', + NHex = '894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7' +) => { + const g = bigInt(gHex, 16) + const N = bigInt(NHex, 16) + const saltDB = bin2hex(randomBytes(32).toString('binary')) + const salt = hex2bin(reverseHex(saltDB)) + const identity = createHash('sha1') + .update((username + ':' + password).toUpperCase()) + .digest('latin1') + const sha = reverseHex( + createHash('sha1') + .update(salt + identity, 'latin1') + .digest('hex') + ) + const privateKey = bigInt(sha, 16) + const verifier = g.modPow(privateKey, N).toString(16) + return { salt: saltDB, verifier } +} diff --git a/package.json b/package.json index c045656..38c82a6 100644 --- a/package.json +++ b/package.json @@ -4,10 +4,15 @@ "description": "SRP implementation for WoW Server authentication in Typescript", "main": "dist/index.js", "types": "dist/index.d.ts", + "prepublish": "tsc", "repository": "git@github.com:Mojotrollz/js-wowemu-auth.git", "author": "Ulf Gebhardt ", "license": "GPL-2.0-only", + "dependencies": { + "big-integer": "^1.6.48" + }, "devDependencies": { + "@types/node": "14.14.11", "typescript": "^4.1.3" } } diff --git a/yarn.lock b/yarn.lock index 3986214..00703d6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,6 +2,16 @@ # yarn lockfile v1 +"@types/node@14.14.11": + version "14.14.11" + resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.11.tgz#fc25a4248a5e8d0837019b1d170146d07334abe0" + integrity sha512-BJ97wAUuU3NUiUCp44xzUFquQEvnk1wu7q4CMEUYKJWjdkr0YWYDsm4RFtAvxYsNjLsKcrFt6RvK8r+mnzMbEQ== + +big-integer@^1.6.48: + version "1.6.48" + resolved "https://registry.yarnpkg.com/big-integer/-/big-integer-1.6.48.tgz#8fd88bd1632cba4a1c8c3e3d7159f08bb95b4b9e" + integrity sha512-j51egjPa7/i+RdiRuJbPdJ2FIUYYPhvYLjzoYbcMMm62ooO6F94fETG4MTs46zPAF9Brs04OajboA/qTGuz78w== + typescript@^4.1.3: version "4.1.3" resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"