diff --git a/package.json b/package.json index 9220f64..8ad44b4 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/bignum/bignum.js b/src/bignum/bignum.js index 982032c..96a81e4 100644 --- a/src/bignum/bignum.js +++ b/src/bignum/bignum.js @@ -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; @@ -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); diff --git a/src/hmac/hmac-sha1.js b/src/hmac/hmac-sha1.js index 4ecf250..9a37096 100644 --- a/src/hmac/hmac-sha1.js +++ b/src/hmac/hmac-sha1.js @@ -1,20 +1,19 @@ -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; @@ -22,54 +21,54 @@ function hmac_sha1_reset ( options ) { 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 ); @@ -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; } diff --git a/src/hmac/hmac-sha256.js b/src/hmac/hmac-sha256.js index b2f4faa..8444b9a 100644 --- a/src/hmac/hmac-sha256.js +++ b/src/hmac/hmac-sha256.js @@ -1,20 +1,19 @@ -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; @@ -22,54 +21,54 @@ function hmac_sha256_reset ( options ) { 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 ); @@ -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; } diff --git a/src/hmac/hmac-sha512.js b/src/hmac/hmac-sha512.js index 9c238d8..6375170 100644 --- a/src/hmac/hmac-sha512.js +++ b/src/hmac/hmac-sha512.js @@ -1,20 +1,19 @@ import {_sha512_hash_size, get_sha512_instance, sha512_constructor} from '../hash/sha512/sha512'; -import {hmac_constructor, _hmac_init_verify, _hmac_key, hmac_process} from './hmac'; +import {hmac_constructor, _hmac_init_verify, _hmac_key} from './hmac'; import {is_string, string_to_bytes} from '../utils'; import {IllegalStateError} from '../errors'; -export function hmac_sha512_constructor (options ) { +export class hmac_sha512_constructor extends hmac_constructor { + constructor(options ) { options = options || {}; if ( !( options.hash instanceof sha512_constructor ) ) - options.hash = get_sha512_instance(); + options.hash = get_sha512_instance(); - hmac_constructor.call( this, options ); + super(options); + } - return this; -} - -function hmac_sha512_reset ( options ) { + reset(options ) { options = options || {}; this.result = null; @@ -22,70 +21,70 @@ function hmac_sha512_reset ( options ) { 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]), - (key[64]<<24)|(key[65]<<16)|(key[66]<<8)|(key[67]), - (key[68]<<24)|(key[69]<<16)|(key[70]<<8)|(key[71]), - (key[72]<<24)|(key[73]<<16)|(key[74]<<8)|(key[75]), - (key[76]<<24)|(key[77]<<16)|(key[78]<<8)|(key[79]), - (key[80]<<24)|(key[81]<<16)|(key[82]<<8)|(key[83]), - (key[84]<<24)|(key[85]<<16)|(key[86]<<8)|(key[87]), - (key[88]<<24)|(key[89]<<16)|(key[90]<<8)|(key[91]), - (key[92]<<24)|(key[93]<<16)|(key[94]<<8)|(key[95]), - (key[96]<<24)|(key[97]<<16)|(key[98]<<8)|(key[99]), - (key[100]<<24)|(key[101]<<16)|(key[102]<<8)|(key[103]), - (key[104]<<24)|(key[105]<<16)|(key[106]<<8)|(key[107]), - (key[108]<<24)|(key[109]<<16)|(key[110]<<8)|(key[111]), - (key[112]<<24)|(key[113]<<16)|(key[114]<<8)|(key[115]), - (key[116]<<24)|(key[117]<<16)|(key[118]<<8)|(key[119]), - (key[120]<<24)|(key[121]<<16)|(key[122]<<8)|(key[123]), - (key[124]<<24)|(key[125]<<16)|(key[126]<<8)|(key[127]) - ); + 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]), + (key[64]<<24)|(key[65]<<16)|(key[66]<<8)|(key[67]), + (key[68]<<24)|(key[69]<<16)|(key[70]<<8)|(key[71]), + (key[72]<<24)|(key[73]<<16)|(key[74]<<8)|(key[75]), + (key[76]<<24)|(key[77]<<16)|(key[78]<<8)|(key[79]), + (key[80]<<24)|(key[81]<<16)|(key[82]<<8)|(key[83]), + (key[84]<<24)|(key[85]<<16)|(key[86]<<8)|(key[87]), + (key[88]<<24)|(key[89]<<16)|(key[90]<<8)|(key[91]), + (key[92]<<24)|(key[93]<<16)|(key[94]<<8)|(key[95]), + (key[96]<<24)|(key[97]<<16)|(key[98]<<8)|(key[99]), + (key[100]<<24)|(key[101]<<16)|(key[102]<<8)|(key[103]), + (key[104]<<24)|(key[105]<<16)|(key[106]<<8)|(key[107]), + (key[108]<<24)|(key[109]<<16)|(key[110]<<8)|(key[111]), + (key[112]<<24)|(key[113]<<16)|(key[114]<<8)|(key[115]), + (key[116]<<24)|(key[117]<<16)|(key[118]<<8)|(key[119]), + (key[120]<<24)|(key[121]<<16)|(key[122]<<8)|(key[123]), + (key[124]<<24)|(key[125]<<16)|(key[126]<<8)|(key[127]) + ); } 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_sha512_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 ); @@ -94,34 +93,30 @@ function hmac_sha512_finish () { result.set( heap.subarray( 0, _sha512_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_sha512_constructor.BLOCK_SIZE = sha512_constructor.BLOCK_SIZE; hmac_sha512_constructor.HMAC_SIZE = sha512_constructor.HASH_SIZE; -var hmac_sha512_prototype = hmac_sha512_constructor.prototype; -hmac_sha512_prototype.reset = hmac_sha512_reset; -hmac_sha512_prototype.process = hmac_process; -hmac_sha512_prototype.finish = hmac_sha512_finish; - var hmac_sha512_instance = null; export function get_hmac_sha512_instance () { - if ( hmac_sha512_instance === null ) hmac_sha512_instance = new hmac_sha512_constructor(); - return hmac_sha512_instance; + if ( hmac_sha512_instance === null ) hmac_sha512_instance = new hmac_sha512_constructor(); + return hmac_sha512_instance; } diff --git a/src/hmac/hmac.js b/src/hmac/hmac.js index b085f17..0a1d36d 100644 --- a/src/hmac/hmac.js +++ b/src/hmac/hmac.js @@ -1,14 +1,15 @@ import {is_buffer, is_bytes, is_string, string_to_bytes} from '../utils'; import {IllegalArgumentError, IllegalStateError} from '../errors'; -export function hmac_constructor (options ) { +export class hmac_constructor { + constructor(options ) { options = options || {}; if ( !options.hash ) - throw new SyntaxError("option 'hash' is required"); + throw new SyntaxError("option 'hash' is required"); if ( !options.hash.HASH_SIZE ) - throw new SyntaxError("option 'hash' supplied doesn't seem to be a valid hash function"); + throw new SyntaxError("option 'hash' supplied doesn't seem to be a valid hash function"); this.hash = options.hash; this.BLOCK_SIZE = this.hash.BLOCK_SIZE; @@ -19,127 +20,124 @@ export function hmac_constructor (options ) { this.result = null; if ( options.password !== undefined || options.verify !== undefined ) - this.reset(options); + this.reset(options); return this; -} - -export function _hmac_key ( hash, password ) { - if ( is_buffer(password) ) - password = new Uint8Array(password); - - if ( is_string(password) ) - password = string_to_bytes(password); - - if ( !is_bytes(password) ) - throw new TypeError("password isn't of expected type"); - - var key = new Uint8Array( hash.BLOCK_SIZE ); - - if ( password.length > hash.BLOCK_SIZE ) { - key.set( hash.reset().process(password).finish().result ); - } - else { - key.set(password); - } - - return key; -} + } -export function _hmac_init_verify ( verify ) { - if ( is_buffer(verify) || is_bytes(verify) ) { - verify = new Uint8Array(verify); - } - else if ( is_string(verify) ) { - verify = string_to_bytes(verify); - } - else { - throw new TypeError("verify tag isn't of expected type"); - } - - if ( verify.length !== this.HMAC_SIZE ) - throw new IllegalArgumentError("illegal verification tag size"); - - this.verify = verify; -} - -function hmac_reset ( options ) { + reset(options ) { options = options || {}; var password = options.password; if ( this.key === null && !is_string(password) && !password ) - throw new IllegalStateError("no key is associated with the instance"); + throw new IllegalStateError("no key is associated with the instance"); this.result = null; this.hash.reset(); if ( password || is_string(password) ) - this.key = _hmac_key( this.hash, password ); + this.key = _hmac_key( this.hash, password ); var ipad = new Uint8Array(this.key); for ( var i = 0; i < ipad.length; ++i ) - ipad[i] ^= 0x36; + ipad[i] ^= 0x36; this.hash.process(ipad); 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; -} + } -export function hmac_process ( data ) { + process(data ) { 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"); this.hash.process(data); return this; -} + } -function hmac_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 inner_result = this.hash.finish().result; var opad = new Uint8Array(this.key); for ( var i = 0; i < opad.length; ++i ) - opad[i] ^= 0x5c; + opad[i] ^= 0x5c; var verify = this.verify; var result = this.hash.reset().process(opad).process(inner_result).finish().result; 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; + } +} + +export function _hmac_key ( hash, password ) { + if ( is_buffer(password) ) + password = new Uint8Array(password); + + if ( is_string(password) ) + password = string_to_bytes(password); + + if ( !is_bytes(password) ) + throw new TypeError("password isn't of expected type"); + + var key = new Uint8Array( hash.BLOCK_SIZE ); + + if ( password.length > hash.BLOCK_SIZE ) { + key.set( hash.reset().process(password).finish().result ); + } + else { + key.set(password); + } + + return key; +} + +export function _hmac_init_verify ( verify ) { + if ( is_buffer(verify) || is_bytes(verify) ) { + verify = new Uint8Array(verify); + } + else if ( is_string(verify) ) { + verify = string_to_bytes(verify); + } + else { + throw new TypeError("verify tag isn't of expected type"); + } + + if ( verify.length !== this.HMAC_SIZE ) + throw new IllegalArgumentError("illegal verification tag size"); + + this.verify = verify; } -var hmac_prototype = hmac_constructor.prototype; -hmac_prototype.reset = hmac_reset; -hmac_prototype.process = hmac_process; -hmac_prototype.finish = hmac_finish; diff --git a/src/pbkdf2/pbkdf2-hmac-sha1.js b/src/pbkdf2/pbkdf2-hmac-sha1.js index 80a177a..e4705e0 100644 --- a/src/pbkdf2/pbkdf2-hmac-sha1.js +++ b/src/pbkdf2/pbkdf2-hmac-sha1.js @@ -1,25 +1,24 @@ import {get_hmac_sha1_instance, hmac_sha1_constructor} from '../hmac/hmac-sha1'; -import {pbkdf2_constructor, pbkdf2_reset} from './pbkdf2'; +import {pbkdf2_constructor} from './pbkdf2'; import {is_string} from '../utils'; import {IllegalArgumentError, IllegalStateError} from '../errors'; -export function pbkdf2_hmac_sha1_constructor (options ) { +export class pbkdf2_hmac_sha1_constructor extends pbkdf2_constructor { + constructor(options ) { options = options || {}; if ( !( options.hmac instanceof hmac_sha1_constructor ) ) - options.hmac = get_hmac_sha1_instance(); + options.hmac = get_hmac_sha1_instance(); - pbkdf2_constructor.call( this, options ); + super(options); + } - return this; -} - -function pbkdf2_hmac_sha1_generate ( salt, count, length ) { + generate(salt, count, length ) { 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"); if ( !salt && !is_string(salt) ) - throw new IllegalArgumentError("bad 'salt' value"); + throw new IllegalArgumentError("bad 'salt' value"); count = count || this.count; length = length || this.length; @@ -29,22 +28,19 @@ function pbkdf2_hmac_sha1_generate ( salt, count, length ) { var blocks = Math.ceil( length / this.hmac.HMAC_SIZE ); for ( var i = 1; i <= blocks; ++i ) { - var j = ( i - 1 ) * this.hmac.HMAC_SIZE; - var l = ( i < blocks ? 0 : length % this.hmac.HMAC_SIZE ) || this.hmac.HMAC_SIZE; + var j = ( i - 1 ) * this.hmac.HMAC_SIZE; + var l = ( i < blocks ? 0 : length % this.hmac.HMAC_SIZE ) || this.hmac.HMAC_SIZE; - this.hmac.reset().process(salt); - this.hmac.hash.asm.pbkdf2_generate_block( this.hmac.hash.pos, this.hmac.hash.len, i, count, 0 ); + this.hmac.reset().process(salt); + this.hmac.hash.asm.pbkdf2_generate_block( this.hmac.hash.pos, this.hmac.hash.len, i, count, 0 ); - this.result.set( this.hmac.hash.heap.subarray( 0, l ), j ); + this.result.set( this.hmac.hash.heap.subarray( 0, l ), j ); } return this; + } } -var pbkdf2_hmac_sha1_prototype = pbkdf2_hmac_sha1_constructor.prototype; -pbkdf2_hmac_sha1_prototype.reset = pbkdf2_reset; -pbkdf2_hmac_sha1_prototype.generate = pbkdf2_hmac_sha1_generate; - var pbkdf2_hmac_sha1_instance = null; export function get_pbkdf2_hmac_sha1_instance () { diff --git a/src/pbkdf2/pbkdf2-hmac-sha256.js b/src/pbkdf2/pbkdf2-hmac-sha256.js index 430e819..3bb0ca8 100644 --- a/src/pbkdf2/pbkdf2-hmac-sha256.js +++ b/src/pbkdf2/pbkdf2-hmac-sha256.js @@ -1,25 +1,24 @@ -import {pbkdf2_constructor, pbkdf2_reset} from './pbkdf2'; +import {pbkdf2_constructor} from './pbkdf2'; import {get_hmac_sha256_instance, hmac_sha256_constructor} from '../hmac/hmac-sha256'; import {is_string} from '../utils'; import {IllegalArgumentError, IllegalStateError} from '../errors'; -export function pbkdf2_hmac_sha256_constructor ( options ) { +export class pbkdf2_hmac_sha256_constructor extends pbkdf2_constructor { + constructor(options ) { options = options || {}; if ( !( options.hmac instanceof hmac_sha256_constructor ) ) - options.hmac = get_hmac_sha256_instance(); + options.hmac = get_hmac_sha256_instance(); - pbkdf2_constructor.call( this, options ); + super(options); + } - return this; -} - -function pbkdf2_hmac_sha256_generate ( salt, count, length ) { + generate(salt, count, length ) { 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"); if ( !salt && !is_string(salt) ) - throw new IllegalArgumentError("bad 'salt' value"); + throw new IllegalArgumentError("bad 'salt' value"); count = count || this.count; length = length || this.length; @@ -29,22 +28,19 @@ function pbkdf2_hmac_sha256_generate ( salt, count, length ) { var blocks = Math.ceil( length / this.hmac.HMAC_SIZE ); for ( var i = 1; i <= blocks; ++i ) { - var j = ( i - 1 ) * this.hmac.HMAC_SIZE; - var l = ( i < blocks ? 0 : length % this.hmac.HMAC_SIZE ) || this.hmac.HMAC_SIZE; + var j = ( i - 1 ) * this.hmac.HMAC_SIZE; + var l = ( i < blocks ? 0 : length % this.hmac.HMAC_SIZE ) || this.hmac.HMAC_SIZE; - this.hmac.reset().process(salt); - this.hmac.hash.asm.pbkdf2_generate_block( this.hmac.hash.pos, this.hmac.hash.len, i, count, 0 ); + this.hmac.reset().process(salt); + this.hmac.hash.asm.pbkdf2_generate_block( this.hmac.hash.pos, this.hmac.hash.len, i, count, 0 ); - this.result.set( this.hmac.hash.heap.subarray( 0, l ), j ); + this.result.set( this.hmac.hash.heap.subarray( 0, l ), j ); } return this; + } } -var pbkdf2_hmac_sha256_prototype = pbkdf2_hmac_sha256_constructor.prototype; -pbkdf2_hmac_sha256_prototype.reset = pbkdf2_reset; -pbkdf2_hmac_sha256_prototype.generate = pbkdf2_hmac_sha256_generate; - var pbkdf2_hmac_sha256_instance = null; export function get_pbkdf2_hmac_sha256_instance () { diff --git a/src/pbkdf2/pbkdf2-hmac-sha512.js b/src/pbkdf2/pbkdf2-hmac-sha512.js index 97b00f7..dec000b 100644 --- a/src/pbkdf2/pbkdf2-hmac-sha512.js +++ b/src/pbkdf2/pbkdf2-hmac-sha512.js @@ -1,25 +1,24 @@ -import {pbkdf2_constructor, pbkdf2_reset} from './pbkdf2'; +import {pbkdf2_constructor} from './pbkdf2'; import {get_hmac_sha512_instance, hmac_sha512_constructor} from '../hmac/hmac-sha512'; import {is_string} from '../utils'; import {IllegalArgumentError, IllegalStateError} from '../errors'; -export function pbkdf2_hmac_sha512_constructor ( options ) { +export class pbkdf2_hmac_sha512_constructor extends pbkdf2_constructor { + constructor(options ) { options = options || {}; if ( !( options.hmac instanceof hmac_sha512_constructor ) ) - options.hmac = get_hmac_sha512_instance(); + options.hmac = get_hmac_sha512_instance(); - pbkdf2_constructor.call( this, options ); + super(options); + } - return this; -} - -function pbkdf2_hmac_sha512_generate ( salt, count, length ) { + generate(salt, count, length ) { 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"); if ( !salt && !is_string(salt) ) - throw new IllegalArgumentError("bad 'salt' value"); + throw new IllegalArgumentError("bad 'salt' value"); count = count || this.count; length = length || this.length; @@ -29,22 +28,19 @@ function pbkdf2_hmac_sha512_generate ( salt, count, length ) { var blocks = Math.ceil( length / this.hmac.HMAC_SIZE ); for ( var i = 1; i <= blocks; ++i ) { - var j = ( i - 1 ) * this.hmac.HMAC_SIZE; - var l = ( i < blocks ? 0 : length % this.hmac.HMAC_SIZE ) || this.hmac.HMAC_SIZE; + var j = ( i - 1 ) * this.hmac.HMAC_SIZE; + var l = ( i < blocks ? 0 : length % this.hmac.HMAC_SIZE ) || this.hmac.HMAC_SIZE; - this.hmac.reset().process(salt); - this.hmac.hash.asm.pbkdf2_generate_block( this.hmac.hash.pos, this.hmac.hash.len, i, count, 0 ); + this.hmac.reset().process(salt); + this.hmac.hash.asm.pbkdf2_generate_block( this.hmac.hash.pos, this.hmac.hash.len, i, count, 0 ); - this.result.set( this.hmac.hash.heap.subarray( 0, l ), j ); + this.result.set( this.hmac.hash.heap.subarray( 0, l ), j ); } return this; + } } -var pbkdf2_hmac_sha512_prototype = pbkdf2_hmac_sha512_constructor.prototype; -pbkdf2_hmac_sha512_prototype.reset = pbkdf2_reset; -pbkdf2_hmac_sha512_prototype.generate = pbkdf2_hmac_sha512_generate; - var pbkdf2_hmac_sha512_instance = null; export function get_pbkdf2_hmac_sha512_instance () { diff --git a/src/pbkdf2/pbkdf2.js b/src/pbkdf2/pbkdf2.js index 3ba2c7a..1db41d9 100644 --- a/src/pbkdf2/pbkdf2.js +++ b/src/pbkdf2/pbkdf2.js @@ -1,14 +1,15 @@ import {is_string} from '../utils'; import {IllegalArgumentError, IllegalStateError} from '../errors'; -export function pbkdf2_constructor (options ) { +export class pbkdf2_constructor { + constructor(options ) { options = options || {}; if ( !options.hmac ) - throw new SyntaxError("option 'hmac' is required"); + throw new SyntaxError("option 'hmac' is required"); if ( !options.hmac.HMAC_SIZE ) - throw new SyntaxError("option 'hmac' supplied doesn't seem to be a valid HMAC function"); + throw new SyntaxError("option 'hmac' supplied doesn't seem to be a valid HMAC function"); this.hmac = options.hmac; this.count = options.count || 4096; @@ -18,25 +19,25 @@ export function pbkdf2_constructor (options ) { var password = options.password; if ( password || is_string(password) ) - this.reset(options); + this.reset(options); return this; -} + } -export function pbkdf2_reset ( options ) { + reset(options ) { this.result = null; this.hmac.reset(options); return this; -} + } -function pbkdf2_generate ( salt, count, length ) { + generate(salt, count, length ) { 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"); if ( !salt && !is_string(salt) ) - throw new IllegalArgumentError("bad 'salt' value"); + throw new IllegalArgumentError("bad 'salt' value"); count = count || this.count; length = length || this.length; @@ -46,20 +47,17 @@ function pbkdf2_generate ( salt, count, length ) { var blocks = Math.ceil( length / this.hmac.HMAC_SIZE ); for ( var i = 1; i <= blocks; ++i ) { - var j = ( i - 1 ) * this.hmac.HMAC_SIZE; - var l = ( i < blocks ? 0 : length % this.hmac.HMAC_SIZE ) || this.hmac.HMAC_SIZE; - var tmp = new Uint8Array( this.hmac.reset().process(salt).process( new Uint8Array([ i>>>24&0xff, i>>>16&0xff, i>>>8&0xff, i&0xff ]) ).finish().result ); - this.result.set( tmp.subarray( 0, l ), j ); - for ( var k = 1; k < count; ++k ) { - tmp = new Uint8Array( this.hmac.reset().process(tmp).finish().result ); - for ( var r = 0; r < l; ++r ) this.result[j+r] ^= tmp[r]; - } + var j = ( i - 1 ) * this.hmac.HMAC_SIZE; + var l = ( i < blocks ? 0 : length % this.hmac.HMAC_SIZE ) || this.hmac.HMAC_SIZE; + var tmp = new Uint8Array( this.hmac.reset().process(salt).process( new Uint8Array([ i>>>24&0xff, i>>>16&0xff, i>>>8&0xff, i&0xff ]) ).finish().result ); + this.result.set( tmp.subarray( 0, l ), j ); + for ( var k = 1; k < count; ++k ) { + tmp = new Uint8Array( this.hmac.reset().process(tmp).finish().result ); + for ( var r = 0; r < l; ++r ) this.result[j+r] ^= tmp[r]; + } } return this; + } } -// methods -var pbkdf2_prototype = pbkdf2_constructor.prototype; -pbkdf2_prototype.reset = pbkdf2_reset; -pbkdf2_prototype.generate = pbkdf2_generate;