webpack/lib/util/createHash.js

138 lines
3.3 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const AbstractMethodError = require("../AbstractMethodError");
2017-11-23 11:44:56 +01:00
const BULK_SIZE = 1000;
class Hash {
2019-07-10 11:42:34 +02:00
/**
* Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
* @param {string|Buffer} data data
* @param {string=} inputEncoding data encoding
* @returns {this} updated hash
2019-07-10 11:42:34 +02:00
*/
update(data, inputEncoding) {
throw new AbstractMethodError();
2019-07-10 11:42:34 +02:00
}
/**
* Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
* @param {string=} encoding encoding of the return value
2019-07-10 11:42:34 +02:00
* @returns {string|Buffer} digest
*/
digest(encoding) {
throw new AbstractMethodError();
2019-07-10 11:42:34 +02:00
}
}
exports.Hash = Hash;
/** @typedef {typeof Hash} HashConstructor */
2019-07-10 11:42:34 +02:00
class BulkUpdateDecorator extends Hash {
2019-07-10 11:42:34 +02:00
/**
* @param {Hash} hash hash
2019-07-10 11:42:34 +02:00
*/
constructor(hash) {
2019-07-10 11:42:34 +02:00
super();
this.hash = hash;
this.buffer = "";
}
2019-07-10 11:42:34 +02:00
/**
* Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
* @param {string|Buffer} data data
* @param {string=} inputEncoding data encoding
* @returns {this} updated hash
2019-07-10 11:42:34 +02:00
*/
update(data, inputEncoding) {
2018-02-25 02:00:20 +01:00
if (
inputEncoding !== undefined ||
typeof data !== "string" ||
data.length > BULK_SIZE
) {
if (this.buffer.length > 0) {
2017-11-23 11:44:56 +01:00
this.hash.update(this.buffer);
this.buffer = "";
}
this.hash.update(data, inputEncoding);
} else {
this.buffer += data;
2018-02-25 02:00:20 +01:00
if (this.buffer.length > BULK_SIZE) {
2017-11-23 11:44:56 +01:00
this.hash.update(this.buffer);
this.buffer = "";
}
}
return this;
}
2019-07-10 11:42:34 +02:00
/**
* Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
* @param {string=} encoding encoding of the return value
2019-07-10 11:42:34 +02:00
* @returns {string|Buffer} digest
*/
digest(encoding) {
2018-02-25 02:00:20 +01:00
if (this.buffer.length > 0) {
2017-11-23 11:44:56 +01:00
this.hash.update(this.buffer);
}
var digestResult = this.hash.digest(encoding);
2018-02-25 02:00:20 +01:00
return typeof digestResult === "string"
? digestResult
: digestResult.toString();
}
}
2019-07-10 11:42:34 +02:00
/**
* istanbul ignore next
*/
class DebugHash extends Hash {
constructor() {
2019-07-10 11:42:34 +02:00
super();
2018-01-20 11:28:45 +01:00
this.string = "";
}
2019-07-10 11:42:34 +02:00
/**
* Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
* @param {string|Buffer} data data
* @param {string=} inputEncoding data encoding
* @returns {this} updated hash
2019-07-10 11:42:34 +02:00
*/
update(data, inputEncoding) {
2018-02-25 02:00:20 +01:00
if (typeof data !== "string") data = data.toString("utf-8");
this.string += data;
return this;
}
2019-07-10 11:42:34 +02:00
/**
* Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
* @param {string=} encoding encoding of the return value
2019-07-10 11:42:34 +02:00
* @returns {string|Buffer} digest
*/
digest(encoding) {
2018-02-25 02:00:20 +01:00
return this.string.replace(/[^a-z0-9]+/gi, m =>
Buffer.from(m).toString("hex")
);
}
}
2018-06-25 10:44:54 +02:00
/**
* Creates a hash by name or function
* @param {string | HashConstructor} algorithm the algorithm name or a constructor creating a hash
* @returns {Hash} the hash
2018-06-25 10:44:54 +02:00
*/
module.exports = algorithm => {
2018-02-25 02:00:20 +01:00
if (typeof algorithm === "function") {
return new BulkUpdateDecorator(new algorithm());
}
2018-02-25 02:00:20 +01:00
switch (algorithm) {
// TODO add non-cryptographic algorithm here
case "debug":
return new DebugHash();
default:
return new BulkUpdateDecorator(require("crypto").createHash(algorithm));
}
};