| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | /** @typedef {import("./Hash")} Hash */
|
|---|
| 9 | /** @typedef {import("../../declarations/WebpackOptions").HashFunction} HashFunction */
|
|---|
| 10 |
|
|---|
| 11 | /** @type {typeof import("crypto") | undefined} */
|
|---|
| 12 | let crypto;
|
|---|
| 13 | /** @type {typeof import("./hash/xxhash64") | undefined} */
|
|---|
| 14 | let createXXHash64;
|
|---|
| 15 | /** @type {typeof import("./hash/md4") | undefined} */
|
|---|
| 16 | let createMd4;
|
|---|
| 17 | /** @type {typeof import("./hash/DebugHash") | undefined} */
|
|---|
| 18 | let DebugHash;
|
|---|
| 19 | /** @type {typeof import("./hash/BatchedHash") | undefined} */
|
|---|
| 20 | let BatchedHash;
|
|---|
| 21 | /** @type {typeof import("./hash/BulkUpdateHash") | undefined} */
|
|---|
| 22 | let BulkUpdateHash;
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * Creates a hash by name or function
|
|---|
| 26 | * @param {HashFunction} algorithm the algorithm name or a constructor creating a hash
|
|---|
| 27 | * @returns {Hash} the hash
|
|---|
| 28 | */
|
|---|
| 29 | module.exports = (algorithm) => {
|
|---|
| 30 | if (typeof algorithm === "function") {
|
|---|
| 31 | if (BulkUpdateHash === undefined) {
|
|---|
| 32 | BulkUpdateHash = require("./hash/BulkUpdateHash");
|
|---|
| 33 | }
|
|---|
| 34 | // eslint-disable-next-line new-cap
|
|---|
| 35 | return new BulkUpdateHash(() => new algorithm());
|
|---|
| 36 | }
|
|---|
| 37 | switch (algorithm) {
|
|---|
| 38 | // TODO add non-cryptographic algorithm here
|
|---|
| 39 | case "debug":
|
|---|
| 40 | if (DebugHash === undefined) {
|
|---|
| 41 | DebugHash = require("./hash/DebugHash");
|
|---|
| 42 | }
|
|---|
| 43 | return new DebugHash();
|
|---|
| 44 | case "xxhash64":
|
|---|
| 45 | if (createXXHash64 === undefined) {
|
|---|
| 46 | createXXHash64 = require("./hash/xxhash64");
|
|---|
| 47 | if (BatchedHash === undefined) {
|
|---|
| 48 | BatchedHash = require("./hash/BatchedHash");
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 | return new /** @type {typeof import("./hash/BatchedHash")} */ (
|
|---|
| 52 | BatchedHash
|
|---|
| 53 | )(createXXHash64());
|
|---|
| 54 | case "md4":
|
|---|
| 55 | if (createMd4 === undefined) {
|
|---|
| 56 | createMd4 = require("./hash/md4");
|
|---|
| 57 | if (BatchedHash === undefined) {
|
|---|
| 58 | BatchedHash = require("./hash/BatchedHash");
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 | return new /** @type {typeof import("./hash/BatchedHash")} */ (
|
|---|
| 62 | BatchedHash
|
|---|
| 63 | )(createMd4());
|
|---|
| 64 | case "native-md4":
|
|---|
| 65 | if (crypto === undefined) crypto = require("crypto");
|
|---|
| 66 | if (BulkUpdateHash === undefined) {
|
|---|
| 67 | BulkUpdateHash = require("./hash/BulkUpdateHash");
|
|---|
| 68 | }
|
|---|
| 69 | return new BulkUpdateHash(
|
|---|
| 70 | () =>
|
|---|
| 71 | /** @type {Hash} */ (
|
|---|
| 72 | /** @type {typeof import("crypto")} */
|
|---|
| 73 | (crypto).createHash("md4")
|
|---|
| 74 | ),
|
|---|
| 75 | "md4"
|
|---|
| 76 | );
|
|---|
| 77 | default:
|
|---|
| 78 | if (crypto === undefined) crypto = require("crypto");
|
|---|
| 79 | if (BulkUpdateHash === undefined) {
|
|---|
| 80 | BulkUpdateHash = require("./hash/BulkUpdateHash");
|
|---|
| 81 | }
|
|---|
| 82 | return new BulkUpdateHash(
|
|---|
| 83 | () =>
|
|---|
| 84 | /** @type {Hash} */ (
|
|---|
| 85 | /** @type {typeof import("crypto")} */
|
|---|
| 86 | (crypto).createHash(algorithm)
|
|---|
| 87 | ),
|
|---|
| 88 | algorithm
|
|---|
| 89 | );
|
|---|
| 90 | }
|
|---|
| 91 | };
|
|---|