Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1044 #1045

Merged
merged 2 commits into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions lib/auth_41.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,15 @@ function xor(a, b) {
return result;
}

exports.xor = xor;

function token(password, scramble1, scramble2) {
// TODO: use buffers (not sure why strings here)
if (!password) {
return Buffer.alloc(0);
}
const stage1 = sha1(password);
return exports.calculateTokenFromPasswordSha(
stage1,
scramble1,
scramble2
);
return exports.calculateTokenFromPasswordSha(stage1, scramble1, scramble2);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I did not touch these, so I suppose there must be pre commit hooks to do some auto formatting.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, there is prettier percommit hook

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

via lint-staged

}

exports.calculateTokenFromPasswordSha = function(
Expand All @@ -82,12 +80,7 @@ exports.calculateTokenFromPasswordSha = function(

exports.calculateToken = token;

exports.verifyToken = function(
publicSeed1,
publicSeed2,
token,
doubleSha
) {
exports.verifyToken = function(publicSeed1, publicSeed2, token, doubleSha) {
const hashStage1 = xor(token, sha1(publicSeed1, publicSeed2, doubleSha));
const candidateHash2 = sha1(hashStage1);
return candidateHash2.compare(doubleSha) === 0;
Expand All @@ -96,3 +89,22 @@ exports.verifyToken = function(
exports.doubleSha1 = function(password) {
return sha1(sha1(password));
};

function xorRotating(a, seed) {
if (!Buffer.isBuffer(a)) {
a = Buffer.from(a, 'binary');
}

if (!Buffer.isBuffer(seed)) {
seed = Buffer.from(seed, 'binary');
}

const result = Buffer.allocUnsafe(a.length);
const seedLen = seed.length;

for (let i = 0; i < a.length; i++) {
result[i] = a[i] ^ seed[i % seedLen];
}
return result;
}
exports.xorRotating = xorRotating;
20 changes: 2 additions & 18 deletions lib/auth_plugins/caching_sha2_password.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const PLUGIN_NAME = 'caching_sha2_password';
const crypto = require('crypto');
const { xor, xorRotating } = require('../auth_41');

const REQUEST_SERVER_KEY_PACKET = Buffer.from([2]);
const FAST_AUTH_SUCCESS_PACKET = Buffer.from([3]);
Expand All @@ -20,23 +21,6 @@ function sha256(msg) {
return hash.digest('binary');
}

function xor(a, b) {
if (!Buffer.isBuffer(a)) {
a = Buffer.from(a, 'binary');
}

if (!Buffer.isBuffer(b)) {
b = Buffer.from(b, 'binary');
}

const result = Buffer.allocUnsafe(a.length);

for (let i = 0; i < a.length; i++) {
result[i] = a[i] ^ b[i];
}
return result;
}

function calculateToken(password, scramble) {
if (!password) {
return Buffer.alloc(0);
Expand All @@ -48,7 +32,7 @@ function calculateToken(password, scramble) {
}

function encrypt(password, scramble, key) {
const stage1 = xor(
const stage1 = xorRotating(
Buffer.from(`${password}\0`, 'utf8').toString('binary'),
scramble.toString('binary')
);
Expand Down