aboutsummaryrefslogtreecommitdiff
path: root/node_modules/loader-utils/lib/getHashDigest.js
diff options
context:
space:
mode:
authorruki <waruqi@gmail.com>2018-11-08 00:38:48 +0800
committerruki <waruqi@gmail.com>2018-11-07 21:53:09 +0800
commit26105034da4fcce7ac883c899d781f016559310d (patch)
treec459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/loader-utils/lib/getHashDigest.js
parent2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff)
downloadxmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz
xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip
switch to vuepress
Diffstat (limited to 'node_modules/loader-utils/lib/getHashDigest.js')
-rw-r--r--node_modules/loader-utils/lib/getHashDigest.js53
1 files changed, 53 insertions, 0 deletions
diff --git a/node_modules/loader-utils/lib/getHashDigest.js b/node_modules/loader-utils/lib/getHashDigest.js
new file mode 100644
index 00000000..6f4a427e
--- /dev/null
+++ b/node_modules/loader-utils/lib/getHashDigest.js
@@ -0,0 +1,53 @@
+"use strict";
+
+const baseEncodeTables = {
+ 26: "abcdefghijklmnopqrstuvwxyz",
+ 32: "123456789abcdefghjkmnpqrstuvwxyz", // no 0lio
+ 36: "0123456789abcdefghijklmnopqrstuvwxyz",
+ 49: "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", // no lIO
+ 52: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
+ 58: "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", // no 0lIO
+ 62: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
+ 64: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_"
+};
+
+function encodeBufferToBase(buffer, base) {
+ const encodeTable = baseEncodeTables[base];
+ if(!encodeTable) throw new Error("Unknown encoding base" + base);
+
+ const readLength = buffer.length;
+
+ const Big = require("big.js");
+ Big.RM = Big.DP = 0;
+ let b = new Big(0);
+ for(let i = readLength - 1; i >= 0; i--) {
+ b = b.times(256).plus(buffer[i]);
+ }
+
+ let output = "";
+ while(b.gt(0)) {
+ output = encodeTable[b.mod(base)] + output;
+ b = b.div(base);
+ }
+
+ Big.DP = 20;
+ Big.RM = 1;
+
+ return output;
+}
+
+function getHashDigest(buffer, hashType, digestType, maxLength) {
+ hashType = hashType || "md5";
+ maxLength = maxLength || 9999;
+ const hash = require("crypto").createHash(hashType);
+ hash.update(buffer);
+ if(digestType === "base26" || digestType === "base32" || digestType === "base36" ||
+ digestType === "base49" || digestType === "base52" || digestType === "base58" ||
+ digestType === "base62" || digestType === "base64") {
+ return encodeBufferToBase(hash.digest(), digestType.substr(4)).substr(0, maxLength);
+ } else {
+ return hash.digest(digestType || "hex").substr(0, maxLength);
+ }
+}
+
+module.exports = getHashDigest;