Skip to content

Commit

Permalink
bcrypto: clean up hash functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Dec 6, 2019
1 parent ff3169d commit 51ae1e4
Show file tree
Hide file tree
Showing 61 changed files with 426 additions and 685 deletions.
17 changes: 6 additions & 11 deletions lib/blake2b160.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ const BLAKE2b = require('./blake2b');
*/

class BLAKE2b160 extends BLAKE2b {
/**
* Create a BLAKE2b160 context.
* @constructor
*/

constructor() {
super();
}

init(key = null) {
init(key) {
return super.init(20, key);
}

Expand All @@ -34,16 +29,16 @@ class BLAKE2b160 extends BLAKE2b {
return super.hmac(20);
}

static digest(data, key = null) {
static digest(data, key) {
return super.digest(data, 20, key);
}

static root(left, right) {
return super.root(left, right, 20);
static root(left, right, key) {
return super.root(left, right, 20, key);
}

static multi(x, y, z) {
return super.multi(x, y, z, 20);
static multi(x, y, z, key) {
return super.multi(x, y, z, 20, key);
}

static mac(data, key) {
Expand Down
17 changes: 6 additions & 11 deletions lib/blake2b256.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ const BLAKE2b = require('./blake2b');
*/

class BLAKE2b256 extends BLAKE2b {
/**
* Create a BLAKE2b256 context.
* @constructor
*/

constructor() {
super();
}

init(key = null) {
init(key) {
return super.init(32, key);
}

Expand All @@ -34,16 +29,16 @@ class BLAKE2b256 extends BLAKE2b {
return super.hmac(32);
}

static digest(data, key = null) {
static digest(data, key) {
return super.digest(data, 32, key);
}

static root(left, right) {
return super.root(left, right, 32);
static root(left, right, key) {
return super.root(left, right, 32, key);
}

static multi(x, y, z) {
return super.multi(x, y, z, 32);
static multi(x, y, z, key) {
return super.multi(x, y, z, 32, key);
}

static mac(data, key) {
Expand Down
17 changes: 6 additions & 11 deletions lib/blake2b384.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ const BLAKE2b = require('./blake2b');
*/

class BLAKE2b384 extends BLAKE2b {
/**
* Create a BLAKE2b384 context.
* @constructor
*/

constructor() {
super();
}

init(key = null) {
init(key) {
return super.init(48, key);
}

Expand All @@ -34,16 +29,16 @@ class BLAKE2b384 extends BLAKE2b {
return super.hmac(48);
}

static digest(data, key = null) {
static digest(data, key) {
return super.digest(data, 48, key);
}

static root(left, right) {
return super.root(left, right, 48);
static root(left, right, key) {
return super.root(left, right, 48, key);
}

static multi(x, y, z) {
return super.multi(x, y, z, 48);
static multi(x, y, z, key) {
return super.multi(x, y, z, 48, key);
}

static mac(data, key) {
Expand Down
17 changes: 6 additions & 11 deletions lib/blake2b512.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ const BLAKE2b = require('./blake2b');
*/

class BLAKE2b512 extends BLAKE2b {
/**
* Create a BLAKE2b512 context.
* @constructor
*/

constructor() {
super();
}

init(key = null) {
init(key) {
return super.init(64, key);
}

Expand All @@ -34,16 +29,16 @@ class BLAKE2b512 extends BLAKE2b {
return super.hmac(64);
}

static digest(data, key = null) {
static digest(data, key) {
return super.digest(data, 64, key);
}

static root(left, right) {
return super.root(left, right, 64);
static root(left, right, key) {
return super.root(left, right, 64, key);
}

static multi(x, y, z) {
return super.multi(x, y, z, 64);
static multi(x, y, z, key) {
return super.multi(x, y, z, 64, key);
}

static mac(data, key) {
Expand Down
17 changes: 6 additions & 11 deletions lib/blake2s128.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ const BLAKE2s = require('./blake2s');
*/

class BLAKE2s128 extends BLAKE2s {
/**
* Create a BLAKE2s128 context.
* @constructor
*/

constructor() {
super();
}

init(key = null) {
init(key) {
return super.init(16, key);
}

Expand All @@ -34,16 +29,16 @@ class BLAKE2s128 extends BLAKE2s {
return super.hmac(16);
}

static digest(data, key = null) {
static digest(data, key) {
return super.digest(data, 16, key);
}

static root(left, right) {
return super.root(left, right, 16);
static root(left, right, key) {
return super.root(left, right, 16, key);
}

static multi(x, y, z) {
return super.multi(x, y, z, 16);
static multi(x, y, z, key) {
return super.multi(x, y, z, 16, key);
}

static mac(data, key) {
Expand Down
17 changes: 6 additions & 11 deletions lib/blake2s160.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ const BLAKE2s = require('./blake2s');
*/

class BLAKE2s160 extends BLAKE2s {
/**
* Create a BLAKE2s160 context.
* @constructor
*/

constructor() {
super();
}

init(key = null) {
init(key) {
return super.init(20, key);
}

Expand All @@ -34,16 +29,16 @@ class BLAKE2s160 extends BLAKE2s {
return super.hmac(20);
}

static digest(data, key = null) {
static digest(data, key) {
return super.digest(data, 20, key);
}

static root(left, right) {
return super.root(left, right, 20);
static root(left, right, key) {
return super.root(left, right, 20, key);
}

static multi(x, y, z) {
return super.multi(x, y, z, 20);
static multi(x, y, z, key) {
return super.multi(x, y, z, 20, key);
}

static mac(data, key) {
Expand Down
17 changes: 6 additions & 11 deletions lib/blake2s224.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ const BLAKE2s = require('./blake2s');
*/

class BLAKE2s224 extends BLAKE2s {
/**
* Create a BLAKE2s224 context.
* @constructor
*/

constructor() {
super();
}

init(key = null) {
init(key) {
return super.init(28, key);
}

Expand All @@ -34,16 +29,16 @@ class BLAKE2s224 extends BLAKE2s {
return super.hmac(28);
}

static digest(data, key = null) {
static digest(data, key) {
return super.digest(data, 28, key);
}

static root(left, right) {
return super.root(left, right, 28);
static root(left, right, key) {
return super.root(left, right, 28, key);
}

static multi(x, y, z) {
return super.multi(x, y, z, 28);
static multi(x, y, z, key) {
return super.multi(x, y, z, 28, key);
}

static mac(data, key) {
Expand Down
17 changes: 6 additions & 11 deletions lib/blake2s256.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ const BLAKE2s = require('./blake2s');
*/

class BLAKE2s256 extends BLAKE2s {
/**
* Create a BLAKE2s256 context.
* @constructor
*/

constructor() {
super();
}

init(key = null) {
init(key) {
return super.init(32, key);
}

Expand All @@ -34,16 +29,16 @@ class BLAKE2s256 extends BLAKE2s {
return super.hmac(32);
}

static digest(data, key = null) {
static digest(data, key) {
return super.digest(data, 32, key);
}

static root(left, right) {
return super.root(left, right, 32);
static root(left, right, key) {
return super.root(left, right, 32, key);
}

static multi(x, y, z) {
return super.multi(x, y, z, 32);
static multi(x, y, z, key) {
return super.multi(x, y, z, 32, key);
}

static mac(data, key) {
Expand Down
34 changes: 24 additions & 10 deletions lib/cshake.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ class CSHAKE extends Keccak {
this.rate = 1088;
}

init(bits = 256, name = null, pers = null) {
init(bits, name, pers) {
if (bits == null)
bits = 256;

if (name == null)
name = EMPTY;

Expand Down Expand Up @@ -169,18 +172,26 @@ class CSHAKE extends Keccak {
return new CSHAKE();
}

static hmac(bits = 256, name, pers, len) {
static hmac(bits, name, pers, len) {
if (bits == null)
bits = 256;

assert((bits >>> 0) === bits);
assert(bits === 128 || bits === 256);
const bs = (1600 - bits * 2) / 8;
return new HMAC(CSHAKE, bs, [bits, name, pers], [len]);

const rate = 1600 - bits * 2;

return new HMAC(CSHAKE, rate / 8, [bits, name, pers], [len]);
}

static digest(data, bits = 256, name, pers, len) {
static digest(data, bits, name, pers, len) {
return CSHAKE.ctx.init(bits, name, pers).update(data).final(len);
}

static root(left, right, bits = 256, name, pers, len) {
static root(left, right, bits, name, pers, len) {
if (bits == null)
bits = 256;

if (len == null)
len = 0;

Expand All @@ -193,7 +204,7 @@ class CSHAKE extends Keccak {
assert(Buffer.isBuffer(left) && left.length === len);
assert(Buffer.isBuffer(right) && right.length === len);

const ctx = CSHAKE.ctx;
const {ctx} = CSHAKE;

ctx.init(bits, name, pers);
ctx.update(left);
Expand All @@ -202,17 +213,20 @@ class CSHAKE extends Keccak {
return ctx.final(len);
}

static multi(x, y, z, bits = 256, name, pers, len) {
const ctx = CSHAKE.ctx;
static multi(x, y, z, bits, name, pers, len) {
const {ctx} = CSHAKE;

ctx.init(bits, name, pers);
ctx.update(x);
ctx.update(y);

if (z)
ctx.update(z);

return ctx.final(len);
}

static mac(data, key, bits = 256, name, pers, len) {
static mac(data, key, bits, name, pers, len) {
return CSHAKE.hmac(bits, name, pers, len).init(key).update(data).final();
}
}
Expand Down
Loading

0 comments on commit 51ae1e4

Please sign in to comment.