Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.

Commit

Permalink
Convert PBKDF2 and HMAC to ES6 classes
Browse files Browse the repository at this point in the history
  • Loading branch information
alippai committed Jan 21, 2018
1 parent 6986c31 commit 3a95501
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 333 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"grunt-jsdoc": "~2.2",
"grunt-rollup": "^9.0.0",
"grunt-shell": "^2.1.0",
"prettier": "^1.10.2",
"qunit-puppeteer": "^1.0.1",
"uglify-es": "^3.3.4"
},
Expand Down
4 changes: 2 additions & 2 deletions src/bignum/bignum.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export class BigNumber_constructor {

const slimbs = new Uint32Array(l);
if ( t ) {
for ( let i = 0; i < m-n-1; i++ ) {
for ( var i = 0; i < m-n-1; i++ ) {
slimbs[i] = (limbs[n+i]>>>t) | ( limbs[n+i+1]<<(32-t) );
}
slimbs[i] = limbs[n+i]>>>t;
Expand Down Expand Up @@ -599,7 +599,7 @@ function _small_primes ( n ) {
return _primes.slice( 0, n );

for ( let p = _primes[_primes.length-1] + 2; _primes.length < n; p += 2 ) {
for ( let i = 0, d = _primes[i]; d*d <= p; d = _primes[++i] ) {
for ( var i = 0, d = _primes[i]; d*d <= p; d = _primes[++i] ) {
if ( p % d == 0 ) break;
}
if ( d*d > p ) _primes.push(p);
Expand Down
105 changes: 50 additions & 55 deletions src/hmac/hmac-sha1.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,74 @@
import {hmac_process, hmac_constructor, _hmac_key, _hmac_init_verify} from './hmac';
import {hmac_constructor, _hmac_key, _hmac_init_verify} from './hmac';
import {_sha1_hash_size, get_sha1_instance, sha1_constructor} from '../hash/sha1/sha1';
import {is_string, string_to_bytes} from '../utils';
import {IllegalStateError} from '../errors';

export function hmac_sha1_constructor (options ) {
export class hmac_sha1_constructor extends hmac_constructor {
constructor(options ) {
options = options || {};

if ( !( options.hash instanceof sha1_constructor ) )
options.hash = get_sha1_instance();
options.hash = get_sha1_instance();

hmac_constructor.call( this, options );
super(options);
}

return this;
}

function hmac_sha1_reset ( options ) {
reset(options ) {
options = options || {};

this.result = null;
this.hash.reset();

var password = options.password;
if ( password !== undefined ) {
if ( is_string(password) )
password = string_to_bytes(password);

var key = this.key = _hmac_key( this.hash, password );
this.hash.reset().asm.hmac_init(
(key[0]<<24)|(key[1]<<16)|(key[2]<<8)|(key[3]),
(key[4]<<24)|(key[5]<<16)|(key[6]<<8)|(key[7]),
(key[8]<<24)|(key[9]<<16)|(key[10]<<8)|(key[11]),
(key[12]<<24)|(key[13]<<16)|(key[14]<<8)|(key[15]),
(key[16]<<24)|(key[17]<<16)|(key[18]<<8)|(key[19]),
(key[20]<<24)|(key[21]<<16)|(key[22]<<8)|(key[23]),
(key[24]<<24)|(key[25]<<16)|(key[26]<<8)|(key[27]),
(key[28]<<24)|(key[29]<<16)|(key[30]<<8)|(key[31]),
(key[32]<<24)|(key[33]<<16)|(key[34]<<8)|(key[35]),
(key[36]<<24)|(key[37]<<16)|(key[38]<<8)|(key[39]),
(key[40]<<24)|(key[41]<<16)|(key[42]<<8)|(key[43]),
(key[44]<<24)|(key[45]<<16)|(key[46]<<8)|(key[47]),
(key[48]<<24)|(key[49]<<16)|(key[50]<<8)|(key[51]),
(key[52]<<24)|(key[53]<<16)|(key[54]<<8)|(key[55]),
(key[56]<<24)|(key[57]<<16)|(key[58]<<8)|(key[59]),
(key[60]<<24)|(key[61]<<16)|(key[62]<<8)|(key[63])
);
if ( is_string(password) )
password = string_to_bytes(password);

var key = this.key = _hmac_key( this.hash, password );
this.hash.reset().asm.hmac_init(
(key[0]<<24)|(key[1]<<16)|(key[2]<<8)|(key[3]),
(key[4]<<24)|(key[5]<<16)|(key[6]<<8)|(key[7]),
(key[8]<<24)|(key[9]<<16)|(key[10]<<8)|(key[11]),
(key[12]<<24)|(key[13]<<16)|(key[14]<<8)|(key[15]),
(key[16]<<24)|(key[17]<<16)|(key[18]<<8)|(key[19]),
(key[20]<<24)|(key[21]<<16)|(key[22]<<8)|(key[23]),
(key[24]<<24)|(key[25]<<16)|(key[26]<<8)|(key[27]),
(key[28]<<24)|(key[29]<<16)|(key[30]<<8)|(key[31]),
(key[32]<<24)|(key[33]<<16)|(key[34]<<8)|(key[35]),
(key[36]<<24)|(key[37]<<16)|(key[38]<<8)|(key[39]),
(key[40]<<24)|(key[41]<<16)|(key[42]<<8)|(key[43]),
(key[44]<<24)|(key[45]<<16)|(key[46]<<8)|(key[47]),
(key[48]<<24)|(key[49]<<16)|(key[50]<<8)|(key[51]),
(key[52]<<24)|(key[53]<<16)|(key[54]<<8)|(key[55]),
(key[56]<<24)|(key[57]<<16)|(key[58]<<8)|(key[59]),
(key[60]<<24)|(key[61]<<16)|(key[62]<<8)|(key[63])
);
}
else {
this.hash.asm.hmac_reset();
this.hash.asm.hmac_reset();
}

var verify = options.verify;
if ( verify !== undefined ) {
_hmac_init_verify.call( this, verify );
_hmac_init_verify.call( this, verify );
}
else {
this.verify = null;
this.verify = null;
}

return this;
}
}

function hmac_sha1_finish () {
finish() {
if ( this.key === null )
throw new IllegalStateError("no key is associated with the instance");
throw new IllegalStateError("no key is associated with the instance");

if ( this.result !== null )
throw new IllegalStateError("state must be reset before processing new data");
throw new IllegalStateError("state must be reset before processing new data");

var hash = this.hash,
asm = this.hash.asm,
heap = this.hash.heap;
asm = this.hash.asm,
heap = this.hash.heap;

asm.hmac_finish( hash.pos, hash.len, 0 );

Expand All @@ -78,34 +77,30 @@ function hmac_sha1_finish () {
result.set( heap.subarray( 0, _sha1_hash_size ) );

if ( verify ) {
if ( verify.length === result.length ) {
var diff = 0;
for ( var i = 0; i < verify.length; i++ ) {
diff |= ( verify[i] ^ result[i] );
}
this.result = !diff;
} else {
this.result = false;
if ( verify.length === result.length ) {
var diff = 0;
for ( var i = 0; i < verify.length; i++ ) {
diff |= ( verify[i] ^ result[i] );
}
this.result = !diff;
} else {
this.result = false;
}
}
else {
this.result = result;
this.result = result;
}

return this;
}
}

hmac_sha1_constructor.BLOCK_SIZE = sha1_constructor.BLOCK_SIZE;
hmac_sha1_constructor.HMAC_SIZE = sha1_constructor.HASH_SIZE;

var hmac_sha1_prototype = hmac_sha1_constructor.prototype;
hmac_sha1_prototype.reset = hmac_sha1_reset;
hmac_sha1_prototype.process = hmac_process;
hmac_sha1_prototype.finish = hmac_sha1_finish;

var hmac_sha1_instance = null;

export function get_hmac_sha1_instance () {
if ( hmac_sha1_instance === null ) hmac_sha1_instance = new hmac_sha1_constructor();
return hmac_sha1_instance;
if ( hmac_sha1_instance === null ) hmac_sha1_instance = new hmac_sha1_constructor();
return hmac_sha1_instance;
}
103 changes: 49 additions & 54 deletions src/hmac/hmac-sha256.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,74 @@
import {hmac_constructor, hmac_process, _hmac_init_verify, _hmac_key} from './hmac';
import {hmac_constructor, _hmac_init_verify, _hmac_key} from './hmac';
import {_sha256_hash_size, get_sha256_instance, sha256_constructor} from '../hash/sha256/sha256';
import {is_string, string_to_bytes} from '../utils';
import {IllegalStateError} from '../errors';

export function hmac_sha256_constructor (options ) {
export class hmac_sha256_constructor extends hmac_constructor {
constructor(options ) {
options = options || {};

if ( !( options.hash instanceof sha256_constructor ) )
options.hash = get_sha256_instance();
options.hash = get_sha256_instance();

hmac_constructor.call( this, options );
super(options);
}

return this;
}

function hmac_sha256_reset ( options ) {
reset(options ) {
options = options || {};

this.result = null;
this.hash.reset();

var password = options.password;
if ( password !== undefined ) {
if ( is_string(password) )
password = string_to_bytes(password);

var key = this.key = _hmac_key( this.hash, password );
this.hash.reset().asm.hmac_init(
(key[0]<<24)|(key[1]<<16)|(key[2]<<8)|(key[3]),
(key[4]<<24)|(key[5]<<16)|(key[6]<<8)|(key[7]),
(key[8]<<24)|(key[9]<<16)|(key[10]<<8)|(key[11]),
(key[12]<<24)|(key[13]<<16)|(key[14]<<8)|(key[15]),
(key[16]<<24)|(key[17]<<16)|(key[18]<<8)|(key[19]),
(key[20]<<24)|(key[21]<<16)|(key[22]<<8)|(key[23]),
(key[24]<<24)|(key[25]<<16)|(key[26]<<8)|(key[27]),
(key[28]<<24)|(key[29]<<16)|(key[30]<<8)|(key[31]),
(key[32]<<24)|(key[33]<<16)|(key[34]<<8)|(key[35]),
(key[36]<<24)|(key[37]<<16)|(key[38]<<8)|(key[39]),
(key[40]<<24)|(key[41]<<16)|(key[42]<<8)|(key[43]),
(key[44]<<24)|(key[45]<<16)|(key[46]<<8)|(key[47]),
(key[48]<<24)|(key[49]<<16)|(key[50]<<8)|(key[51]),
(key[52]<<24)|(key[53]<<16)|(key[54]<<8)|(key[55]),
(key[56]<<24)|(key[57]<<16)|(key[58]<<8)|(key[59]),
(key[60]<<24)|(key[61]<<16)|(key[62]<<8)|(key[63])
);
if ( is_string(password) )
password = string_to_bytes(password);

var key = this.key = _hmac_key( this.hash, password );
this.hash.reset().asm.hmac_init(
(key[0]<<24)|(key[1]<<16)|(key[2]<<8)|(key[3]),
(key[4]<<24)|(key[5]<<16)|(key[6]<<8)|(key[7]),
(key[8]<<24)|(key[9]<<16)|(key[10]<<8)|(key[11]),
(key[12]<<24)|(key[13]<<16)|(key[14]<<8)|(key[15]),
(key[16]<<24)|(key[17]<<16)|(key[18]<<8)|(key[19]),
(key[20]<<24)|(key[21]<<16)|(key[22]<<8)|(key[23]),
(key[24]<<24)|(key[25]<<16)|(key[26]<<8)|(key[27]),
(key[28]<<24)|(key[29]<<16)|(key[30]<<8)|(key[31]),
(key[32]<<24)|(key[33]<<16)|(key[34]<<8)|(key[35]),
(key[36]<<24)|(key[37]<<16)|(key[38]<<8)|(key[39]),
(key[40]<<24)|(key[41]<<16)|(key[42]<<8)|(key[43]),
(key[44]<<24)|(key[45]<<16)|(key[46]<<8)|(key[47]),
(key[48]<<24)|(key[49]<<16)|(key[50]<<8)|(key[51]),
(key[52]<<24)|(key[53]<<16)|(key[54]<<8)|(key[55]),
(key[56]<<24)|(key[57]<<16)|(key[58]<<8)|(key[59]),
(key[60]<<24)|(key[61]<<16)|(key[62]<<8)|(key[63])
);
}
else {
this.hash.asm.hmac_reset();
this.hash.asm.hmac_reset();
}

var verify = options.verify;
if ( verify !== undefined ) {
_hmac_init_verify.call( this, verify );
_hmac_init_verify.call( this, verify );
}
else {
this.verify = null;
this.verify = null;
}

return this;
}
}

function hmac_sha256_finish () {
finish() {
if ( this.key === null )
throw new IllegalStateError("no key is associated with the instance");
throw new IllegalStateError("no key is associated with the instance");

if ( this.result !== null )
throw new IllegalStateError("state must be reset before processing new data");
throw new IllegalStateError("state must be reset before processing new data");

var hash = this.hash,
asm = this.hash.asm,
heap = this.hash.heap;
asm = this.hash.asm,
heap = this.hash.heap;

asm.hmac_finish( hash.pos, hash.len, 0 );

Expand All @@ -78,34 +77,30 @@ function hmac_sha256_finish () {
result.set( heap.subarray( 0, _sha256_hash_size ) );

if ( verify ) {
if ( verify.length === result.length ) {
var diff = 0;
for ( var i = 0; i < verify.length; i++ ) {
diff |= ( verify[i] ^ result[i] );
}
this.result = !diff;
} else {
this.result = false;
if ( verify.length === result.length ) {
var diff = 0;
for ( var i = 0; i < verify.length; i++ ) {
diff |= ( verify[i] ^ result[i] );
}
this.result = !diff;
} else {
this.result = false;
}
}
else {
this.result = result;
this.result = result;
}

return this;
}
}

hmac_sha256_constructor.BLOCK_SIZE = sha256_constructor.BLOCK_SIZE;
hmac_sha256_constructor.HMAC_SIZE = sha256_constructor.HASH_SIZE;

var hmac_sha256_prototype = hmac_sha256_constructor.prototype;
hmac_sha256_prototype.reset = hmac_sha256_reset;
hmac_sha256_prototype.process = hmac_process;
hmac_sha256_prototype.finish = hmac_sha256_finish;

var hmac_sha256_instance = null;

export function get_hmac_sha256_instance () {
if ( hmac_sha256_instance === null ) hmac_sha256_instance = new hmac_sha256_constructor();
if ( hmac_sha256_instance === null ) hmac_sha256_instance = new hmac_sha256_constructor();
return hmac_sha256_instance;
}
Loading

0 comments on commit 3a95501

Please sign in to comment.