Skip to content

Commit

Permalink
Optimize padding calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
nf404 committed Jul 7, 2019
1 parent 7821ef0 commit 2905748
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/hasher/hasher.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class Hasher {

/**
* Add PKCS7 padding to message
* Pad with bytes all of the same value as the number of padding bytes
*
* @protected
* @param {number} length
Expand All @@ -128,6 +129,7 @@ class Hasher {

/**
* Add ISO7816-4 padding to message
* Pad with 0x80 followed by zero bytes
*
* @protected
* @param {number} length
Expand All @@ -138,6 +140,7 @@ class Hasher {

/**
* Add zero padding to message
* Pad with 0x00 characters
*
* @protected
* @param {number} length
Expand Down
11 changes: 6 additions & 5 deletions src/hasher/hasher32be.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ class Hasher32be extends Hasher {
*/
addLengthBits() {
// @todo fix length to 64 bit
this.state.message += "\x00\x00\x00\x00";
let lengthBits = this.state.length << 3;
for (let i = 3; i >= 0; i--) {
this.state.message += String.fromCharCode(lengthBits >> (i << 3));
}
this.state.message += "\x00\x00\x00" +
String.fromCharCode(this.state.length >> 29 & 0xff) +
String.fromCharCode(this.state.length >> 21 & 0xff) +
String.fromCharCode(this.state.length >> 13 & 0xff) +
String.fromCharCode(this.state.length >> 5 & 0xff) +
String.fromCharCode(this.state.length << 3 & 0xff);
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/hasher/hasher32le.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ class Hasher32le extends Hasher {
* @protected
*/
addLengthBits() {
let lengthBits = this.state.length << 3;
for (let i = 0; i < 4; i++) {
this.state.message += String.fromCharCode(lengthBits >> (i << 3));
}
// @todo fix length to 64 bit
this.state.message += "\x00\x00\x00\x00";
this.state.message +=
String.fromCharCode(this.state.length << 3 & 0xff) +
String.fromCharCode(this.state.length >> 5 & 0xff) +
String.fromCharCode(this.state.length >> 13 & 0xff) +
String.fromCharCode(this.state.length >> 21 & 0xff) +
String.fromCharCode(this.state.length >> 29 & 0xff) +
"\x00\x00\x00";
}
}

Expand Down

0 comments on commit 2905748

Please sign in to comment.