kkamkou/node-concardis

View on GitHub
lib/request/CollectionChecksum.js

Summary

Maintainability
A
0 mins
Test Coverage
/**
 * Licensed under the MIT License
 *
 * @author   Kanstantsin A Kamkou (2ka.by)
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     https://github.com/kkamkou/node-concardis
 */

'use strict';

const crypto = require('crypto');

const Collection = require('./Collection');

/*public final*/ class CollectionChecksum {
  constructor(collection, secret, signAlgorithm) {
    if (!(collection instanceof Collection)) {
      throw new TypeError('Query "Collection" is expected');
    }

    if (!collection.size()) {
      throw new Error('Collection is empty');
    }

    this.secret = secret;
    this.collection = collection;
    this.signatureAlgorithm = signAlgorithm || 'sha256';
  }

  toString() {
    return crypto
      .createHash(this.signatureAlgorithm, this.secret)
      .update(
        this.collection.toArray()
          .filter(v => v.value !== '')
          .map(v => v.toString() + this.secret)
          .join('')
        ,
        'utf8'
      )
      .digest('hex').toUpperCase();
  }
}

module.exports = CollectionChecksum;